我在服务器上运行了两个应用程序。
最初,我在两个子域中都使用通配符SSL证书,而nodejs负责CORS。但是,然后我们被要求对两个域使用单独的SSL证书。当我使用单独的SSL证书时,Nginx开始拒绝cors请求(我可以使用邮递员使用API)。
然后我在这里的一些文章中了解了Nginx cors选项,并提出了以下Nginx设置
/ etc / nginx / sites-available / default
# Vendor API server { listen 5511 ssl; ssl_certificate /ssl/ssl-bundle-api.crt; ssl_certificate_key /ssl/ssl-api.key; location /{ include /etc/nginx/shared/allow-cors; proxy_pass http://localhost:4511; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; } } # Retailer app server { listen 5512 ssl; ssl_certificate /ssl/ssl-bundle-react.crt; ssl_certificate_key /ssl/ssl-react.key; location /{ proxy_pass http://localhost:4512; proxy_redirect off; proxy_set_header Host $host:$server_port; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Ssl on; } }
/ etc / nginx / shared / allow-cors
if ($request_method = "OPTIONS") { add_header Access-Control-Allow-Origin $http_origin always; add_header Access-Control-Allow-Credentials true always; add_header Access-Control-Allow-Methods 'DELETE,GET,OPTIONS,POST,PUT' always; add_header Access-Control-Allow-Headers 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Token-Auth,X-Mx-ReqToken,X-Requested-With' always; add_header 'Access-Control-Max-Age' 1728000 always; add_header 'Content-Type' 'text/plain charset=UTF-8' always; add_header 'Content-Length' 0 always; return 204; } add_header Access-Control-Allow-Origin $http_origin always; add_header Access-Control-Allow-Credentials true always; add_header Access-Control-Allow-Methods 'DELETE,GET,OPTIONS,POST,PUT' always; add_header Access-Control-Allow-Headers 'Accept,Authorization,Cache-Control,Content-Type,DNT,If-Modified-Since,Keep-Alive,Origin,User-Agent,X-Requested-With,X-Token-Auth,X-Mx-ReqToken,X-Requested-With' always;
然后,我从nodejs API中删除了cors设置。这似乎完全解决了我的AWS ec2 ubuntu 18.04实例上的问题。但是,当我在客户端的本地服务器(使用ec2实例的映像创建)上部署此代码时,它又遇到了问题。在Firefox上,飞行前检查似乎获得了预期的204,但是随后我没有看到发送任何POST请求。
[在Chrome上,我看到204的飞行前检查成功。我还看到了Chrome中的实际POST请求,但状态为
(failed) net::ERR_FAILED
。有人可以帮忙解决这个问题。
我在服务器上运行了两个应用程序。在端口4512(在HTTP上本地)和5512(在https上)上的React应用。可以在https://merchant.abc.com:5512上访问端口4511上的Node js(express)API(位于...
我认为返回204
是因为这是负载均衡器的配置。也许您可以检查this issue以了解您的情况。