首页 > 解决方案 > 在同一台服务器上运行 PHP 和 Node.js 的问题

问题描述

我正在尝试在同一台服务器上运行带有 socket.io 的 Node.js。我们有一个小工具可以让用户测试他们的 websockets,你可以在这里找到。现在的问题是它没有与域连接,例如如果不使用原始 ip https://123.23.23.12:3001/socket.io/?EIO=3&transport=polling&t=NIHXfZI(为了保密而披露真实 ip)它连接到 socket.io 但是当我使用https://www.101toolbox.com:3001/socket.io/?EIO=3&transport=polling&t=NIHXfZI它只是无法连接。现在我尝试创建反向代理和其他东西,但它也没有工作,服务器使用 laravel 和一些由 pm2 运行的节点 js,请注意它们在将网站移动到新主机之前工作没有使用任何特殊apache2 配置。这是apache2配置,我真的没有想法。

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerName 101toolbox.com
        ServerAlias www.101toolbox.com *.101toolbox.com 101toolbox.com
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/101toolbox/public
        ErrorLog ${APACHE_LOG_DIR}/101toolbox_error.log
        CustomLog ${APACHE_LOG_DIR}/101toolbox_access.log combined
        <Directory /var/www/101toolbox>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>

SSLCertificateFile /etc/letsencrypt/live/101toolbox.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/101toolbox.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

标签: node.jssocket.ioapache2

解决方案


这是使节点应用程序工作的 apache2 的正确配置。事实证明,我应该为 socket.io 注册 2 个不同的代理。

<IfModule mod_ssl.c>
<VirtualHost *:443>
    LoadModule proxy_module modules/mod_proxy.so
    LoadModule proxy_http_module modules/mod_proxy_http.so

    ServerName 101toolbox.com
    ServerAlias www.101toolbox.com *.101toolbox.com 101toolbox.com
    ServerAdmin webmaster@localhost
    DocumentRoot /var/www/101toolbox/public
    ErrorLog ${APACHE_LOG_DIR}/101toolbox_error.log
    CustomLog ${APACHE_LOG_DIR}/101toolbox_access.log combined

    <Directory /var/www/101toolbox>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    SSLProxyEngine On
    SSLProxyVerify none
    SSLProxyCheckPeerCN off
    SSLProxyCheckPeerName off
    SSLProxyCheckPeerExpire off 
    ProxyRequests Off 
    ProxyPass /node https://127.0.0.1:3000
    ProxyPassReverse /node https://127.0.0.1:3000

    ProxyPass /sockets https://127.0.0.1:3001
    ProxyPassReverse /sockets https://127.0.0.1:3001
    
    ProxyPreserveHost On
    ProxyPass   /socket.io https://127.0.0.1:3001/socket.io
    ProxyPassReverse /socket.io https://127.0.0.1:3001/socket.io

SSLCertificateFile /etc/letsencrypt/live/101toolbox.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/101toolbox.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

推荐阅读