代理服务配置与负载均衡

代理

主要作用:
将流量平均分配
代理的方式
	01 正向代理
		外部想要访问服务器 先找代理 找到之后还需要找服务器
		应用:VPN

	02 反向代理
		外部想要访问服务器 只需要找代理 不需要找服务器
		应用:负载均衡

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协议
Nginx代理实践

部署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

部署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
Nginx代理常用参数

添加发往后端服务器的请求头信息

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_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

代理到后端的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


	[root@lb01 conf.d]# vim game.conf
	在location / {}内 写入以下内容:
        proxy_connect_timeout 1s;
        proxy_read_timeout 3s;
        proxy_send_timeout 3s;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx 

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

	[root@lb01 conf.d]# vim game.conf
	在location / {}内 写入以下内容:
        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 8 8k;

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

配置代理优化文件

	[root@lb01 conf.d]# 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]# vim game.conf
	只保留并修改为以下内容即可:
server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://172.16.1.7:80;
        include /etc/nginx/proxy_params;
    }
}
	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

负载均衡

# 为什么要用负载均衡?
	反向代理 只会将外部请求转发给某一台服务器
	负载均衡 可以实现将外部请求转发给多台服务器

负载均衡的架构

通过代理将流量按照一定的比例,转发到后端

负载均衡的实现

实现
'''将后端服务打包成一个IP连接池'''

1.反向代理
server {
   	listen 80;
   	server_name _;
   	location / {
        proxy_pass http://[连接池];
   	}
}

2.IP连接池
upstream [连接池名称] {
    server [ip]:[port];
    server [ip]:[port];
    server [ip]:[port];
}
# 实现客户请求转发至三台web服务器

01 准备文件(注意在不同的服务器操作)
	[root@web01 opt]# cd /opt
	[root@web01 opt]# tar -czvf Super_Marie.tar.gz Super_Marie
	[root@web01 opt]# scp Super_Marie.tar.gz 172.16.1.8:/opt/
	[root@web01 opt]# scp Super_Marie.tar.gz 172.16.1.9:/opt/

	[root@web02 opt]# tar -xf Super_Marie.tar.gz

	[root@web03 opt]# tar -xf Super_Marie.tar.gz

	没有安装nginx的 执行安装
	[root@web02 opt]# yum install nginx -y
	[root@web03 opt]# yum install nginx -y

	在web01中
	[root@web01 conf.d]# cd /etc/nginx/conf.d
	[root@web01 conf.d]# scp game5.conf 172.16.1.8:/etc/nginx/conf.d/
	[root@web01 conf.d]# scp game5.conf 172.16.1.9:/etc/nginx/conf.d/

	在web02 和 web03中修改配置文件 保持一致
	[root@web03 ~]# cd /etc/nginx
	[root@web03 nginx]# vim nginx.conf
	[root@web02 ~]# cd /etc/nginx
	[root@web02 nginx]# vim nginx.conf
	将 :user nginx;
	改为 :user www;

	[root@web02 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web02 nginx]# systemctl restart nginx

	[root@web03 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web03 nginx]# systemctl restart nginx

02 测试
	在浏览器分别输入 
	192.168.15.7 | 192.168.15.8 | 192.168.15.9
	正常访问到游戏 则正常

	如果有显示页面为 前期测试的考试系统页面 则执行:
	systemctl disable --now httpd
	systemctl restart nginx

	到此 后端服务器三台都部署完成

03 编辑IP连接池
	[root@lb01 conf.d]# vim game.conf
	upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}

	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

04 通过监控日志测试
	[root@web01 conf.d]# tail -f /var/log/nginx/access.log
	[root@web02 nginx]# tail -f /var/log/nginx/access.log
	[root@web03 nginx]# tail -f /var/log/nginx/access.log

	在浏览器访问 192.168.15.5
	在三个web服务器都显示日志 则代表实现了负载均衡

'''
监控日志:
	tail -f /var/log/nginx/access.log
'''

负载均衡的比例

# 1.轮询
	默认情况下,Nginx负载均衡就是轮询状态
	就是将客户请求循环发送至每一台服务器(类似于平均)

	[root@web01 ~]# cd /opt/Super_Marie
	[root@web02 ~]# cd /opt/Super_Marie
	[root@web03 ~]# cd /opt/Super_Marie

	[root@web01 Super_Marie]# echo web01 > web.html
	[root@web02 Super_Marie]# echo web02 > web.html
	[root@web03 Super_Marie]# echo web03 > web.html

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}


	浏览器访问:192.168.15.5/web.html
	循环显示 web01 web02 web03 就正常
