微服务架构中的CORS问题

时间:2019-02-27 09:56:12

标签: nginx cors

经过大量搜索和尝试,我决定将我的问题发布到CORS附近。

上下文

我在PI上的本地服务器(NGINX)上有一个网站。我的网站通过REST API处理微服务。

我的PI位于我的Freebox(法语互联网框)的后面,后者将特定端口(例如:28800)重定向到我pi的本地@。仅供参考,我没有(也不想要)固定IP。我与其他用户共享我的IP。因此,我的端口范围在20000到30000之间(大约)。

我有一个NO-IP主机名,指向我的带有专用端口的Freebox IP。

要恢复: 用户-> mysite.no-ip.com ---(翻译NO-IP)---> 91.123.456.678:specifcport ----(重定向到我的pi)---> 192.168.0.30:80--- -(nginx)---> index.html

我可以做什么:

在我的nginx网站conf中,我设置了一个位置/ core。 当我做 mysite.no-ip.com/core/blabla,我很好地获得了blabla治疗。 没问题!

这是我的问题:

当我单击启动blabla动作的按钮(通过mysite.no-ip.comm / core / blabla上的AJAX POST)时,出现CORS错误。

我试图修改我的nginx配置,添加标头取决于OPTIONS / GET / POST请求类型。没有附加内容。

但是,如果通过按钮发送请求,我发现我的nginx永远不会被请求/ core / blabla触及。我有APACHE服务器抛出的CORS答案。而且我没有安装任何APACHE服务器。那么,也许这是隐藏在我的Freebox中的服务器?不知道如何设置它以允许CORS请求。

这是我的Nginx配置文件的片段

  server {
    listen 80;
    server_name mysite.no-ip.com

    location / {
    alias /path/to/root/production/folder/here;
    }

    location /core/ {
        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }  
        if ($request_method = 'POST') { 
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
       }

      proxy_pass http://localhost:8002/;
      }
 }

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

好的,解决我的问题,这是通过将我的Internet提供程序框配置和我的请求中添加一些标头的组合解决的。