我有3個heroku應用程序
- frontend react
- backend node
- reverse-proxy nginx
- 對reverse-proxy/api/?(.*的調用被轉發到后端
- rest所有對reverse-proxy的呼叫都被轉發到前端
/etc/nginx/conf.d/default.conf
代碼
upstream frontend {
server $FRONTEND_URL;
}
upstream backend {
server $BACKEND_URL;
}
server {
listen $PORT;
location / {
proxy_pass http://frontend;
proxy_set_header Host $FRONTEND_URL;
}
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://backend;
proxy_set_header Host $BACKEND_URL;
}
}
issue
我正在使用cookie進行身份驗證,但后端設置的cookie未被“轉發”
現在它工作了,我做了一些改變:
- 在我的節點應用程序中更改為
secure: false
為我做到了這一點(以后可能會添加tls證書) - @mariolu建議修復
現在看來
location /api {
rewrite /api/(.*) /$1 break;
proxy_pass http://backend;
proxy_set_header Host $BACKEND_URL;
proxy_set_header Cookie $http_cookie;
}
app.set("trust proxy", true);
你需要添加
在位置配置中。變量$http_cookie是用戶請求cookie。