【初探篇】反向代理在系统结构中的应用场景

news/2024/10/18 14:15:25/

🌱本专栏将会从基础开始,循序渐进讲解Nginx的使用和有关配置,也请大家多多支持,希望大家都能够从中获益。
📫专栏地址:Nginx
📫相关软件:链接地址

文章目录

    • 反向代理在系统结构中的应用场景
      • Nginx的反向代理配置
      • 基于反向代理的负载均衡器
        • 将101代理到102
        • 配置101的负载均衡(轮询模式)
        • 配置101的负载均衡(权重模式)
        • 其他负载均衡策略(不常用)

反向代理在系统结构中的应用场景

image-20220430170948611

反向代理方式是指以代理服务器来接受Internet上的连接请求,然后将请求转发给内部网络上的服务器;并将从服务器上得到的结果返回给Internet上请求连接的客户端,此时代理服务器对外就表现为一个服务器。

反向代理服务器通常有两种模型,一种是作为内容服务器的替身,另一种作为内容服务器集群的负载均衡器。

  • 作内容服务器的替身

如果您的内容服务器具有必须保持安全的敏感信息,如信用卡号数据库,可在防火墙外部设置一个代理服务器作为内容服务器的替身。当外部客户机尝试访问内容服务器时,会将其送到代理服务器。实际内容位于内容服务器上,在防火墙内部受到安全保护。代理服务器位于防火墙外部,在客户机看来就像是内容服务器。

当客户机向站点提出请求时,请求将转到代理服务器。然后,代理服务器通过防火墙中的特定通路,将客户机的请求发送到内容服务器。内容服务器再通过该通道将结果回传给代理服务器。代理服务器将检索到的信息发送给客户机,好像代理服务器就是实际的内容服务器。如果内容服务器返回错误消息,代理服务器会先行截取该消息并更改标头中列出的任何 URL,然后再将消息发送给客户机。如此可防止外部客户机获取内部内容服务器的重定向 URL。

这样,代理服务器就在安全数据库和可能的恶意攻击之间提供了又一道屏障。与有权访问整个数据库的情况相对比,就算是侥幸攻击成功,作恶者充其量也仅限于访问单个事务中所涉及的信息。未经授权的用户无法访问到真正的内容服务器,因为防火墙通路只允许代理服务器有权进行访问。

  • 作为内容服务器的负载均衡器

可以在一个组织内使用多个代理服务器来平衡各 Web 服务器间的网络负载。在此模型中,可以利用代理服务器的高速缓存特性,创建一个用于负载平衡的服务器池。此时,代理服务器可以位于防火墙的任意一侧。如果 Web 服务器每天都会接收大量的请求,则可以使用代理服务器分担 Web 服务器的负载并提高网络访问效率。

对于客户机发往真正服务器的请求,代理服务器起着中间调停者的作用。代理服务器会将所请求的文档存入高速缓存。如果有不止一个代理服务器,DNS 可以采用“轮询法”选择其 IP 地址,随机地为请求选择路由。客户机每次都使用同一个 URL,但请求所采取的路由每次都可能经过不同的代理服务器。

可以使用多个代理服务器来处理对一个高用量内容服务器的请求,这样做的好处是内容服务器可以处理更高的负载,并且比其独自工作时更有效率。在初始启动期间,代理服务器首次从内容服务器检索文档,此后,对内容服务器的请求数会大大下降。

Nginx的反向代理配置

配置如下:

image-20220430182343864

访问http://test80.xzj520520.cn/:

image-20220430182313784

基于反向代理的负载均衡器

克隆两个centos,ip分别设为192.168.8.102,192.168.8.103(网段要用自己的电脑对应)

image-20220430184102076

修改静态ip

image-20220430184135760

image-20220430184225267

重启网络服务:

image-20220430184320760

另一个虚拟机的配置也是一样。配置好虚拟机之后,配置nginx.cfg

102的nginx.cfg

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

102的nginx在html目录下的index.html文件

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>I am from 192.168.8.102</h1>
</body>
</html>

103的nginx.cfg

