【问题标题】:Laravel Websockets reverse proxy port Nginx config to Apache virtual hostLaravel Websockets 反向代理端口 Nginx 配置到 Apache 虚拟主机
【发布时间】:2021-08-30 04:59:05
【问题描述】:

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

我正在使用带有 Apache 的 Cent OS 8 服务器,并且已经在 https://api.example.com/ 上为我的网站打开了端口 80,为了让我在 https://site.example.com/ 上的网站,我继续创建了一个名为的子域https://api-socket.example.com/ 并且需要将其代理到端口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,但随后我的网络服务器停止运行?

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

【问题讨论】:

  • 这就是上述配置的目的,因此它存在于api-socket.example.com 上,因此它只用于套接字,这就是为什么它在 URL 中有6001,你有一些例子吗?
  • 您服务器上的 Apache 版本是多少?
  • @Haridarshan Apache 版本:Apache/2.4.37 (centos),安装了 certbot 并使用 Cloudflare for SSL

标签: php laravel apache proxy


【解决方案1】:

您能否在不使用 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/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)           http://127.0.0.1:6001/$1 [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/$1 [P,L]
    RewriteCond %{HTTP:Upgrade} !=websocket [NC]
    RewriteRule /(.*)           http://127.0.0.1:6001/$1 [P,L]

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

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

【讨论】:

  • 好的,所以我已经完成了所有设置,不知道如何检查a2enmod 安装,因为在我的 Cent OS 8 服务器上运行它们不会返回任何结果,只是说命令不是成立。此外,当我在api-socket.example.com 访问我的页面时,我得到一个 404 未找到,而/laravel-websockets 页面在连接到6001 时尝试连接到wss://api.example.com:6001/app/local,所以我认为它会起作用?
  • 看来a2enmod 是给 Ubunto 的,不是 Centos 的?
  • 是的..你可以通过这个命令检查安装的模块httpd -M | grep -i proxy
  • DocumentRoot设置为vhost conf中代码库的路径
猜你喜欢
  • 2017-09-25
  • 2021-01-03
  • 2021-08-16
  • 2014-12-02
  • 2014-10-14
  • 2016-10-28
  • 1970-01-01
  • 2011-05-04
  • 2017-06-14
相关资源
最近更新 更多