当前位置: 首页 >服务端 > Nginx做web服务器反向代理

Nginx做web服务器反向代理

实验目的

通过nginx实现反向代理的功能,类似apache反向代理和haproxy反向代理

工作中用nginx做反向代理和负载均衡的也越来越多了

有些公司从web服务器到反向代理,都使用nginx。nginx在1.9版本加入了tcp的反向代理功能
甚至安全策略:nginx+lua 完全可以搞定。

 

打开nginx官网

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

 

nginx做反向代理,安装命令如下,使用www用户运行nginx

useradd -s /sbin/noglogin -M wwwwget http://nginx.org/download/nginx-1.9.12.tar.gztar zxf nginx-1.9.12.tar.gzcd nginx-1.9.12./configure --prefix=/usr/local/nginx-1.9.12 \--user=www --group=www  --with-http_ssl_module \--with-http_stub_status_module  --with-file-aiomake && make installln -s  /usr/local/nginx-1.9.12/  /usr/local/nginx

检查语法

[root@linux-node2 nginx-1.9.12]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful[root@linux-node2 nginx-1.9.12]# 

检查服务器有无其它服务占用80端口,可以关闭了。

[root@linux-node1 ~]# /usr/local/httpd/bin/apachectl -k stop

 

 

配置nginx反向代理,修改主配置文件

gzip是默认关闭的
长连接默认打开的
sendfile 默认打开的

[root@linux-node1 conf]# cat nginx.conf#user  nobody;worker_processes  1;#error_log  logs/error.log;#error_log  logs/error.log  notice;#error_log  logs/error.log  info;#pidlogs/nginx.pid;events {worker_connections  10240;}http {includemime.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;sendfileon;#tcp_nopush on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on; upstream backend {server 10.0.1.105:8080 weight=1  max_fails=3 fail_timeout=30s;server 10.0.1.106:8080 weight=2  max_fails=3 fail_timeout=30s;}server {listen80;server_name  www.nginx-nmap.com;#charset koi8-r;#access_log  logs/host.access.log  main;location / {roothtml;index  index.html index.htm;proxy_pass http://backend;}#error_page  404  /404.html;# redirect server error pages to the static page /50x.html#error_page500 502 503 504  /50x.html;location = /50x.html {roothtml;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#proxy_passhttp://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#roothtml;#fastcgi_pass127.0.0.1:9000;#fastcgi_index  index.php;#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#includefastcgi_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 {#listen8000;#listensomename:8080;#server_name  somename  alias  another.alias;#location / {#roothtml;#index  index.html index.htm;#}#}# HTTPS server##server {#listen443 ssl;#server_name  localhost;#ssl_certificate  cert.pem;#ssl_certificate_key  cert.key;#ssl_session_cacheshared:SSL:1m;#ssl_session_timeout  5m;#ssl_ciphers  HIGH:!aNULL:!MD5;#ssl_prefer_server_ciphers  on;#location / {#roothtml;#index  index.html index.htm;#}#}}[root@linux-node1 conf]# 
上面设置虚拟主机名www.nginx-nmap.com,以及后端集群组backend,设置了location把任何请求都发给后端backend
 
 
上面配置文件里也设置了后端web集群

负载均衡配置时的2个参数:fail_timeout和max_fails
这2个参数一起配合,来控制nginx怎样认为upstream中的某个server是失效的当在fail_timeout的时间内,某个server连接失败了max_fails次,则nginx会认为该server不工作了。
同时,在接下来的 fail_timeout时间内,nginx不再将请求分发给失效的server。
比如失败3次,那么接下来10秒不会之内不会把请求发个这个认为失败的机器。然后过了30秒后,这个机器继续收到探测请求.一般生产中设置为30秒

 upstream backend {server 10.0.1.105:8080 weight=1  max_fails=3 fail_timeout=30s;server 10.0.1.106:8080 weight=2  max_fails=3 fail_timeout=30s;}

  

 

 关于nginx反向代理功能由下面模块提供

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

 

 

可以参照下官方个的配置例子
官方文档做的挺好

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

 

 检测语法,启动或者reload。查看监听状态

[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload[root@linux-node1 conf]# netstat -lntp | grep 80tcp0  0 0.0.0.0:80  0.0.0.0:*LISTEN  27141/nginx: master tcp60  0 :::8080 :::*LISTEN  20130/httpd [root@linux-node1 conf]# 

  

客户端windows的hosts文件里配置如下

浏览器测试

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

停止node2的httpd。nginx会自动把请求发送给node1,前端无感知
[root@linux-node2 nginx-1.9.12]# systemctl stop httpd[root@linux-node2 nginx-1.9.12]# systemctl start httpd[root@linux-node2 nginx-1.9.12]# 
启动node2的httpd之后,刷30秒,node2才出现,也就是我们设置的fail_timeout=30的缘故

  

关于会话保持

会话保持,有基于ip的有ip_hash

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

直接添加这一行即可

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

重启

[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -tnginx: the configuration file /usr/local/nginx-1.9.12/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx-1.9.12/conf/nginx.conf test is successful[root@linux-node1 conf]# /usr/local/nginx/sbin/nginx -s reload[root@linux-node1 conf]# 
 
再次访问就只有node2了

Nginx做web服务器反向代理 _ JavaClub全栈架构师技术笔记

关于nginx的负载均衡算法有很多,自行百度

 

 

 

 

来源链接:https://www.cnblogs.com/nmap/p/6492208.html

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/113204.html

标签:Nginx
分享给朋友:

“Nginx做web服务器反向代理” 的相关文章