Linux之代理
代理简介
代理就是将流量平均分配
代理的方式
1、正向代理
找完代理之后,还需要找服务器。
应用:VPN
2、反向代理
只需要找代理,不需要找服务器
应用:负载均衡
Nginx代理服务支持的协议
1、ngx_http_uwsgi_module : Python
2、ngx_http_fastcgi_module : PHP
3、ngx_http_scgi_module : Java
4、ngx_http_v2_module : Golang
5、ngx_http_proxy_module : HTTP
Nginx代理实践
实现用lb01代理web01
1、部署web01
[root@web01 conf.d]# cat game8.conf server { listen 80; server_name 192.168.15.7; location / { root /opt/mali; index index.heml; } location ~ /images{ root /opt/image; } }
2、部署lb01
部署Nginx
# 下载Nginx源代码包 [root@lb01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz # 解压 [root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz # 进入源代码目录 [root@lb01 ~]# cd nginx-1.20.2 # 安装依赖包 [root@lb01 nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel -y # 设置编译参数 [root@lb01 nginx-1.20.2]# ./configure --with-http_gzip_static_module --with-stream --with-http_ssl_module # 编译 [root@lb01 nginx-1.20.2]# make # 安装 [root@lb01 nginx-1.20.2]# make install # 优化 [root@lb01 nginx]# mkdir /etc/nginx [root@lb01 nginx]# mv /usr/local/nginx/conf/* /etc/nginx/ [root@lb01 nginx]# mkdir /etc/nginx/conf.d [root@lb01 nginx]# groupadd www -g 666 [root@lb01 nginx]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin [root@lb01 nginx]# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network-online.target remote-fs.target nss-lookup.target Wants=network-online.target [Service] Type=forking PIDFile=/var/run/nginx.pid ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf ExecReload=/bin/sh -c "/bin/kill -s HUP $(/bin/cat /var/run/nginx.pid)" ExecStop=/bin/sh -c "/bin/kill -s TERM $(/bin/cat /var/run/nginx.pid)" [Install] WantedBy=multi-user.target [root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf [root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/ [root@lb01 sbin]# mkdir /var/log/nginx [root@lb01 sbin]systemctl daemon-reload [root@lb01 sbin]# systemctl start nginx
部署反向代理
[root@lb01 conf.d]# cat /etc/nginx/conf.d/game.conf server { listen 80; server_name _; location / { proxy_pass http://172.16.1.7:80; } }
Nginx代理常用参数
1、添加发往后端服务器的请求信息
Syntax: proxy_set_header field value; Default: proxy_set_header Host $http_host; proxy_set_header Connection close; Context: http, server, location # 用户请求的时候HOST的值是linux.proxy.com, 那么代理服务会像后端传递请求的还是linux.proxy.com proxy_set_header Host $http_host; # 将$remote_addr的值放进变量X-Real-IP中,$remote_addr的值为客户端的ip proxy_set_header X-Real-IP $remote_addr; # 客户端通过代理服务访问后端服务, 后端服务通过该变量会记录真实客户端地址 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2、代理到后端的TCP链接、相应、返回等超时时间
#nginx代理与后端服务器连接超时时间(代理连接超时) Syntax: proxy_connect_timeout time; Default: proxy_connect_timeout 60s; Context: http, server, location #nginx代理等待后端服务器的响应时间 Syntax: proxy_read_timeout time; Default: proxy_read_timeout 60s; Context: http, server, location #后端服务器数据回传给nginx代理超时时间 Syntax: proxy_send_timeout time; Default: proxy_send_timeout 60s; Context: http, server, location proxy_connect_timeout 1s; proxy_read_timeout 3s; proxy_send_timeout 3s;
3、proxy_buffer代理缓冲区
#nignx会把后端返回的内容先放到缓冲区当中,然后再返回给客户端,边收边传, 不是全部接收完再传给客户端 Syntax: proxy_buffering on | off; Default: proxy_buffering on; Context: http, server, location #设置nginx代理保存用户头信息的缓冲区大小 Syntax: proxy_buffer_size size; Default: proxy_buffer_size 4k|8k; Context: http, server, location #proxy_buffers 缓冲区 Syntax: proxy_buffers number size; Default: proxy_buffers 8 4k|8k; Context: http, server, location
4、配置代理优化文件
[root@lb01 ~]# vim /etc/nginx/proxy_params proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 10s; proxy_read_timeout 10s; proxy_send_timeout 10s; proxy_buffering on; proxy_buffer_size 8k; proxy_buffers 8 8k; [root@lb01 conf.d]# cat game.conf server { listen 80; server_name _; location / { proxy_pass http://172.16.1.7:80; include /etc/nginx/proxy_params; } }