問題需求:我需要反代理下載阿里源的東西。
在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信息。