![](/img/trans.png)
[英]Is it possible to change the HTTP status code returned when proxy_pass gateway is down in nginx?
[英]NGINX proxy_pass change scheme
我有一個從外部地址接收請求的服務,例如https://example.com和一個內部地址,例如 localhost:8080。
我必須使所有針對此服務的請求看起來好像它們將被定向到相同的主機名。
我最初的計划是設置一個 NGINX 反向代理,當在 localhost:8081 聯系時,它與example.com
交換localhost
。
map $http_host $served_host {
default $http_host;
localhost:8081 example.com;
}
server {
listen 8081;
listen [::]:8081;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $served_host;
}
}
這幾乎可行,但該方案仍然是錯誤的。 From outside requests are directed to https://example.com , while from the private network (localhost:8081) they are now directed to http://example.com .
我該如何更改 https -> http 或 http -> Z5E056C500A1D80B6A7110B? 兩者都可以,我只需要完全相同的地址。
我自己想通了,以防有人偶然發現這個問題:
原始請求將X-Forwarded-Proto
header 添加了值https
。 該服務使用它來重建原始地址,包括https://
。 雖然內部沒有這個 header。 所以簡單地添加
proxy_set_header X-Forwarded-Proto https;
代理通行證使內部請求也顯示為使用方案https
。 我不確定這是否是最佳解決方案,但它正在工作:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.