Nginx反向代理、负载均衡、ip_hash实现
一、Nginx反向代理实现需要三台虚拟机:虚拟机ip作用server1172.25.63.1反向代理服务器server2172.25.63.2后端服务器server3172.25.63.3后端服务器1.为nginx制作软链接ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/2.修改配置文...
·
一、Nginx反向代理实现
需要三台虚拟机:
虚拟机 | ip | 作用 |
---|---|---|
server1 | 172.25.63.1 | 反向代理服务器 |
server2 | 172.25.63.2 | 后端服务器 |
server3 | 172.25.63.3 | 后端服务器 |
1.为nginx制作软链接
ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
2.修改配置文件
[root@server1 nginx]# cd conf/
[root@server1 conf]# vim nginx.conf
修改:
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 server 172.25.63.3:80; #后端服务器ip及端口
17
18 }
.......
55 server {
56 listen 80;
57 server_name www.westos.org;
58 location / {
59 proxy_pass http://westos;
60 }
61 }
修改完成后使用以下命令检查是否有错误:
[root@server1 conf]# 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
打开(nginx没有打开时)或重新加载(nginx打开时):
[root@server1 conf]# nginx #打开nginx
[root@server1 conf]# nginx -s reload #重新加载nginx
3 配置后端服务器
两台虚拟机均安装httpd并写入测试页面:
[root@server2 ~]# vim /var/www/html/index.html
[root@server2 ~]# cat /var/www/html/index.html
server2
[root@server2 ~]# systemctl start httpd
[root@server3 ~]# vim /var/www/html/index.html
[root@server3 ~]# cat /var/www/html/index.html
server3
[root@server3 ~]# systemctl start httpd
4.测试
在物理机测试:
[root@foundation63 ~]# curl www.westos.org
server3 #反向代理成功
[root@foundation63 ~]# curl www.westos.org
server3
注意:此步需要在物理机做解析
[root@foundation63 ~]# cat /etc/hosts
172.25.63.1 www.westos.org
二、Nginx负载均衡实现
此实验与一中实验基本相同,为以不同的点就是配置文件:
在server1:
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 server 172.25.63.2:80;
17 server 172.25.63.3:80;
18
19 }
.......
56 server {
57 listen 80;
58 server_name www.westos.org;
59 location / {
60 proxy_pass http://westos;
61 }
62 }
-t 检查没有错误后重新加载:
[root@server1 conf]# 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@server1 conf]# nginx -s reload
测试:
在物理机
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3
[root@foundation63 ~]# curl www.westos.org
server2
[root@foundation63 ~]# curl www.westos.org
server3
nginx同样对后端的服务器有故障检测
当server2 的服务宕机后:
[root@server2 ~]# systemctl stop httpd
再进行测试:
当server2重新上线后:
三、使一个ip地址只能访问一个服务器
有时需要使一个ip地址只能访问一个服务器而不是在两台服务器之间切换,此时需要在server1的配置文件作如下更改:
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 ip_hash;
17 server 172.25.63.2:80;
18 server 172.25.63.3:80;
19
20 }
........
[root@server1 conf]# 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@server1 conf]# nginx -s reload
测试:
四、将server1作为server3的备用服务器
1.编辑nginx配置文件
在server1:
[root@server1 conf]# vim nginx.conf
12 http {
13 include mime.types;
14 default_type application/octet-stream;
15 upstream westos {
16 #ip_hash;
17 server 127.0.0.1:80 backup;
18 server 172.25.63.3:80;
19
20 }
2.编辑nginx测试页面
在server1:
[root@server1 nginx]# cd html/
[root@server1 html]# echo server1 > index.html
[root@server1 html]# cat index.html
server1
之后重新加载:
[root@server1 html]# 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@server1 html]# nginx -s reload
3.测试
在物理机:
更多推荐
已为社区贡献1条内容
所有评论(0)