# 2.权重
	Nginx中的权重0-100,数字越大,权重越高
	weight数字越大 代表负载均衡会更大比例的将客户请求转发到对应的服务器

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 weight=9;
    server 172.16.1.8:80 weight=5;
    server 172.16.1.9:80 weight=1;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}
	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

	浏览器访问:192.168.15.5/web.html
	刷新十次 6次显示 web01
	3次 显示 web02
	1次 显示 web03
# 3.ip_hash
	每一个IP固定访问某一个后端
	当有一个IP访问一台服务器时 该IP无论刷新多少次 都只会访问该服务器 其他IP只能访问其他服务器 

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
    ip_hash;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx



	浏览器访问:192.168.15.5/web.html
	如果首次显示 web01
	那么永远都是 web01
	每一个IP固定访问某一个后端

负载均衡后端状态

在这里插入图片描述

# 1.down状态
	暂时不分配流量

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 down;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx


	浏览器访问:192.168.15.5/web.html
	永远都不会显示 web01 因为已经暂停流量转发给 web01了
# 2.backup状态
	只有当(没有backup状态的)所有的机器全部宕机,才能启动

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 backup;
    server 172.16.1.8:80;
    server 172.16.1.9:80;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx


	浏览器访问:192.168.15.5/web.html
	在 web02 web03 正常启动时
	永远都不会显示 web01 
	在其余所有服务器宕机时 
	此时客户请求就只会访问 web01
# 3.max_fails、fail_timeout(要连用)
	要与 proxy_next_upstream:后端错误标识 连用

	保持配置文件中 存在以下内容
	[root@lb01 conf.d]# vim game.conf
upstream supermarie {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_pass http://supermarie;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}


	[root@lb01 conf.d]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
	[root@lb01 conf.d]# systemctl restart nginx

	浏览器访问:192.168.15.5/web.html
	可以尝试重启 web01 | 置空 web.html
	就只会显示 web02 web03
'''
proxy_next_upstream 可以跟的参数:

proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;

error   :	与服务器建立连接,向其传递请求或读取响应头时发生错误;
timeout :	在与服务器建立连接,向其传递请求或读取响应头时发生超时;
invalid_header :	服务器返回空的或无效的响应;
http_500 :	服务器返回代码为500的响应;
http_502 :	服务器返回代码为502的响应;
http_503 :	服务器返回代码为503的响应;
http_504 :	服务器返回代码504的响应;
http_403 :	服务器返回代码为403的响应;
http_404 :	服务器返回代码为404的响应;
http_429 :	服务器返回代码为429的响应(1.11.13);

non_idempotent :	通常,请求与 非幂等 方法(POST,LOCK,PATCH)不传递到请求是否已被发送到上游服务器(1.9.13)的下一个服务器; 启用此选项显式允许重试此类请求;

off :	禁用将请求传递给下一个服务器。
'''
负载均衡部署BBS

部署后端服务

# 部署Python
	1.创建用户
	[root@web02 ~]# groupadd django -g 888
	[root@web02 ~]# useradd django -u 888 -g 888 -r -M -s /bin/sh
    
	[root@web03 ~]# groupadd django -g 888
	[root@web03 ~]# useradd django -u 888 -g 888 -r -M -s /bin/sh
    
    2.安装依赖软件
	[root@web02 ~]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
	[root@web03 ~]# yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
# 部署Django和uwsgi 
	3.安装Django和uwsgi
	[root@web01 ~]# scp bbs.zip 172.16.1.8:/opt/
	[root@web01 ~]# scp bbs.zip 172.16.1.9:/opt/
    
	[root@web02 ~]# pip3 install django==1.11
	[root@web02 ~]# pip3 install uwsgi
	[root@web02 ~]# pip3 install pymysql
    
	[root@web03 ~]# pip3 install django==1.11
	[root@web03 ~]# pip3 install uwsgi
	[root@web03 ~]# pip3 install pymysql
    
	4.创建项目
	[root@web02 ~]# cd /opt/
	[root@web02 opt]# unzip bbs.zip
	[root@web02 opt]# cd bbs
	[root@web02 bbs]# vim bbs/settings.py
	修改下面两处
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}


	[root@web03 ~]# cd /opt/
	[root@web03 opt]# unzip bbs.zip
	[root@web03 opt]# cd bbs
	[root@web03 bbs]# vim bbs/settings.py
	修改下面两处
ALLOWED_HOSTS = ['*']
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'bbs',
        'USER': 'root',
        'PASSWORD': '123456',
        'HOST': '172.16.1.61',
        'PORT': 3306,
        'CHARSET': 'utf8'
    }
}

# 启动测试
[root@web02 bbs]# python3 manage.py runserver 0.0.0.0:8000
[root@web03 bbs]# python3 manage.py runserver 0.0.0.0:8000


# 配置并启动
	5.编辑项目配置文件
	[root@web02 bbs]# vim /opt/bbs/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/bbs
