nginx实现反向代理

反向代理服务器:代理服务器对WEB用户的访问!也就是说它是真真服务器的“脸”!

当我们需要隐藏一台服务器真真放在哪个国家时,可以这样做:

代理服务器放在中国,真真的服务器可以放在任何一个地方,而用户只知道你的代理服务器在中国,通过设置代理服务器去访问真真的服务器!

具体实现如下

1.基础工作

  安装linux,nginx,php/.net/....

2.模拟环境

 IP为192.168.1.219的机器设置三个站点,通过端口访问

192.168.1.219
192.168.1.219:82
192.168.1.219 :83
3.实现

在nginx.conf文件里 添加 include 网站配置文件的路径

假设 192.168.1.219
配置文件在/etc/nginx/sites-enabled/default
nginx.conf添加 include /etc/nginx/sites-enabled/*;

192.168.1.219:82
配置文件在/etc/nginx/sites-2/default
nginx.conf添加 include /etc/nginx/sites-2/*;

192.168.1.219:83   将重定向为http://www.163.com/
配置文件在/etc/nginx/sites-3/default
nginx.conf添加 include /etc/nginx/sites-3/*;

192.168.1.219,192.168.1.219:82配置文件结构如下
server {
        listen       80;//或82
        server_name  192.168.1.219;
        charset utf-8;
        location / {
            root   /var/www/nginx/sites-enabled/; //对应的站点目录
            index  index.html index.htm;          
        }
}

192.168.1.219:83配置文件结构如下
server {
        listen   83;
        server_name  192.168.1.219:83;

        access_log  /var/log/nginx/localhost.access.log;

 location / {
             proxy_pass http://www.163.com/; //将192.168.1.219:83转向为163服务器
          }

          location ^~ /test/ {
             proxy_pass  http://192.168.1.219:82/; //将192.168.1.219:83/test转向82
          }
          location ~ \.php$ {
             proxy_pass  http://192.168.1.219;//将192.168.1.219:83/xxx.php转向80
           }
}

原文地址:https://www.cnblogs.com/bobsoft/p/2714554.html