简体   繁体   中英

Trying to run Faye websocket on Apache using a proxy

I'm trying to use Faye gem on thin server with Rails in a production environment but I cannot make it work. I'm using apache 2.4 and ws_tunnel also proxy modules are enabled. My setup is like below.

Apache configuration

<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 file

require 'faye'

Faye::WebSocket.load_adapter('thin')

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

run app

And I'm starting faye with the following command

rackup faye.ru -E production -s thin 

This way I can reach the faye.js by typing the following url from the browser.

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

But When I type https://mysite.example.com/faye/ for example, I get the following result

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>

which is strange because I only get "Sure you're not looking for /faye ?" in the development.

I'm calling the Faye in application.js as

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

And I get an error like

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

I also tried to use the wss protocol in the proxy settings and also giving my certificate paths to thin to start with SSL but nothing worked.

I assume this may be happening because of the proxy or the ws_tunnel module because when I run the following command from the shell in the server

curl http://localhost:9292

I get only "Sure you're not looking for /faye ?" message.

Does anyone came accross something like this?

I would appreciate if someone could help me.

Thanks in advance.

For those who's having the same issue, my advice is to no matter which version of Apache you are using, just change it to nginx without any hesitation. It's been very easy to configure and run it.

I'm using the below configuration for my app named example. After setting up nginx correctly, everything else worked quite well.

hope this helps someone

# 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM