nginx正向代理 (带着请求头)

当你获得云服务器之后,

你有这样一个需求:当你要访问一个url的时候,这个URL只能在人家的云服务器上访问(比如百度),所以你要买百度的BCC,你可能在想在BCC起服务,那样有点麻烦,直接使用nginx代理就可以解决问题了,因为url涉及到验证,所以要把请求头带上。

首先下载nginx

         apt-get install nginx

最后配置nginx配置文件就可以啦!

 配置文件一般在: /etc/nginx/sites-enabled/

快速查找配置文件:

        命令:locate nginx.conf

配置文件内容如下:

server{

    listen 80;

   server_name  xxxxxxxxxxx.com;   # 你自己的域名

    location / {

             proxy_pass   http://xxxxxxxxxx.com;  # 在目标url

             proxy_rediect off;

             # 把你需要带的请求头都带上              

             proxy_set_header Host $http_host;
             proxy_set_header accept-encodeing 'gzip, deflate';

             proxy_set_header content-type 'application/json';

             proxy_set_header x-bce-date $http_x_bce_date;  # 动态的值 请求头就会带过来
             proxy_set_header authorization $http_authorization;
             proxy_set_header accept '*/*';
         }

}

重启nginx:

    sudo /etc/init.d/nginx reload

这样 你直接访问你的域名 实际上访问的就是你的目标url了,验证也在头里面了。

如果验证是放在url里那就没这么麻烦了,不用配置哪些头信息,把头信息去掉,

 location / {

             proxy_pass   http://xxxxxxxxxx.com;  # 在目标url

             proxy_rediect off;

         }

这样就OK啦!记得重启nginx!
原文地址:https://www.cnblogs.com/niehaidong111/p/9933377.html