# wsgi文件路径
wsgi-file       = bbs/wsgi.py
# 模块wsgi路径
module          = bbs.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true
    
    
	[root@web03 bbs]# vim /opt/bbs/myweb_uwsgi.ini
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/bbs
# wsgi文件路径
wsgi-file       = bbs/wsgi.py
# 模块wsgi路径
module          = bbs.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true
    

    
	6.启动uwsgi
	测试
	[root@web02 bbs]# uwsgi --ini myweb_uwsgi.ini --uid 666
	启动
	[root@web02 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
[uWSGI] getting INI configuration from myweb_uwsgi.ini


	[root@web03 bbs]# uwsgi --ini myweb_uwsgi.ini --uid 666
	启动
	[root@web02 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
[uWSGI] getting INI configuration from myweb_uwsgi.ini

-d    :	以守护进程方式运行
--ini :	指定配置文件路径
--uid :	指定uid
    
    
	7.编辑Nginx配置文件
	[root@web01 conf.d]# vim /etc/nginx/conf.d/python.conf
	[root@web02 bbs]# vim /etc/nginx/conf.d/python.conf
	[root@web03 bbs]# vim /etc/nginx/conf.d/python.conf
	写入以下内容:
server {
    listen 80;
    server_name py.test.com;
    location / { 
        include uwsgi_params;
        uwsgi_pass 127.0.0.1:8000;
        uwsgi_read_timeout 2;
        uwsgi_param UWSGI_SCRIPT bbs.wsgi;
        uwsgi_param UWSGI_CHDIR /opt/bbs;
        index  index.html index.htm;
        client_max_body_size 35m;
    }
}
    
	8.重启Nginx配置
	[root@web01 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web01 conf.d]# systemctl restart nginx
    
    
    [root@web02 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web02 conf.d]# systemctl restart nginx

    
	[root@web03 conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
	[root@web03 conf.d]# systemctl restart nginx

部署负载均衡

[root@lb01 conf.d]# cd /etc/nginx/conf.d
[root@lb01 conf.d]# cp game.conf python.conf


[root@lb01 conf.d]# vim python.conf
upstream bbs {
    server 172.16.1.7:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.8:80 max_fails=3 fail_timeout=3s;
    server 172.16.1.9:80 max_fails=3 fail_timeout=3s;
}

server {
    listen 80;
    server_name py.test.com;
    location / {
        proxy_pass http://bbs;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_404; 
        include /etc/nginx/proxy_params;
    }
}

浏览器连接 py.test.com 如下图就正常了


'''
如果重启服务器 
要在web中重启配置
[root@web02 ~]# cd /opt/bbs
[root@web02 bbs]# uwsgi -d --ini myweb_uwsgi.ini --uid 666
[root@web02 bbs]# systemctl restart nginx
db中重启数据库
[root@db01 ~]# systemctl restart mariadb
'''

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
***反向代理配置https双机负载均衡的问题,您可以按照以下步骤进行配置: 1. 安装nginx:首先确保已经在服务器上安装了nginx。您可以使用适用于您操作系统的包管理器进行安装,或者从nginx官方网站下载并手动安装。 2. 生成SSL证书:为了启用HTTPS,您需要为每个后端服务器生成SSL证书。您可以使用公开的CA(证书颁发机构)签名证书,或者使用自签名证书。这里以自签名证书为例,在服务器上执行以下命令: ```shell openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /path/to/private.key -out /path/to/certificate.crt ``` 这将生成一个私钥文件 `private.key` 和一个证书文件 `certificate.crt`。 3. 配置nginx:打开 nginx 的配置文件 `nginx.conf`,一般位于 `/etc/nginx/nginx.conf` 或 `/usr/local/nginx/conf/nginx.conf`。确保以下配置已添加或修改: ```nginx http { # ... upstream backend { server backend1.example.com; server backend2.example.com; } server { listen 443 ssl; server_name yourdomain.com; ssl_certificate /path/to/certificate.crt; ssl_certificate_key /path/to/private.key; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 可选:添加其他的代理设置 # ... } } } ``` 在上述配置中,`backend1.example.com` 和 `backend2.example.com` 是您的后端服务器的域名或IP地址,您可以根据实际情况进行修改。 4. 重启nginx:保存并关闭 `nginx.conf` 文件。然后使用以下命令重启nginx服务: ```shell sudo service nginx restart ``` 或者 ```shell sudo systemctl restart nginx ``` 这样就完成了nginx反向代理配置https双机负载均衡。现在,当用户访问 `yourdomain.com` 时,nginx将会根据负载均衡算法将请求转发到后端服务器,并通过HTTPS进行加密传输。 请注意,上述示例中的配置仅供参考,请根据您的实际需求进行适当的修改。同时,确保您的后端服务器已经正确配置和运行,并监听适当的端口。 希望以上信息对您有所帮助!如有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值