【问题标题】:how do setting websocket proxy and cors in nginx?如何在 nginx 中设置 websocket 代理和 cors?
【发布时间】:2018-10-12 03:19:37
【问题描述】:

我已经在 nginx 中设置了代理,并在 tomcat 中设置了 web socket 服务器

如果我向http://MY_URL/api/ws/data 发出请求,端口应该从 80 更改为 8060 还有,那些finally必须是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:从“http://MY_URL/api/ws/data/info?t=1525237291534”重定向到“https://MY_URL/api/ws/data/info?t=1525237291534”已被 CORS 策略阻止:请求的资源上不存在“Access-Control-Allow-Origin”标头.因此不允许访问 Origin 'http://localhost:3000'。

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

++++

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

错误请求

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

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

【问题讨论】:

  • P.S.它已经在 aws cloud front 中设置了 ssl 重定向

标签: ssl nginx proxy server cors


【解决方案1】:

我建议为您的 websocket API 创建一个单独的域。这是理想的吗?不,但它可以节省您在 stackoverflow 中搜索较短解决方案的时间。

这是我尝试找到更好方法的第二次尝试,最终使用 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
}

【讨论】:

    【解决方案2】:

    要解决 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

    【讨论】:

    • “等等?”。你能把这个更好吗?
    • 不确定您的意思,但“etc”代表“et cetera”,意思是“等等”,用于描述您可以继续添加超出示例的标题;为清楚起见已删除。
    • 哈哈哈对不起我真的累了。我的意思是你能包含所有完整的标题以及它们在server 配置中的位置。没关系,我通过为我的 websocket 连接创建一个新的api 域并使用传统的标头来解决这个问题
    猜你喜欢
    • 2020-04-26
    • 2020-01-25
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2019-05-13
    • 2020-08-06
    • 2020-09-02
    相关资源
    最近更新 更多