使用 NGINX 作为反向代理的 HTTP 错误 body 为空
HTTP error body empty using NGINX as reverse proxy
NGINX 代理我的 Express NodeJS 应用。
虽然在我的开发环境中我正确地得到错误代码和错误响应,但在生产环境中我只得到没有错误响应的错误代码。
但是,当我 运行 PowerShell 中的失败请求时,我确实收到错误响应。
在生产中,API 和应用程序位于不同的子域中:
- "api.domain.com" // Express 后端
- "app.domain.com" // 使用 axios
的 React 应用
在开发中,所有内容都托管在同一域中。
HTTP/2XX 请求按预期工作。
请在下面找到生产 NGINX conf(来自 available-sites):
server {
server_name api.domain.com;
location / {
if ($request_method = OPTIONS ) {
add_header Content-Length 0;
add_header Content-Type text/plain;
add_header 'Access-Control-Allow-Origin' 'https://app.domain.com' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept, Origin';
return 204;
}
add_header 'Access-Control-Allow-Origin' 'https://app.domain.com' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept, Origin';
proxy_pass http://localhost:3008;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = api.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name api.domain.com;
return 404; # managed by Certbot
}
我已经测试了各种 header 配置但均未成功,如有任何帮助,我们将不胜感激。
我缺少以下 NGINX header 的“始终”标志:
add_header 'Access-Control-Allow-Credentials' 'true' 永远;
NGINX 代理我的 Express NodeJS 应用。
虽然在我的开发环境中我正确地得到错误代码和错误响应,但在生产环境中我只得到没有错误响应的错误代码。
但是,当我 运行 PowerShell 中的失败请求时,我确实收到错误响应。
在生产中,API 和应用程序位于不同的子域中:
- "api.domain.com" // Express 后端
- "app.domain.com" // 使用 axios 的 React 应用
在开发中,所有内容都托管在同一域中。
HTTP/2XX 请求按预期工作。
请在下面找到生产 NGINX conf(来自 available-sites):
server {
server_name api.domain.com;
location / {
if ($request_method = OPTIONS ) {
add_header Content-Length 0;
add_header Content-Type text/plain;
add_header 'Access-Control-Allow-Origin' 'https://app.domain.com' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept, Origin';
return 204;
}
add_header 'Access-Control-Allow-Origin' 'https://app.domain.com' always;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PATCH, PUT,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type, Accept, Origin';
proxy_pass http://localhost:3008;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/api.domain.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/api.domain.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = api.domain.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name api.domain.com;
return 404; # managed by Certbot
}
我已经测试了各种 header 配置但均未成功,如有任何帮助,我们将不胜感激。
我缺少以下 NGINX header 的“始终”标志: add_header 'Access-Control-Allow-Credentials' 'true' 永远;