Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何让http和websocket使用同一端口 #40

Closed
Riceneeder opened this issue Nov 25, 2022 · 2 comments
Closed

请问如何让http和websocket使用同一端口 #40

Riceneeder opened this issue Nov 25, 2022 · 2 comments

Comments

@Riceneeder
Copy link

如题,目前实现的项目需要两种协议,但不想使用两个端口

@k8w
Copy link
Owner

k8w commented Nov 26, 2022

可以前置 nginx 解决,例如(nginx 配置):

# https 和 wss
server {
    server_name xxx.cn;

    listen 443 ssl;
    ssl on;
    ssl_certificate /etc/nginx/cert/k8w.cn.pem; 
    ssl_certificate_key /etc/nginx/cert/k8w.cn.key;

    # https (Server 是 http)
    location /match-server/ {
        proxy_set_header  Access-Control-Allow-Origin *;
        proxy_pass http://127.0.0.1:3200;
    }

    # wss (Server 是 ws)
    location /room-server/1/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:3201;
    }
    
}

# http 和 ws
server {
    server_name xxx.cn;
    
    listen 80;

    # http (Server 是 http) 跟上面一样
    location /match-server/ {
        proxy_set_header  Access-Control-Allow-Origin *;
        proxy_pass http://127.0.0.1:3200;
    }

    # ws (Server 是 ws) 跟上面一样
    location /room-server/1/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_pass http://127.0.0.1:3201;
    }
    
}

@Riceneeder
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants