【问题标题】:NGINX Configs guidance needed for socket.iosocket.io 所需的 NGINX 配置指南
【发布时间】:2018-03-19 13:53:25
【问题描述】:

以下代码位于我在 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;
}
}

【问题讨论】:

  • 这与套接字有什么关系?
  • @sturcotte06 ""add_header 'Access-Control-Allow-Origin' '*';" and add_header "'Access-Control-Allow-Credentials' 'true';" 在 if 语句之后我的套接字有多个标头。我可以寻求解决此问题的建议吗“
  • "[...] 导致我的 requests 有多个标头。您的套接字有源地址、目标地址和其他属性,但它没有任何标头。套接字与协议无关,在您的情况下是 http。
  • @sturcotte06 对不起,我真的不明白你,但如果你指的是为什么我的套接字首先有标题,我认为这是因为 sock.io 中的 engine.io 正在添加在标题中也是如此。 =[
  • @sturcotte06 我对正则表达式不太好,但我试图制作一个正则表达式来查看请求是否在末尾没有 /socket.io,这样我就可以添加一个标头.. 有什么帮助吗?

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


【解决方案1】:

这是我在上面的代码中添加的:我注释掉了影响套接字请求的 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';
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-17
    • 2012-03-11
    • 2023-03-18
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    相关资源
    最近更新 更多