NGINX配置socket.io所需的指导

时间:2018-03-19 13:53:26

标签: nginx socket.io socket.io-1.0 socket.io-redis

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

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;
}
}

1 个答案:

答案 0 :(得分: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';
    }