问题需求:我需要反代理下载阿里源的东西。
在nginx的server 配置如下:
server {
listen 80;
server_name 自己的服务器的ip地址;
location /centos {
proxy_pass http://mirrors.aliyun.com;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
}
}
发现通过自己的服务器去请求一直报错403,后面发现是因为proxy_set_header Host $http_host; 这一行的问题,需要把http_host 改为proxy_host。
原因说明:当Host设置为$http_host时,则不改变请求头的值,所以当要转发到bbb.example.com的时候,请求头还是aaa.example.com的Host信息,就会有问题;当Host设置为$proxy_host时,则会重新设置请求头为bbb.example.com的Host信息。