NGINX代理websocket配置

  如题,我在100.100.6.22的8100端口启动了一个web项目,通过/live/提供http接口,通过/live/ws/提供websocket接口,进入到nginx所在目录,修改nginx.conf文件:

[sbt@wlf nginx]$ cd /usr/local/nginx
[sbt@wlf nginx]$ vi conf/nginx.conf

  主要新增两个location:

server {
    listen       7777;
    server_name  100.100.6.22;
    charset utf-8;
    access_log  logs/nacos.access.log  main;
    error_log   logs/nacos.error.log notice;
    index index.html; 

    location /live/ {
      proxy_pass http://100.100.6.22:8100/;
      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-Forwarded-Proto  $scheme;
    }


    location /live/ws/ {
      proxy_pass http://100.100.6.22:8100/ws/;
      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_http_version  1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
    }
}

  保存后重启nginx,使用root权限或者切换回root用户重启:

[sbt@wlf nginx]$ sudo ./sbin/nginx -s reload

  跑一次websocket接口,可以看nginx日志:

[sbt@wlf nginx]$ tail -f logs/access.log     
{ "@timestamp": "2020-11-24T13:43:11+08:00", "remote_addr": "10.110.1.66","costime": "8.183","realtime": "8.183","status": 101,"x_forwarded": "","referer": "","request": "GET /live/ws/meetingConnect?lang=cn&codec=raw&appid=123456 HTTP/1.1","upstr_addr": "100.100.6.22:8100","bytes":4695,"dm":,"agent": "" }

 

posted on 2020-11-24 17:00  不想下火车的人  阅读(6562)  评论(2编辑  收藏  举报

导航