Skip to content

Instantly share code, notes, and snippets.

@namnv609
Last active February 12, 2019 21:13
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save namnv609/a255f5c69018e7d15421f749a4abd5d4 to your computer and use it in GitHub Desktop.
Save namnv609/a255f5c69018e7d15421f749a4abd5d4 to your computer and use it in GitHub Desktop.
ActionCable WebSocket with Apache2 proxy
<VirtualHost *:80 *:443>
# Domain
ServerName servername.domain
SSLEngine On
SSLProxyEngine On
# Path to SSL CRT file
SSLCertificateFile /etc/apache2/ssl/apache.crt
# Path to SSL KEY file
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
ProxyRequests Off
ProxyPreserveHost On
<Location />
ProxyPass http://0.0.0.0:3000/
ProxyPassReverse http://0.0.0.0:3000/
</Location>
<Location /cable>
ProxyPass ws://0.0.0.0:3000/cable
ProxyPassReverse ws://0.0.0.0:3000/cale
</Location>
</VirtualHost>
@namnv609
Copy link
Author

  • Requirements:
    • httpd 2.4.5 and later
  • Enable Apache2 modules:
    • sudo a2enmod proxy proxy_ajp proxy_http rewrite deflate headers proxy_balancer proxy_connect proxy_html proxy_wstunnel

@ForeverYoung1208
Copy link

ForeverYoung1208 commented Dec 26, 2017

Doesn't work for me (((
I've got
"WebSocket connection to 'wss://site.domain/cable' failed: Connection closed before receiving a handshake response"

Can you help me? I've already broken my mind with it ((

<VirtualHost site.domain:*>
    ServerName site.domain

    SSLEngine on
    SSLProxyEngine on                           # make sure apache knows SSL is okay to proxy

    SSLCertificateFile /хххct.crt
    SSLCertificateKeyFile /ххct.key

    RequestHeader set X_FORWARDED_PROTO 'https'

    ProxyRequests Off
    ProxyPreserveHost On

  <Location />
    ProxyPass http://127.0.0.1:3001/
    ProxyPassReverse http://127.0.0.1:3001/
  </Location>
  <Location /cable>
    ProxyPass wss://127.0.0.1:3001/cable
    ProxyPassReverse wss://127.0.0.1:3001/cable
  </Location>
</VirtualHost>

I'm stuck (((((((

@thadeu
Copy link

thadeu commented Dec 27, 2017

@ForeverYoung This code solved for me.

<VirtualHost *:80  *:443>
  ServerName api.mysite.com.br
  ServerAlias app.mysite.com.br

  DocumentRoot "/webapp/mysite/public"

  #SSLEngine On
  #SSLProxyEngine On

  ProxyRequests Off
  ProxyPreserveHost on

  <Location />
    ProxyPass http://0.0.0.0:3000/
    ProxyPassReverse http://0.0.0.0:3000/
  </Location>
 
 # ----------------------------------------
 # difference
 # host.com.br instead of 0.0.0.0:3000
  <Location /cable>
    ProxyPass ws://mysite.com.br:3000/cable
    ProxyPassReverse ws://mysite.com.br:3000/cable
  </Location>
 # ----------------------------------------

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  <Directory "/webapp/mysite/public">
        Options FollowSymlinks Multiviews
        AllowOverride All
        Allow from all
        Require all granted
        RailsEnv production
  </Directory>

  #SSLCertificateFile /etc/letsencrypt/live/mysite.com.br/fullchain.pem
  #SSLCertificateKeyFile /etc/letsencrypt/live/mysite.com.br/privkey.pem
  #Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment