欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx实现反向代理和负载均衡

程序员文章站 2022-07-12 19:17:47
...

1. upstream +proxy_pass 模块实现反向代理

http {
    include       mime.types;
    default_type  application/octet-stream;
        upstream westos {
                server 172.25.254.2:80;

}
server {
        listen 80;
        server_name www.westos.org;
        location / {
                proxy_pass http://westos;
}

}
[aaa@qq.com sbin]# ./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
[aaa@qq.com sbin]# ./nginx -s reload
[aaa@qq.com sbin]# 

可以做一下链接,方便用命令

[aaa@qq.com sbin]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[aaa@qq.com sbin]#

2. 打开后端服务器

[aaa@qq.com ~]# 
[aaa@qq.com ~]# cd /var/www/html/
[aaa@qq.com html]# vim index.html
[aaa@qq.com html]# systemctl start httpd
[aaa@qq.com html]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2020-02-25 18:12:51 CST; 6s ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 11735 (httpd)
   Status: "Processing requests..."
   CGroup: /system.slice/httpd.service

nginx实现反向代理和负载均衡

测试:
做好解析

[aaa@qq.com kiosk]# vim /etc/hosts
[aaa@qq.com kiosk]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.254.1   www.westos.org  westos.org  bbs.westos.org 

[aaa@qq.com kiosk]# 
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# 

nginx实现反向代理和负载均衡

3. 配置多后端(实现负载均衡)

http {
    include       mime.types;
    default_type  application/octet-stream;
        upstream westos {
                server 172.25.254.2:80;
                server 172.25.254.3:80;
}
[aaa@qq.com sbin]# 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
[aaa@qq.com sbin]# nginx -s reload
[aaa@qq.com sbin]# 
[aaa@qq.com ~]# vim /var/www/html/index.html
[aaa@qq.com ~]# cat /var/www/html/index.html 
i am server3
[aaa@qq.com ~]# systemctl start httpd
[aaa@qq.com ~]# 

测试:

[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# 

nginx实现反向代理和负载均衡

对后端服务器有健康检查机制
关闭后端服务器server2

[aaa@qq.com html]# systemctl stop httpd
[aaa@qq.com html]# 

重新测试

[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# curl www.westos.org
i am server3
[aaa@qq.com kiosk]# 

nginx实现反向代理和负载均衡

4.同一个ip的请求调向固定的后端服务器

http {
    include       mime.types;
    default_type  application/octet-stream;
        upstream westos {
                ip_hash;
                server 172.25.254.2:80;
                server 172.25.254.3:80;
}

测试:

[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# curl www.westos.org
i am server2
[aaa@qq.com kiosk]# 

可见,将真机172.25.254.8的访问请求固定到了server2这个后端服务器上。
nginx实现反向代理和负载均衡

5. nginx本机作为后端的backup

http {
    include       mime.types;
    default_type  application/octet-stream;
        upstream westos {
                server 172.25.254.2:80;
                server 127.0.0.1:80 backup;
}
[aaa@qq.com sbin]# cd ../conf/
[aaa@qq.com conf]# vim nginx.conf
[aaa@qq.com conf]# vim ../html/index.html 
[aaa@qq.com 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
[aaa@qq.com conf]# nginx -s reload
[aaa@qq.com conf]# cat ../html/index.html 
i am server2_backup
[aaa@qq.com conf]# 

nginx实现反向代理和负载均衡测试:
当后端服务器server2 正常工作时
nginx实现反向代理和负载均衡
当server2出现故障的时候,nginx作为后端服务的backup顶替其工作

[aaa@qq.com kiosk]# curl www.westos.org
i am server2_backup
[aaa@qq.com kiosk]# curl www.westos.org
i am server2_backup
[aaa@qq.com kiosk]# curl www.westos.org
i am server2_backup
[aaa@qq.com kiosk]# 

nginx实现反向代理和负载均衡

相关标签: linux企业实战