尝试使用代理在 Apache 上运行 Faye websocket

Posted

技术标签:

【中文标题】尝试使用代理在 Apache 上运行 Faye websocket【英文标题】:Trying to run Faye websocket on Apache using a proxy 【发布时间】:2015-02-17 14:23:21 【问题描述】:

我正在尝试在生产环境中使用带有 Rails 的瘦服务器上的 Faye gem,但我无法使其工作。我正在使用 apache 2.4 和 ws_tunnel 也启用了代理模块。我的设置如下。

Apache 配置

<VirtualHost *:443>
    ServerName mysite.example.com
    ServerAlias mysite.example.com
    DocumentRoot /var/www/html/mysite/current/public

    SSLEngine on
    SSLCertificateFile "/etc/httpd/conf/keys/ServerCertificate.cer"
    SSLCertificateKeyFile "/etc/httpd/conf/keys/example.com.key"
    SSLCertificateChainFile "/etc/httpd/conf/keys/CACertificate-1.cer"

    RewriteEngine On
    SSLProxyEngine On
    ProxyRequests off
    ProxyPreserveHost on

    ProxyPass /faye !
    ProxyPassReverse /faye !

    ProxyPass / balancer://thinservers/
    ProxyPassReverse / balancer://thinservers/
    <Proxy *>
        Require all granted
    </Proxy>

    <Location /faye>
         ProxyPass ws://localhost:9292
         ProxyPassReverse ws://localhost:9292
    </Location>

    RequestHeader set X_FORWARDED_PROTO 'https'
    # Custom log file locations
    ErrorLog  /etc/httpd/logs/error-unilever.log
    CustomLog /etc/httpd/logs/access-unilever.log combined
 </VirtualHost>

Faye.ru 文件

require 'faye'

Faye::WebSocket.load_adapter('thin')

app = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

run app

我用下面的命令开始 faye

rackup faye.ru -E production -s thin 

这样我可以通过在浏览器中输入以下 url 来访问 faye.js。

https://mysite.example.com/faye/faye.js

但是例如当我输入https://mysite.example.com/faye/ 时,我得到以下结果

Sure you're not looking for /faye ?<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at 
root@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>

这很奇怪,因为我只得到“确定你不是在寻找 /faye 吗?”在开发中。

我将 application.js 中的 Faye 称为

client = new Faye.Client("https://mysite.example.com/faye/faye")

我收到类似的错误

POST https://mysite.example.com/faye/faye 400 (Bad Request)

我还尝试在代理设置中使用 wss 协议,并将我的证书路径设置为精简以从 SSL 开始,但没有任何效果。

我认为这可能是由于代理或 ws_tunnel 模块的原因,因为当我从服务器中的 shell 运行以下命令时

curl http://localhost:9292

我只得到“确定你不是在寻找 /faye 吗?”消息。

有人遇到过这种情况吗?

如果有人可以帮助我,我将不胜感激。

提前致谢。

【问题讨论】:

【参考方案1】:

对于那些遇到同样问题的人,我的建议是无论您使用的是哪个版本的 Apache,只要毫不犹豫地将其更改为 nginx。配置和运行它非常容易。

我正在为名为 example 的应用程序使用以下配置。正确设置 nginx 后,其他一切都运行良好。

希望这对某人有所帮助

# app: example
# locally running on port 7000
upstream example 
    server 127.0.0.1:7000;
    server 127.0.0.1:7001;


# Faye is set to run on port 9292
upstream example_socket
    server 127.0.0.1:9292;


server 
    # listen 80 if it's a non SSL site
    listen 443;
    server_name www.example.com;
    location /faye 
        proxy_pass http://example_socket;
        # Including common configurations
        include /etc/nginx/conf.d/proxy.conf;
        include /etc/nginx/conf.d/proxy_socket.conf;
   

    location / 
        root /var/www/vhost/example/current/public;
        proxy_pass http://example;
        # Including common configurations
        # can be found on Internet easily
        include /etc/nginx/conf.d/proxy.conf;
    

【讨论】:

以上是关于尝试使用代理在 Apache 上运行 Faye websocket的主要内容,如果未能解决你的问题,请参考以下文章

代理错误 502,在 apache2 服务器上运行节点 js 应用程序

使用 NodeJS 代理通行证的 Apache SSL 配置无法启动

对选择 Faye 或 Rails 4 Actioncontroller::Live 的困惑

Websockets 与主要应用程序(nginx + 乘客 + faye)

Nodejs 无法在 ws:// apache 代理服务器上建立连接

在 Python 数据流/Apache Beam 上启动 CloudSQL 代理