NGINX代理我的Express NodeJS应用程序。
在我的开发环境中,我可以正确地获得错误代码和错误响应,而在生产中,我只获得错误代码而没有错误响应。
但是,当我在PowerShell中运行失败的请求时,我确实会得到错误响应。
在生产过程中,API和APP位于不同的子域:
- "api.domain.com"//Express后端
- "app.domain.com"//使用axios的React应用程序
在开发过程中,所有内容都托管在同一个域上。
HTTP/2X请求按预期工作。
请在下面找到生产NGINX conf(来自可用站点(:
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
}
我已经测试了各种头部配置,但没有成功,任何帮助都将不胜感激。
我错过了"总是";以下NGINX标头的标志:add_header'访问控制允许凭据''true'始终;