本期内容概要
- 代理
- 负载均衡
内容详细
1、代理
1.主要作用: 将流量平均分配 2.代理的方式 01 正向代理 外部想要访问服务器 先找代理 找到之后还需要找服务器 应用:VPN 02 反向代理 外部想要访问服务器 只需要找代理 不需要找服务器 应用:负载均衡
1.1、Nginx代理服务支持的协议
ngx_http_uwsgi_module : Python协议 ngx_http_fastcgi_module : PHP 协议 ngx_http_scgi_module : Java协议 ngx_http_v2_module : Golang协议 ngx_http_proxy_module : HTTP协议
1.2、Nginx代理实践
1.2.1、部署web01服务器
# 部署web01 [root@web01 ~]# cd /etc/nginx/conf.d 将所有 .conf 文件先打包 [root@web01 conf.d]# vim game5.conf server { listen 80; server_name 192.168.15.7; location / { root /opt/Super_Marie; index index.html; } } [root@web01 ~]# systemctl restart nginx 浏览器访问 192.168.15.7 正常载入游戏就是正常部署web01
1.2.2、部署lb01服务器
# 部署Nginx(编译安装 不能yum安装 否则负载均衡可能无法使用) 1.下载Nginx源代码包 [root@lb01 ~]# wget https://nginx.org/download/nginx-1.20.2.tar.gz 2.解压 [root@lb01 ~]# tar -xf nginx-1.20.2.tar.gz 3.进入源代码目录 [root@lb01 ~]# cd nginx-1.20.2 4.安装依赖包 [root@lb01 nginx-1.20.2]# yum install openssl openssl-devel zlib zlib-devel -y 5.设置编译参数 [root@lb01 nginx-1.20.2]# ./configure --with-http_gzip_static_module --with-stream --with-http_ssl_module 6.编译 [root@lb01 nginx-1.20.2]# make 7.安装 [root@lb01 nginx-1.20.2]# make install 8.优化 [root@lb01 nginx-1.20.2]# mkdir /etc/nginx [root@lb01 nginx-1.20.2]# mv /usr/local/nginx/conf/* /etc/nginx/ [root@lb01 nginx-1.20.2]# mkdir /etc/nginx/conf.d [root@lb01 nginx-1.20.2]# cd /etc/nginx/conf.d [root@lb01 conf.d]# cd .. [root@lb01 nginx]# >nginx.conf [root@lb01 nginx]# vim nginx.conf 内容可以从web中复制 [root@web01 nginx]# cat /etc/nginx/nginx.conf 将所有内容写入 [root@lb01 nginx]# vim nginx.conf 但是 user nginx; 要改为 user www; [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 写入内容可以从web中复制 [root@web01 nginx]# cat /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 nginx]# cd /usr/local/nginx/sbin [root@lb01 sbin]# mv /usr/local/nginx/sbin/nginx /usr/sbin/ [root@lb01 sbin]# ln -s /etc/nginx/nginx.conf /usr/local/nginx/conf/nginx.conf [root@lb01 sbin]# mkdir /var/log/nginx [root@lb01 sbin]# systemctl start nginx [root@lb01 sbin]# nginx -t -------------------------------------------------------------------------------------------------------------- # 部署反向代理 [root@lb01 sbin]# cd /etc/nginx/conf.d [root@lb01 conf.d]# vim game.conf server { listen 80; server_name _; location / { proxy_pass http://172.16.1.7:80; } } [root@lb01 conf.d]# systemctl restart nginx
1.3、Nginx代理常用参数
1.3.1、添加发往后端服务器的请求头信息
Syntax: proxy_set_header field value; Default: proxy_set_header Host $http_host; proxy_set_header Connection close; Context: http, server, location [root@lb01 conf.d]# cd /etc/nginx/conf.d [root@lb01 conf.d]# vim game.conf 在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