宝塔 nginx 反向代理追加websocket支持

December 09, 2023
测试
测试
测试
测试
5 分钟阅读

背景:xxx项目搭建在本地局域网中,其地址是:127.0.0.1:8800,并且这个项目某功能单元包含websocker技术。

需求:用户在客户端以“xxx.com”即可访问到局域网 127.0.0.1:8800 ,其“xxx.com”访问的功能同局域网访问效果。

nginx站点配置

server
{
    listen 80;
    server_name xxx.com;
    index index.php index.html index.htm default.php default.htm default.html;
    root /www/wwwroot/xxx.com;
    
    #SSL-START SSL相关配置,请勿删除或修改下一行带注释的404规则
    #error_page 404/404.html;
    #SSL-END

    
    #ERROR-PAGE-START  错误页配置,可以注释、删除或修改
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    #ERROR-PAGE-END
    
    #PHP-INFO-START  PHP引用配置,可以注释或修改
    #清理缓存规则

    location ~ /purge(/.*) {
        proxy_cache_purge cache_one $host$1$is_args$args;
        #access_log  /www/wwwlogs/xxx.com_purge_cache.log;
    }


    #引用反向代理规则,注释后配置的反向代理将无效(重要!!!!!)
    # 具体配置请见本文章`反向代理配置`节点
    include /www/server/panel/vhost/nginx/proxy/xxx.com/*.conf;



    include enable-php-00.conf;
    #PHP-INFO-END
    
    #REWRITE-START URL重写规则引用,修改后将导致面板设置的伪静态规则失效
    include /www/server/panel/vhost/rewrite/xxx.com.conf;
    #REWRITE-END
    
    #禁止访问的文件或目录
    location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
    {
        return 404;
    }
    
    #一键申请SSL证书验证目录相关设置
    location ~ \.well-known{
        allow all;
    }
    
    access_log  /dev/null;
    error_log  /www/wwwlogs/xxx.com.error.log;
}

反向代理配置

本地中转

location /
{
    proxy_pass http://127.0.0.1:8800;

    ####################################
    #支持WEBSOCKET
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    ####################################
    

   
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    
    add_header X-Cache $upstream_cache_status;
    
    add_header Cache-Control no-cache;
}

附加其它协议配置

远程中转HTTPS

location / {
    proxy_pass https://xxx.com/abc;
    proxy_set_header Host xxx.com:443;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto https;
    
   add_header X-Cache $upstream_cache_status;
    
   add_header Cache-Control no-cache;
}

远程中转HTTP

location / {
    proxy_pass http://xxx.com/abc;
    proxy_set_header Host xxx.com:80;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-Proto http;
    
   add_header X-Cache $upstream_cache_status;
    
   add_header Cache-Control no-cache;
}

继续阅读

更多来自我们博客的帖子

如何安装 BuddyPress
由 测试 December 17, 2023
经过差不多一年的开发,BuddyPress 这个基于 WordPress Mu 的 SNS 插件正式版终于发布了。BuddyPress...
阅读更多
Filter如何工作
由 测试 December 17, 2023
在 web.xml...
阅读更多
如何理解CGAffineTransform
由 测试 December 17, 2023
CGAffineTransform A structure for holding an affine transformation matrix. ...
阅读更多