#user  nobody;
worker_processes  1;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}# another virtual host using mix of IP-, name-, and port-based configuration##server {#    listen       8000;#    listen       somename:8080;#    server_name  somename  alias  another.alias;#    location / {#        root   html;#        index  index.html index.htm;#    }#}# HTTPS server##server {#    listen       443 ssl;#    server_name  localhost;#    ssl_certificate      cert.pem;#    ssl_certificate_key  cert.key;#    ssl_session_cache    shared:SSL:1m;#    ssl_session_timeout  5m;#    ssl_ciphers  HIGH:!aNULL:!MD5;#    ssl_prefer_server_ciphers  on;#    location / {#        root   html;#        index  index.html index.htm;#    }#}}

103的nginx在html目录下的index.html文件

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>I am from 192.168.8.103</h1>
</body>
</html>

测试:

访问http://192.168.8.102/

image-20220430185446556

访问http://192.168.8.103/

image-20220430185502170

将101代理到102

101的nginx.conf

worker_processes  1; #允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个events {#单个进程最大连接数(最大连接数=连接数*进程数)#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。worker_connections  1024;
}http {#文件扩展名与文件类型映射表(是conf目录下的一个文件)include       mime.types;#默认文件类型,如果mime.types预先定义的类型没匹配上,默认使用二进制流的方式传输default_type  application/octet-stream;#sendfile指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度。sendfile        on;#长连接超时时间,单位是秒keepalive_timeout  65;#虚拟主机的配置server {#监听端口listen       80;#域名,可以有多个,用空格隔开server_name  test80.xzj520520.cn;#配置根目录以及默认页面location / {proxy_pass http://192.168.8.102;# root   /www/test80;# index  index.html index.htm;}#出错页面配置error_page   500 502 503 504  /50x.html;#/50x.html文件所在位置location = /50x.html {root   html;}}}

测试,访问192.168.8.101

image-20220430185930024

配置101的负载均衡(轮询模式)

image-20220430190409638

worker_processes  1; #允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个events {#单个进程最大连接数(最大连接数=连接数*进程数)#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。worker_connections  1024;
}http {#文件扩展名与文件类型映射表(是conf目录下的一个文件)include       mime.types;#默认文件类型,如果mime.types预先定义的类型没匹配上,默认使用二进制流的方式传输default_type  application/octet-stream;#sendfile指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度。sendfile        on;#长连接超时时间,单位是秒keepalive_timeout  65;#定义一组服务器
upstream httpds{server 192.168.8.102:80;server 192.168.8.103:80;
}#虚拟主机的配置server {#监听端口listen       80;#域名,可以有多个,用空格隔开server_name  test80.xzj520520.cn;#配置根目录以及默认页面location / {proxy_pass http://httpds;# root   /www/test80;# index  index.html index.htm;}#出错页面配置error_page   500 502 503 504  /50x.html;#/50x.html文件所在位置location = /50x.html {root   html;}}}

测试:

多次访问http://192.168.8.101/发现102和103被交替访问

image-20220430190456271

image-20220430190504857

配置101的负载均衡(权重模式)

worker_processes  1; #允许进程数量,建议设置为cpu核心数或者auto自动检测,注意Windows服务器上虽然可以启动多个processes,但是实际只会用其中一个events {#单个进程最大连接数(最大连接数=连接数*进程数)#根据硬件调整,和前面工作进程配合起来用,尽量大,但是别把cpu跑到100%就行。worker_connections  1024;
}http {#文件扩展名与文件类型映射表(是conf目录下的一个文件)include       mime.types;#默认文件类型,如果mime.types预先定义的类型没匹配上,默认使用二进制流的方式传输default_type  application/octet-stream;#sendfile指令指定nginx是否调用sendfile 函数(zero copy 方式)来输出文件,对于普通应用,必须设为on。如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络IO处理速度。sendfile        on;#长连接超时时间,单位是秒keepalive_timeout  65;#定义一组服务器
upstream httpds{server 192.168.8.102 weight=10;server 192.168.8.103 weight=1;# server 192.168.8.102 weight=10 down; #down表示不参与负载均衡# server 192.168.8.102 weight=10 backup; #backup表示是备用服务器,没有服务器可用的时候使用
}#虚拟主机的配置server {#监听端口listen       80;#域名,可以有多个,用空格隔开server_name  test80.xzj520520.cn;#配置根目录以及默认页面location / {proxy_pass http://httpds;# root   /www/test80;# index  index.html index.htm;}#出错页面配置error_page   500 502 503 504  /50x.html;#/50x.html文件所在位置location = /50x.html {root   html;}}}

测试:

多次访问http://192.168.8.101/发现102访问的次数多余103访问的次数。

其他负载均衡策略(不常用)

  • ip_hash

根据客户端的ip地址转发同一台服务器,可以保持会话,但是很少用这种方式去保持会话,例如我们当前正在使用wifi访问,当切换成手机信号访问时,会话就不保持了。

  • least_conn

最少连接访问,优先访问连接最少的那一台服务器,这种方式也很少使用,因为连接少,可能是由于该服务器配置较低,刚开始赋予的权重较低。

  • url_hash(需要第三方插件)

根据用户访问的url定向转发请求,不同的url转发到不同的服务器进行处理(定向流量转发)。

  • fair(需要第三方插件)

根据后端服务器响应时间转发请求,这种方式也很少使用,因为容易造成流量倾斜,给某一台服务器压垮。


http://www.ppmy.cn/news/248286.html

相关文章

iss信息服务器,配置iss服务器

配置iss服务器 内容精选 换一换 可以。您可以在云服务器关机后,对云服务器变更规格。温馨提醒:目前包周期云服务器只支持升级配置。 网站接入WAF防护之后,若您访问网站时出现404 Not Found、502 Bad Gateway,504 Gateway Timeout等错误,请参考以下方法解决。网站接入WAF后…

独立服务器和vps的区别

独立服务器是指单独的一台服务器&#xff0c;而vps服务器大多是在独立服务器上面虚拟出来的共享服务器。 内存的区别&#xff0c;独立服务器是完全独立控制&#xff0c;100%内存独立使用权限&#xff1b;而vps则是和其他人共享&#xff0c;内存消耗较大&#xff0c;实际得到的可…

Varnish反向代理服务器和 HTTP 加速器

Varnish 是一款高性能且开源的反向代理服务器和 HTTP 加速器&#xff0c;其采用全新的软件体系机构&#xff0c;和现在的硬件体系紧密配合&#xff0c;与传统的 squid 相比&#xff0c;varnish 具有性能更高、速度更快、管理更加方便等诸多优点。 什么是反向代理&#xff1f; …

游戏服服务器——DB服

服务器框架 https://blog.csdn.net/nie2314550441/article/details/105981967 一、DB服介绍 整个服务器&#xff08;除网站后台&#xff09;与数据库交互都是通过DB服进行。DB服用于数据库交互和数据缓存&#xff0c;使用sqlserver做为存储&#xff0c;用来保存用户的所有相关…

Squid代理服务器(透明代理服务配置、日志分析、反向代理、日志分析、ACL访问控制)

目录 一.Squid 服务基础 (1)代理的工作机制 (2)代理的基本类型 二.安装 Squid 服务 (1)编译安装 Squid (2)修改 Squid 的配置文件 (3)Squid 的运行控制 (4)创建 Squid 服务脚本 三.构建传统代理服务器 四.构建透明代理服务器 五.ACL访问控制 六.Squid 日志分析 七.Squi…

原神3.2私人服务器搭建

写在前面&#xff1a;本人小白&#xff0c;最近想试试原神&#xff0c;于是学习了一下私人服务器搭建方法&#xff0c;在此留下心得与记录。我更愿意称现在的原神私人服务器为原神debug模式&#xff0c;因为在私人服务器中任务系统不完善&#xff1b;某些地方存在bug&#xff0…

Web服务器之Nginx介绍

一.Nginx简介 Nginx (engine x) 是一个高性能的Web和反向代理服务器&#xff0c;同时也是一个 IMAP/POP3/SMTP 代理服器。Nginx处理高并发能力是十分强大的&#xff0c;能经受高负载的考验。而且支持热部署&#xff0c;几乎可以做到 7 * 24 小时不间断运行&#xff0c;即使运行…

大话西游服务端开服架设服务器搭建教程

大话西游服务端开服架设服务器搭建教程 大话西游一款回合制角色扮演手游&#xff0c;游戏内包含人族、仙族、魔族、鬼族四大种族&#xff0c;每个种族各有4个角色可供玩家选择。相信很多玩这款游戏的玩家也有不少想知道自己怎么可以开一个sf&#xff0c;自己当服主&#xff0c…