Laravel Websockets 反向代理端口 Nginx 配置到 Apache 虚拟主机

Laravel Websockets reverse proxy port Nginx config to Apache virtual host

我正在尝试将我的 Laravel Websockets 应用程序部署为我的 Laravel 8 API 项目的一部分。一切都在本地工作,但部署后我无法连接到我网站域上的端口 6001,这是一个子域。

我正在使用带有 Apache 的 Cent OS 8 服务器,并且已经在 https://api.example.com/, and in order for my site on https://site.example.com/ I've gone ahead and created a sub-domain called https://api-socket.example.com/ 上为我的网站打开了端口 80,并且需要将其代理到端口 6001

我尝试将 Nginx 服务器的 config 替换为虚拟主机,但是当我重新启动 httpd 时,Cloudflare 出现 521 错误,我的配置是:

/etc/httpd/sites-available/api-socket.example.com.conf

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName api-socket.example.com

    <Proxy *>
        Require all granted
        Allow from all
    </Proxy>

    SSLEngine on
    SSLProxyEngine on
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off

    RewriteEngine on
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule .* wss://127.0.0.1:6001%{REQUEST_URI} [P]
    ProxyPass / ws://127.0.0.1:6001
    ProxyPassReverse / ws://127.0.0.1:6001
</VirtualHost>

我安装了 certbot,生成 SSL 后,我得到了上述文件的 Let's Encrypt 变体 *:443,但随后我的 Web 服务器停止了 运行?

我在这里缺少什么来创建代理,以便当我转到 https://api-socket.example.com/ 它只是代理到该端口上服务器本地主机上的 Web 套接字服务器 运行?

您能否在没有 ssl 的情况下尝试一下,以确保配置是否有效。 确保启用以下模块

a2enmod proxy
a2enmod proxy_http
a2enmod proxy_wstunnel
<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName api-socket.example.com

    RewriteEngine On
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule /(.*)           ws://127.0.0.1:6001/ [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)           http://127.0.0.1:6001/ [P,L]

    ProxyPassReverse / http://127.0.0.1:6001/
</VirtualHost>

如果此配置有效,请对端口 443 尝试相同的配置,稍作修改

<VirtualHost *:443>
    ServerAdmin admin@example.com
    ServerName api-socket.example.com

    RewriteEngine On
    ProxyPreserveHost On
    ProxyRequests Off
    RewriteCond %{HTTP:Upgrade} =websocket [NC]
    RewriteRule ^/ws/(.*)       wss://127.0.0.1:6001/ [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)           http://127.0.0.1:6001/ [P,L]

    ProxyPassReverse / http://127.0.0.1:6001/
</VirtualHost>

更新: 如果以上不起作用,只需找到一个 link 使其与 Apache、Certbot 和 Cloudflare 一起使用 https://github.com/cdr/code-server/discussions/2104#discussioncomment-360665