kickinespresso

kickinespresso

Nginx reverse proxy with Phoenix and Let's Encrypt SSL config

I’m trying to run Phoenix 1.5 with Nginx reverse proxy with Let’s Encrypt SSL and having little success with the configuration. I’m using edeliver to deploy, which seems to be working fine. This may be because the tutorials or configs I’m using are outdated, but it seems to be right from my end. I started with these tutorials Setting up Phoenix…2016, Nginx config w/ phx

This is what I have for my Nginx config (gist link):


upstream phoenix_upstream {
  server 127.0.0.1:4000;
}

server {
  listen 80;
  server_name espresso.kickinespresso.com;
  return 301 https://$server_name$request_uri;
}

server { 
  listen [::]:443 ssl ipv6only=on; # managed by Certbot
  listen  443 ssl;
  server_name espresso.kickinespresso.com;
  
  ssl_certificate /etc/letsencrypt/live/espresso.kickinespresso.com/fullchain.pem; # managed by Certbot
  ssl_certificate_key /etc/letsencrypt/live/espresso.kickinespresso.com/privkey.pem; # managed by Certbot
  include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
  ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

  location / {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://phoenix_upstream;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }
}

Here is my config/prod.exe for the endpoint (gist link:

config :espresso, EspressoWeb.Endpoint,
  http: [port: 4000],
  check_origin: false,
  cache_static_manifest: "priv/static/cache_manifest.json",
  server: true,
  code_reloader: false

I’m also not sure if I should have other configurations in the Endpoint for URL or port. I’ve seen configurations where the url is specified like this: url: [host: "espresso.kickinespresso.com", port: 443], but if it is a proxy_pass from nginx shouldn’t this not matter? I’ve also seen configurations where the IP is specified for localhost like http: [ip: {127, 0, 0, 1}, port: 4000],

I’m at a bit of a loss as of how to troubleshoot this since I can’t really find any logs from nginx in /var/log/nginx/ that show any issues. The erlang log /home/espresso/app_release/espresso/var/log also isn’t providing anything back.

Has anyone done anything similar?

Most Liked

derek-zhou

derek-zhou

I just did the same thing so memory is still fresh. A couple of things:

you are missing:

proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;

They are important to make Plug.SSL work, which you should add to your endpoint:

if Mix.env == :prod do
    plug Plug.SSL, rewrite_on: [:x_forwarded_proto, :x_forwarded_host, :x_forwarded_port]
  end

Secondly, if you use web socket or the built in live-dashboard, you need to turn on websocket for those end points, such as:

location /live {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “upgrade”;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;
proxy_pass http://localhost:4001/live;
proxy_read_timeout 6000s; # raise the proxy timeout for the websocket
}

Lastly, you still need to make sure your endpoint has the proper external url. the url is used to generate full url in your application, such as email links, from the router helper functions.

DiodonHystrix

DiodonHystrix

  http: [:inet6, port: 4000],
  url: [host: "domain", port: 443],

I have it done this way

derek-zhou

derek-zhou

See the above for the reverse proxy config. For endpoint config you can check this out:

https://github.com/derek-zhou/liv

Please note this application is intended to be deployed in a sub directory, so there is some special handling of LV socket location. Other than that, it is bog standard.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement