当前位置:   article > 正文

【运维知识进阶篇】集群架构-Nginx反向代理详解_nginx反向代理配置详解

nginx反向代理配置详解

在互联网请求中,客户端通常无法直接向服务端发起请求,就需要用代理服务,来实现客户端和的交互,起到一个中介的作用。

Nginx代理服务常见模式

Nginx代理按照应用场景模式可以分为正向代理和反向代理。

正向代理是内部上网过程中,客户端经过代理访问服务端

反向代理是公司集群架构中,客户端通过代理反向返回数据给服务端。反向代理是负载均衡的前身,本篇文章详细给大家介绍Nginx反向代理。

Nginx作为支持的代理协议

超文本传输协议

http/https协议

tcp/dup协议
http1.1长连接通讯协议
go语言远程调用、python语言远程调用
右键收发协议
流媒体、直播、点播

Nginx常用代理协议

http_proxy(Http Server底层和Socket底层)、fastcgi(Nginx转发给PHP,也可以理解成PHP是nginx代理)、uwcgi(python Server)

Nginx反向代理的模式支持的模块

反向代理模式Nginx配置模块
http、websocket、httpsngx_http_proxy_module
tastcgingx_http_fastcgi_module
uwcgingx_http_uwcgi_module
grpcngx_http_v2_module

Nginx反向代理配置语法

  1. [root@Web01 04]# vim /etc/nginx/conf.d/default.conf
  2. location / {
  3. root /code;
  4. index index.php index.html;
  5. }
  6. location ~ \.php$ {
  7. root /code;
  8. fastcgi_pass 127.0.0.1:9000; #将当前请求转发给后端代理,后面可以是域名+端口+uri或者是IP地址+端口+uri
  9. fastcgi_param SCRIPT_FILENAME $document_
  10. root$fastcgi_script_name;
  11. include fastcgi_params;
  12. }
  13. }

用户随机一个端口和代理80建立连接,80又随机了端口又像服务端的80端口重新建立连接,这就出现了端口限制问题,形成了一个瓶颈,80随机的端口最大是65535。

 

代理会代理用户请求重新向后端发起连接请求

1、代理默认会丢弃头部信息,我们需要把参数添加上

c876e263bd5c4f7483fa9a8e2f2bcfc5.png

 aacd7557d5bb499ea950b2c7a1c3813f.png

2、代理默认和后端建立连接方式是短链接HTTP1.0

51b8486e62cb431885521b2a2f4022b4.png

 

 

将10.0.0.5作为代理服务器

1、Nginx安装

  1. [root@LB01 ~]# scp 10.0.0.7:/etc/yum.repos.d/nginx.repo /etc/yum.repos.d/ #配置yum源
  2. The authenticity of host '10.0.0.7 (10.0.0.7)' can't be established.
  3. ECDSA key fingerprint is SHA256:zQvI/tCFYssR7l6cr90EtaIA93FXJp8FmUhGtkZshlA.
  4. ECDSA key fingerprint is MD5:0b:a1:ee:d2:75:92:1a:62:05:63:5e:d1:e8:42:13:84.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added '10.0.0.7' (ECDSA) to the list of known hosts.
  7. root@10.0.0.7's password:
  8. nginx.repo 100% 192 110.6KB/s 00:00
  9. [root@LB01 ~]# yum -y install nginx #安装配置

2、配置Nginx

  1. [root@LB01 ~]# vim /etc/nginx/conf.d/default.conf
  2. server {
  3. listen 80;
  4. server_name blog.koten.com;
  5. location / {
  6. proxy_pass http://10.0.0.7:80; #指定服务端IP,因为Nginx是七层,所以前面必须带http
  7. proxy_set_header Host $http_host; #指定host,携带头部信息
  8. proxy_http_version 1.1; #指定http版本号,长连接
  9. }
  10. }
  11. ~
  12. ~
  13. <nf.d/default.conf" 12L, 105C written
  14. [root@LB01 ~]# nginx -t
  15. nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  16. nginx: configuration file /etc/nginx/nginx.conf test is successful
  17. [root@LB01 ~]# systemctl start nginx
  18. [root@LB01 ~]# systemctl enable nginx
  19. Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
  20. [root@LB01 ~]#

