Cat-God-007

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

Nginx服务的搭建与配置(二)----Nginx的反向代理

Nginx的反向代理

Nginx服务的搭建与配置(—)

web网站使用反向代理,除了可以防止外网对内网服务器的恶性攻击、缓存以减少服务器的压力和访问安全控制之外,
还可以进行负载均衡,将用户请求分配给多个服务器。

1.删除残留文件(没有,就跳过)

cd /etc/nginx/conf.d
rm -rf default.conf.rpmsave

2.修改配置文件,配置反向代理。

vi default.conf
#添加如下内容:
server {  
        listen       80;  
        server_name  www.007.com;  
        access_log      /var/log/nginx/host.access.log   main;

        location / {  
            proxy_pass   http://test;           //反向代理服务器的地址,也可以是IP地址。
            index            index.html index.htm index.php;  
                         }       
            }  
upstream test1 {                                                                # 后端web服务器
    server 192.168.205.149:8080  weight=1;                     # 默认端口80,默认权重为1.
    }  
upstream test2 {  
    server192.168.205.150:8080;  
    }  
server {  
        listen       80;  
        server_name  cat.com;  
        access_log  /var/logs/nginx/cat.access.log  main;   # 自己创文件夹及文件哦!
  
        location / {  
            proxy_pass   http://test1;                                     # 反向代理服务器的地址
            index  index.html index.htm index.php;  
        }       
    }  
server {  
        listen       80;  
        server_name  god.com;  
        access_log  /var/logs/nginx/god.access.log   main;  
  
        location / {  
            proxy_pass   http://test2;  
            index  index.html index.htm index.php;  
        }          
    } 
保存,退出

3.重启nginx

systemctl restart nginx

4.数据访问流程

使用浏览器访问http://www.cat.com,由于nginx反向代理接受客户机请求,找到server_name为http://www.cat.com的server节点, 根据proxy_pass对应的http路径,将请求转发到upstream test上,即ip为192.168.205.149的服务器。

感谢大家,点赞,收藏,关注,评论!

posted on   成果和地方  阅读(32)  评论(0编辑  收藏  举报  

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示