如何从一个子域登录到另一个子域并避免CORS问题?

时间:2019-05-20 16:31:15

标签: cors nginx-config

我有一个www.mydomain.test是登录表单。我想将此表格发送到inside.mydomain.test

现在,我在www.mydomain.test上发送表单时收到此错误消息:

Access to XMLHttpRequest at 'http://inside.mydomain.test/api/v1/login' from origin
'http://www.mydomain.test' has been blocked by CORS policy: Response to
preflight request doesn't pass access control check: The
'Access-Control-Allow-Origin' header contains the invalid value 'false'.

我在服务器上使用nginx HTTP服务器,我有权编辑服务器配置。我将inside.mydomain.test上的配置更新为此:

server {
    root /var/www/inside.mydomain.test;
    server_name inside.mydomain.test;

    # location ~* \.(?:css|js|map|jpe?g|gif|png)$ { }
    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/v1 {
        proxy_pass http://localhost:3000;
        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;

        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            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-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
        if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
            add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
        }
    }
}

这是表格enable-cors.org,但对我不起作用...知道吗?

0 个答案:

没有答案
相关问题