> 文章列表 > 【Nginx】负载均衡和反向代理配置

【Nginx】负载均衡和反向代理配置

【Nginx】负载均衡和反向代理配置

环境说明:

Centos 7版本

主机名

IP地址

nginx

192.168.10.150

nginx-2

192.168.10.151

nginx-3

192.168.10.152

nginx-4

192.168.10.153

准备Nginx的页面内容,方便观察

#nginx-2、Nginx-3和Nginx-4的Nginx的页面内容
[root@nginx-2 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.151
[root@nginx-3 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.152
[root@nginx-4 ~]# vim /usr/local/nginx/html/index.html 
This is 192.168.10.153

一、反向代理配置

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  localhost;location / {
#proxy_pass就会把请求转发proxy_pass http://192.168.10.151;
#有proxy_pass,就不会访问下面的root#root   html;#index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
[root@nginx ~]# systemctl restart nginx

#验证

【Nginx】负载均衡和反向代理配置

如果改为proxy_pass http://www.baidu.com;结果如下:

【Nginx】负载均衡和反向代理配置

#清楚浏览器缓存,并且改为baidu.com形式,访问的是www.bai.com,也会有重定向,访问百度时,可能是因为https模式,就会有重定向,如果只是改百度的ip就会是http模式,结果如下:

【Nginx】负载均衡和反向代理配置

二、负载均衡配置

  1. 基于轮询

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;
#upstream用于配置负载均衡后端服务器的地址,合成一个组,会有一个名称,proxy_pass就会把请求转发到这个组upstream backend {server 192.168.10.151:80;server 192.168.10.152:80;
}    server {listen       80;server_name  localhost;location / {proxy_pass http://backend;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
[root@nginx ~]# systemctl restart nginx

#验证

#不断刷新地址

【Nginx】负载均衡和反向代理配置
  1. 基于权重

#在轮询的基础上加上weight设置就行

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;upstream backend {server 192.168.10.151:80 weight=8;server 192.168.10.152:80 weight=6;server 192.168.10.153:80 weight=4;
}    server {listen       80;server_name  localhost;location / {proxy_pass http://backend;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
[root@nginx ~]# systemctl restart nginx

#验证

【Nginx】负载均衡和反向代理配置

down参数

#表示服务器下线,不再参与

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;upstream backend {server 192.168.10.151:80 weight=8 down;server 192.168.10.152:80 weight=6;server 192.168.10.153:80 weight=4;
}    server {listen       80;server_name  localhost;location / {proxy_pass http://backend;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
[root@nginx ~]# systemctl restart nginx

#验证

【Nginx】负载均衡和反向代理配置

backup参数

#标记一个服务器为备用服务器,当主服务器不可用时,Nginx会自动将请求到备用服务器上

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {worker_connections  1024;
}
http {include       mime.types;default_type  application/octet-stream;sendfile        on;keepalive_timeout  65;upstream backend {server 192.168.10.151:80 weight=8 down;server 192.168.10.152:80 weight=6;server 192.168.10.153:80 weight=4 backup;
}    server {listen       80;server_name  localhost;location / {proxy_pass http://backend;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}
}
[root@nginx ~]# systemctl restart nginx

#验证

【Nginx】负载均衡和反向代理配置