Nginx 配置 ws://:

Nginx 配置 ws://:

我有一个域名,我称之为<domain>ip<ip>和端口<my-local-port>,我不想在我的服务器上使用 nginx(dns 成功绑定到我的服务器),我的问题是反向代理 ws:// 协议到我的服务器我通过两个链接发送我的请求:

  • https://<ip>:<my-local-port>

  • ws://<ip>:<my-local-port>

对于 https 我将 nginx 配置为:

server {
      listen       443;
      server_name  <domain>    www.<domain> ;

      location / {
        proxy_pass       http://localhost:<my-local-port>;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
      }
}

我绑定https://<domain>到我的本地端口<my-local-port>

我如何绑定 ws://<domain>到本地端口<my-local-port>

答案1

首先定义一个websocket上游:

upstream websocket {
   server <ip>:<my-local-port>;
}

然后服务器将http连接升级为websocket:

map $http_upgrade $connection_upgrade {
   default upgrade;
   '' close;
}
server {
   listen 8020;
      location / {
         proxy_pass http://websocket;
         proxy_http_version 1.1;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $connection_upgrade;
         proxy_set_header Host $host;
      }
   }
}

来源

相关内容