Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
NGINX是否要反转代理WebSocket并启用SSL(wss://)?_Ssl_Tcp_Proxy_Nginx_Mod Proxy - Fatal编程技术网

NGINX是否要反转代理WebSocket并启用SSL(wss://)?

NGINX是否要反转代理WebSocket并启用SSL(wss://)?,ssl,tcp,proxy,nginx,mod-proxy,Ssl,Tcp,Proxy,Nginx,Mod Proxy,我对自己构建NGINX感到非常迷茫和陌生,但我希望能够在没有额外层的情况下启用安全的websockets 我不想在websocket服务器本身上启用SSL,而是想使用NGINX在整个过程中添加SSL层 每个网页都说我做不到,但我知道我可以!多亏了(我自己)能教我怎么做的人 不要害怕,因为一群勇敢的Ops程序员已经用全新的解决了这个问题 写在2012年8月,所以如果你来自未来,你应该做你的家庭作业 先决条件 假设您正在使用CentOS: 删除NGINX的当前实例(建议为此使用dev服务器) 如果

我对自己构建NGINX感到非常迷茫和陌生,但我希望能够在没有额外层的情况下启用安全的websockets

我不想在websocket服务器本身上启用SSL,而是想使用NGINX在整个过程中添加SSL层


每个网页都说我做不到,但我知道我可以!多亏了(我自己)能教我怎么做的人

不要害怕,因为一群勇敢的Ops程序员已经用全新的解决了这个问题

写在2012年8月,所以如果你来自未来,你应该做你的家庭作业

先决条件 假设您正在使用CentOS:

  • 删除NGINX的当前实例(建议为此使用dev服务器)
  • 如果可能,保存旧的NGINX配置文件,以便可以重复使用它们(包括
    init.d/NGINX
    脚本)
  • yum安装pcre-pcre-devel-openssl-openssl-devel
    以及构建NGINX所需的任何其他libs
  • 从GitHub获取nginx\u tcp\u proxy\u模块,并记住放置它的文件夹(确保它未压缩)
构建新的NGINX 同样,假设CentOS:

  • cd/usr/local/
  • wget'http://nginx.org/download/nginx-1.2.1.tar.gz“
  • tar-xzvf nginx-1.2.1.tar.gz
  • cd nginx-1.2.1/
  • patch-p1
  • /configure--add module=/path/to/nginx\u tcp\u proxy\u module--with-http\u ssl\u module
    (如果需要,可以添加更多模块)
  • make
  • make-install
可选:

  • sudo/sbin/chkconfig nginx开启
设置Nginx 如果要重新使用旧的配置文件,请记住首先复制它们

重要提示:您需要在配置文件的最高级别创建一个
tcp{}
指令。确保它不在
http{}
指令内

下面的示例配置显示了单个上游websocket服务器,以及SSL和非SSL的两个代理

tcp {
    upstream websockets {
        ## webbit websocket server in background
        server 127.0.0.1:5501;
        
        ## server 127.0.0.1:5502; ## add another server if you like!

        check interval=3000 rise=2 fall=5 timeout=1000;
    }   

    server {
        server_name _;
        listen 7070;

        timeout 43200000;
        websocket_connect_timeout 43200000;
        proxy_connect_timeout 43200000;

        so_keepalive on;
        tcp_nodelay on;

        websocket_pass websockets;
        websocket_buffer 1k;
    }

    server {
        server_name _;
        listen 7080;

        ssl on;
        ssl_certificate      /path/to/cert.pem;
        ssl_certificate_key  /path/to/key.key;

        timeout 43200000;
        websocket_connect_timeout 43200000;
        proxy_connect_timeout 43200000;

        so_keepalive on;
        tcp_nodelay on;

        websocket_pass websockets;
        websocket_buffer 1k;
    }
}

请注意,nginx现在在1.3.13版上支持Websockets。使用示例:

location /websocket/ {

    proxy_pass ​http://backend_host;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_read_timeout 86400;

}

您还可以查看和文档。

Pankaj Malhotra的一篇优秀、简洁的文章讨论了如何使用NGINX实现这一点,该文已提供

NGINX的基本配置如下所示:

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}

upstream appserver {
    server 192.168.100.10:9222; # appserver_ip:ws_port
}

server {
    listen 8888; // client_wss_port

    ssl on;
    ssl_certificate /path/to/crt;
    ssl_certificate_key /path/to/key;


    location / {
        proxy_pass http://appserver;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}
这对我很有用:

location / {
    # redirect all HTTP traffic to localhost:8080
    proxy_pass http://localhost:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

--借用自:

对于我来说,它归结为
代理通行证
位置设置。我需要改用HTTPS协议,并在节点服务器端设置有效的SSL证书。这样,当我引入外部节点服务器时,我只需更改IP,其他所有配置都保持不变

我希望这能帮助一些人。。。我一直盯着问题看。。。叹息

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}
upstream nodeserver {
        server 127.0.0.1:8080;
}
server {
        listen 443 default_server ssl http2;
        listen [::]:443 default_server ssl http2 ipv6only=on;
        server_name mysite.com;
        ssl_certificate ssl/site.crt;
        ssl_certificate_key ssl/site.key;
        location /websocket { #replace /websocket with the path required by your application
                proxy_pass https://nodeserver;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection $connection_upgrade;
                proxy_http_version 1.1;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_intercept_errors on;
                proxy_redirect off;
                proxy_cache_bypass $http_upgrade;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-NginX-Proxy true;
                proxy_ssl_session_reuse off;
            }
}

适用于带SSL的.net core 2.0 Nginx

location / {
    # redirect all HTTP traffic to localhost:8080
    proxy_pass http://localhost:8080;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    # WebSocket support
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $http_connection;
}
使用nginx/1.14.0,这对我来说很有效

我有一个websocket服务器在端口8097上运行,用户从连接到端口8098上的wss,nginx只是解密内容并将其转发到websocket服务器

所以我有这个配置文件(在我的例子中是
/etc/nginx/conf.d/default.conf


如果要在测试环境中添加SSL,则可以使用
mkcert
。下面我提到了GithubURL

下面我还提到了反向代理的nginx配置示例

server {
        listen 80;
        server_name test.local;
        return 301 https://test.local$request_uri;
}
server {
  listen 443 ssl;
  server_name test.local;
  ssl_certificate /etc/nginx/ssl/test.local.pem;
  ssl_certificate_key /etc/nginx/ssl/test.local-key.pem;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Client-Verify SUCCESS;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://localhost:3000;
      proxy_redirect off;
      proxy_buffering off;
    }
}


这很有帮助,但我还是会在60秒时超时。我通过设置以下内容来解决这个问题:超时43200000;websocket\u connect\u超时43200000;websocket读取超时43200000;websocket\u发送\u超时43200000;代理连接超时43200000;代理读取超时43200000;代理发送超时43200000;我想在浏览器经过身份验证后,通过相同的http端口为WebSocket提供服务。看起来这无法处理同一端口上的WebSocket。人们是如何处理的?需要一些软件修改来检测传入的协议。由于WebSocket实际上是以HTTP握手(比TCP更高的软件级别)开始的,因此您必须调整应用程序以处理TCP和HTTP流量。我现在还不能推荐一种方法来做到这一点。如果2018年的其他人来到这里,这些指令将不再有效。请访问以获取最新说明或查看下面的Harlan T Wood答案。您找到解决方案了吗?由于我还必须面对一个类似的问题,因此它具有与上面所述相同的超时问题;)@3rdEden:对于超时问题,
proxy\u read\u timeout
有效,我编辑了答案。我应该将此配置放在哪里,后端主机是什么?@Sekai:A
location
指令放在
server
或另一个
location
指令中(请参阅)
后端\u主机
是一个
上游
(请参阅)-您将代理到的一个或一组服务器。此超时问题如何?我们真的必须将它设置为一个非常大的数字来避免它吗?现在没有更优雅的解决方案了吗?NGINX的现代版本也解决了超时问题吗?我很难让TeamCity的web套接字在反向代理后工作。您的
#WebSocket支持
为我做到了这一点。我以前尝试转发端口400,但是wss在443上工作。仅供未来读者参考:)你找到解决办法了吗?因为我也不得不面对一个类似的问题,所以我最喜欢这个答案,因为很多人(像你一样)同时使用/用于websockets和常规的HTTP2。@有人知道调用Javascript是什么吗?这通过unraid和反向代理对我和ShinobiCCTV都有效。我可以看到web套接字被阻止。我尝试了
localation/horizon
,但它不起作用。只有
localation/
location/websocify
起作用。
server {
        listen 80;
        server_name test.local;
        return 301 https://test.local$request_uri;
}
server {
  listen 443 ssl;
  server_name test.local;
  ssl_certificate /etc/nginx/ssl/test.local.pem;
  ssl_certificate_key /etc/nginx/ssl/test.local-key.pem;

   location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $remote_addr;
      proxy_set_header X-Client-Verify SUCCESS;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_pass http://localhost:3000;
      proxy_redirect off;
      proxy_buffering off;
    }
}