宝塔/Nginx/WebSocket

记录下,宝塔配置默认wss无法连接

选择网站 ->设置->反向代理->没有就添加一个,有就直接点配置文件

#PROXY-START/

location /
{
    proxy_pass http://localhost:端口/;
    proxy_set_header Host www.xxx.com;
    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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    
    set $static_filesSQnNhw0 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filesSQnNhw0 1;
    	expires 1m;
        }
    if ( $static_filesSQnNhw0 = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/

加两行

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

改动后就是这样

#PROXY-START/

location /
{
    proxy_pass http://localhost:端口/;
    proxy_set_header Host www.xxx.com;
    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;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    
    set $static_filesSQnNhw0 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filesSQnNhw0 1;
    	expires 1m;
        }
    if ( $static_filesSQnNhw0 = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

#PROXY-END/

反向代理这么加

最后提一嘴

解决nginx下websocket的长连接问题icon-default.png?t=N7T8https://baijiahao.baidu.com/s?id=1714370851394804830&wfr=spider&for=pc这个文章有用,好东西

分享一个十分精简的开发框架,十分钟上手

dev-tmpl: 快速开发框架

挺好用的,简单,灵活

### 宝塔面板中 Nginx 的使用与配置教程 #### 一、Nginx 自定义 404 页面配置 在宝塔面板中,可以通过自定义 `nginx.conf` 文件实现 404 错误页面的设置。具体操作如下: 1. 登录到宝塔面板并进入网站管理界面。 2. 找到目标站点,点击“设置”按钮,在弹出菜单中选择“配置文件”选项卡。 3. 编辑该站点的 Nginx 配置文件,添加以下代码片段以指定 404 页面路径: ```nginx error_page 404 /custom_404.html; location = /custom_404.html { root /www/wwwroot/yourdomain.com/; } ``` 上述代码表示当发生 404 错误时返回 `/custom_404.html` 页面[^1]。 4. 修改完成后保存配置,并通过命令行验证 Nginx 配置文件的有效性: ```bash /opt/nginx/sbin/nginx -t ``` --- #### 二、Nginx 反向代理 WebSocket (WSS) 配置 为了支持 WebSocket 协议(尤其是加密版本 WSS),需要调整 Nginx 中的反向代理规则。以下是具体的配置方式: 1. 进入宝塔面板的目标站点配置文件编辑器。 2. 添加或修改以下内容至服务器块内: ```nginx map $http_upgrade $connection_upgrade { default upgrade; '' close; } server { listen 80; server_name yourdomain.com; location /ws/ { proxy_pass http://backend_server_address; # 替换为目标地址 proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; } } ``` 此处的关键在于 `proxy_set_header` 和 `$connection_upgrade` 映射逻辑[^2]。 3. 测试配置有效性后重新加载服务即可生效。 --- #### 三、Windows 环境下手动配置 PHP-Nginx 环境 对于 Windows 用户而言,虽然官方推荐 Linux 平台作为生产环境首选方案,但在开发阶段仍可尝试搭建本地测试环境。以下是基于宝塔面板的手动配置流程概述: 1. 创建一个新的虚拟主机目录结构; 2. 将 PHP-FPM 设置为 FastCGI 接口监听模式; 3. 更新 Nginx 主配置文件加入类似以下条目: ```nginx fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_index index.php; fastcgi_pass 127.0.0.1:9000; ``` 更多细节参见参考资料[^3]。 --- #### 四、Docker+Nginx+Gunicorn 部署 Django 应用实例 如果计划利用 Docker 技术简化运维工作流,则可以参考此部分说明完成 Django Web 应用程序上线准备过程: 1. 构建镜像前需先确认依赖项已妥善处理完毕,例如运行数据库迁移脚本以及收集静态资源等任务: ```bash python manage.py migrate python manage.py collectstatic --noinput ``` 2. Gunicorn 是一种高性能 Python HTTP Server 工具,适合用来承载大型流量场景下的 API 请求负载均衡需求;而 Nginx 则主要负责前端访问入口转发功能[^4]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值