Skip to content

Drab in production and behind a proxy

Tomek Gryszkiewicz edited this page Jun 13, 2018 · 10 revisions

When using in production, an app is often behind an apache/nginx server for domain virtualization or balancing, so the external port (80) is different from the actual app port (i.e. 4000). The necessary mapping between the two ports is usually done by configuring a proxy, but a particularly care have to be taken to correctly handle websocket calls, as they are at the core of Drab mechanism to communicate between the client browser and the backend server.

Here there are some configuration examples for nginx and apache:

NGINX

location / {
    include proxy.conf;
    proxy_http_version 1.1;
    proxy_redirect off;
    proxy_pass http://drab;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-Cluster-Client-Ip $remote_addr;

    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

APACHE

  1. Load the ws tunnel module adding this directive in /etc/httpd/conf/httpd.conf:

LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so

  1. set up a proxy for both http (and/or https) and ws adding the following directives inside the virtualhost block for your domain in the /etc/httpd/conf/httpd.conf file:
ProxyPass /socket/ ws://example.com:4000/socket/
ProxyPassReverse /socket/ ws://example.com:4000/socket/
ProxyPass / http://example.com:4000/
ProxyPassReverse / http://example.com:4000/
  1. being sure to write the directives in the correct order:

The configured ProxyPass and ProxyPassMatch rules are checked in the order of configuration. The first rule that matches wins. So usually you should sort conflicting ProxyPass rules starting with the longest URLs first.

  1. restart apache: # apachectl restart

Phoenix config prod.exs

Phoenix checks the origin of request when the origin header is present. It is compared against the host value in YourApp.Endpoint.config(:url)[:host], so please ensure that the correct host is set in prod.exs:

config :your_app, url: [host: "myhost.com", port: 443]