nginx正反代理

nginx正向代理

配置访问权限使得172.16.121.217只能被172.16.121.216访问
修改/etc/nginx/nginx.conf配置文件

location / {
if ( $remote_addr !~* "^172\.16\.121\.216") {
        return 403;
        }
}

测试访问
在这里插入图片描述

修改172.16.121.216配置文件 配置216正向代理172.16.121.217

listen 81;
resolver 8.8.8.8;
location / {
        proxy_pass http://172.16.121.217:81$request_uri;
        }
}

修改windows internet属性
在这里插入图片描述

测试使用216访问可以看到217的网页
在这里插入图片描述

nginx反向代理

在172.16.121.217虚拟机上配置反向代理172.16.121.216:8080
在nginx.conf配置文件中加入反向代理

server
{       
listen 8080;
server_name mail.test.com;
default_type ‘text/html’;
charset utf-8;
index index.php index.html index.htm;
root /usr/local/nginx/html/;
location ~ / {
        proxy_pass http://172.16.121.216:8080$request_uri;
}
}
    这里要将之前的正向代理限制访问先注释掉
        #       location / {
#        if ( $remote_addr !~* "^172\.16\.121\.216") {
#                return 403;
#                }
#       }

查看端口

[root@wjh ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2406/nginx: master  
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      2406/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1104/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1660/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1104/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1660/master       

在172.16.121.216中的nginx.conf配置文件中加入8080端口

 server {
        root /usr/local/nginx/html/;
        index index.html index.html;
        listen 8080;
}

查看一下端口

[root@wjh ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      2371/nginx: master  
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      2371/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1098/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1498/master         
tcp6       0      0 :::22                   :::*                    LISTEN      1098/sshd           
tcp6       0      0 ::1:25                  :::*                  

测试217跳转是否成功
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值