-1

下面的代码位于我在 nginx 中启用站点的文件夹下..
但是,我“add_header 'Access-Control-Allow-Origin' '*';”的行 和 add_header "'Access-Control-Allow-Credentials' 'true';" 在 if 语句导致我的套接字有多个标题之后。我可以寻求解决此问题的建议吗?

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name devapi.doctoroneworld.com;


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

proxy_set_header Content-Type 'application/json';

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:9000/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
}
4

1 回答 1

0

这就是我在上面的代码中添加的内容:我注释掉了影响套接字请求的 add_headers

set $cors '';
    if ($request_uri !~ '^/socket.io-client') {
    set $cors 'true';
    }

    if ($cors = 'true') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    }
于 2018-03-19T15:38:05.283 回答