nginx 增加支持webSocket配置

2021/08 17 16:08

在http{}区域内增加:

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

如下片段:

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

    log_format  main  '$remote_addr  - $remote_user [$time_local] "$request" '
                 '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for" "$host"  "$cookie_ssl_edition" '
                 '"$upstream_addr"   "$upstream_status"  "$request_time"  '
                 '"$upstream_response_time" ';
    access_log  logs/access.log  main;


    client_max_body_size 50M;
    sendfile        on;
    server_tokens off;

    keepalive_timeout  65;
	
	
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }
	
    upstream myapp {
        server 127.0.0.1:8080;
    }

在 location 内添加:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

如下片段:

location /v1/ {
    proxy_pass  http://myapp;
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header Host $host;
    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 $connection_upgrade;
}

--转载请注明: https://www.macs.vip/archives/228