代理信息恢复 

fd5e5fb5be8b4a409fb94ef9235c5017.png

 

代理服务器优化配置(提升客户访问速度)

代理到后端的TCP连接、响应、返回等超时时间

  1. //nginx代理与后端服务器连接超时时间(代理连接超时)
  2. Syntax: proxy_connect_timeout time;
  3. Default: proxy_connect_timeout 60s;
  4. Context: http, server, location
  5. //nginx代理等待后端服务器的响应时间
  6. Syntax: proxy_read_timeout time;
  7. Default: proxy_read_timeout 60s;
  8. Context: http, server, location
  9. //后端服务器数据回传给nginx代理超时时间
  10. Syntax: proxy_send_timeout time;
  11. Default: proxy_send_timeout 60s;
  12. Context: http, server, location

proxy_butter代理缓冲区 

  1. //nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端
  2. Syntax: proxy_buffering on | off;
  3. Default: proxy_buffering on;
  4. Context: http, server, location
  5. //设置nginx代理保存用户请求头信息的缓冲区大小
  6. Syntax: proxy_buffer_size size;
  7. Default: proxy_buffer_size 4k|8k;
  8. Context: http, server, location
  9. //proxy_buffers 具体数据缓冲区
  10. Syntax: proxy_buffers number size;
  11. Default: proxy_buffers 8 4k|8k;
  12. Context: http, server, location

 优化后配置文件

  1. [root@LB01 ~]# vim /etc/nginx/conf.d/default.conf
  2. server {
  3. listen 80;
  4. server_name blog.koten.com;
  5. location / {
  6. proxy_pass http://10.0.0.7:80; #指定服务端地址
  7. proxy_set_header Host $http_host; #指定携带头部信息
  8. proxy_http_version 1.1; #指定http长连接
  9. proxy_connect_timeout 30s; #连接超时时间
  10. proxy_read_timeout 60s; #等待响应时间
  11. proxy_send_timeout 60s; #数据回传等待时间
  12. proxy_buffering on; #缓存区开启
  13. proxy_buffer_size 32k; #缓存头部信息大小
  14. proxy_buffers 4 128k; #缓存数据大小
  15. }
  16. <nf.d/default.conf" 22L, 323C written

 

扩展

/etc/nginx/nginx.conf中的$http_x_forwarded_for记录真实客户端的IP地址

客户端通过10.0.0.5的代理IP访问10.0.0.7,10.0.0.7会记录10.0.0.5的代理IP,我们想记录客户端真实IP,就需要$http_x_forwarded_for,将这个参数在代理中携带上即可

不加时,服务端10.0.0.7末尾是10.0.0.5

84734b25ab05492cb00b8f59a816cef9.png

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 添加到 /etc/nginx/conf.d/default.conf 里面

  1. [root@LB01 ~]# vim /etc/nginx/conf.d/default.conf
  2. server_name blog.koten.com;
  3. location / {
  4. proxy_pass http://10.0.0.7:80;
  5. proxy_set_header Host $http_host; #头部信息
  6. proxy_http_version 1.1; #长连接
  7. proxy_set_header X-Forwarded-For $proxy_add_x_fo
  8. rwarded_for; #记录客户端IP
  9. proxy_connect_timeout 30s; #连接超时时间
  10. proxy_read_timeout 60s; #响应超时时间
  11. proxy_send_timeout 60s; #数据回传超时时间
  12. proxy_buffering on; #开启缓冲区
  13. proxy_buffer_size 32k; #头部信息缓冲区大小
  14. proxy_buffers 4 128k; #数据缓冲区大小
  15. <nf.d/default.conf" 25L, 388C written

e5cdfceade724ace8558e792ad04c61c.png

 发现显示了10.0.0.1的客户端IP


我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/453989
推荐阅读
相关标签
  

闽ICP备14008679号