首页 » Web服务器 » Nginx » 正文

nginx代理websoket

配置文件如下:

http {
    include       mime.types;
    default_type  application/octet-stream;

    # for websoket
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    log_format  main escape=json '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for" '
                      '"$upstream_addr" "$request_time" "$upstream_response_time"';

    access_log  logs/access.log  main;

    server {
        listen 80 default;
        server_name abc.test.com;
        access_log      logs/access.log       main;
        error_log       logs/error.log;

        location / {
            proxy_pass      http://127.0.0.1:8080;
			
            # for websoket
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection $connection_upgrade;
        }
    }
}

 

关键项即文中注释 for websoket项。

 

更多参考:https://www.nginx.com/blog/websocket-nginx/

 

 

赞 (0)

发表评论

*