两个NGINX服务器一个通过CORS问题

时间:2017-11-10 20:04:19

标签: nginx cors

我有两个nginx服务器通过docker在我的localhost上运行。我希望一台服务器通过ajax调用另一台服务器(api)并做出反应。我在一点上完成了所有这些工作,但我再次启动了我的服务器而且我得到了:

  

无法加载http://localhost:8081/items?Title=butterflies:否   '访问控制允许来源'标题出现在请求的上   资源。起源' http://localhost:8080'因此是不允许的   访问。

所以Cors问题又回来了。我在default.conf中有以下代码:

server {
    listen  8080;

    root    /app/public;
    index   index.php index.html;
    charset utf-8;

    access_log /var/log/nginx/access.log;
    error_log  /var/log/nginx/error.log;

    error_page 404 = /handle404.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
        add_header "Access-Control-Allow-Origin" "*" always;
        add_header "Access-Control-Allow-Methods" "POST, GET, OPTIONS" always;
    }

    # Docker image version endpoints
    location /version {
        default_type text/plain;
        rewrite ^/version/bisapi-php$ /version/bisapi-php.php last;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_read_timeout 900;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

此外,这是我的代码,我在其中调用api:

var encodedURI = window.encodeURI('http://localhost:8081/items?Title=butterflies', { crossdomain: true });
    return axios.get(encodedURI).then(function(response){
        return response.data;
    });

所以我显然在我的网站中根据我的网站进行了通用调用。不知道为什么它不会允许这样做。我有点在nginx的新领域,所以请帮忙。

1 个答案:

答案 0 :(得分:0)

我将我的标题从位置\中取出并将它们放在服务器标记内以全局调用标题并且它有效。我一定是在不同的道路上。