如何在nginx中设置websocket代理和cors?

时间:2018-05-02 05:03:50

标签: ssl nginx proxy server cors

我已经在nginx中设置了代理,并在tomcat中设置了一个Web套接字服务器

如果我使用http://MY_URL/api/ws/data发出请求,则端口应从80更改为8060 最后,那些最终必须是https

/etc/nginx/conf.d/default.conf(我使用的是default.conf)

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        add_header Access-Control-Allow-Origin *;
        root /usr/share/nginx/html;
        try_files $uri $uri/ /index.html;
    }

location /api/ {
  proxy_pass              http://localhost:8080;
    }

    # redirect server error pages to the static page /50x.html
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

location /api/ws/data/ {
        add_header Access-Control-Allow-Origin *;
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

        proxy_set_header Host $http_host;
        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_set_header X-Forwarded-Proto $scheme;

        proxy_pass http://localhost:8060;
        proxy_redirect off;
        proxy_read_timeout 86400;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
    }
}

何时,我在本地服务器发出请求

无法加载http://MY_URL/api/ws/data/info?t=1525237291534:CORS政策阻止了从“http://MY_URL/api/ws/data/info?t=1525237291534”到“https://MY_URL/api/ws/data/info?t=1525237291534”的重定向:没有“Access-Control-Allow-Origin”标题存在于请求的资源上。因此,不允许原点“http://localhost:3000”访问。

如何在nginx中设置websocket代理和cors?

++++

使用--disable-web-security命令执行此chrome后,

错误请求

主机和端口的这种组合需要TLS。

是什么意思呢?以及如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

要解决CORS问题,请尝试使用 more_set_headers 而不是 add_header

more_set_headers 'Access-Control-Allow-Origin:*';
more_set_headers 'Access-Control-Allow-Methods: GET,POST,OPTIONS';
more_set_headers 'Access-Control-Allow-Credentials:true';
more_set_headers 'Access-Control-Allow-Headers:DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

more_set_headers 指令是HttpHeadersMore模块的一部分,该模块包含在nginx的nginx-extras flavor中,您可以通过执行以下操作将其安装在ubuntu 16上:

  

sudo apt-get install nginx-extras

答案 1 :(得分:0)

我建议为您的websocket API创建一个单独的域。这理想吗?不,但是它将为您节省搜索堆栈溢出来寻找更短解决方案的时间。

这是我的第二种尝试,试图找到一种更好的方法,并最终使用CORS添加新域(或子域)要容易得多,并且完全可行。

server {
    server_name  socketapi.ddns.net;


    location / {

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

        #Enables socket connectiosn over https, will forward to the http version
        proxy_pass http://127.0.0.1:3050; #could be localhost if Echo and NginX are on the same box
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $remote_addr;
    }
     client_max_body_size 20M; 
    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot

    ssl_certificate /etc/letsencrypt/live/socketapi.ddns.net/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/socketapi.ddns.net/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}