2021年5月8日 星期六

Nginx 反向代理 WebSocket

設定 https://example.com:9999/socket.io
反向代理到 http://192.168.0.10:9999 的 WebSocket 服務

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

upstream websocket {
    server 192.168.0.10:9999;
}

server {
    listen 9999 ssl;
    server_name example.com;
    
    #ssl
    ssl_certificate /etc/nginx/ssl/example.com.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com.key;

    location /socket.io {
        proxy_pass http://websocket;
        #Version 1.1 is recommended for use with keepalive connections
        proxy_http_version 1.1; #WebSocket
        proxy_set_header Upgrade $http_upgrade; #WebSocket
        proxy_set_header Connection $connection_upgrade; #WebSocket       
        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 https;
        proxy_set_header Cookie $http_cookie;
    }
    
    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }
}


參考:



沒有留言:

張貼留言