[英]Nginx as reverse proxy, can't get full url in express.js
我無法使用var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
假設我在路由 /getUrl 上調用它
我應該得到 http://<server_ip>/app/users/happy/getUrl
相反,我只得到 http://<server_ip>/getUrl
我使用 Nginx 作為反向代理。
location /app/users/happy/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:4216/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Server $http_host;
proxy_redirect ~/(.*)$ /app/users/happy/$1;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
我還嘗試添加app.set('trust proxy', 'loopback');
代理轉換請求
GET http://<proxy_ip>/app/users/happy/getUrl HTTP/1.1
Host: <proxy_ip>
進入
GET http://<server_ip>/getUrl HTTP/1.1
Host: <server_ip>
X-Forwarded-Host: <server_ip>
X-<other headers mentioned in the nginx directive>
因此前綴/app/users/happy
永遠不會到達您的服務器。 您可以將其添加到另一個 header 中:
proxy_set_header X-Path-Prefix /app/users/happy
並以req.get("X-Path-Prefix")
獲取它,但是您需要它做什么?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.