2
votes

I'm setting up a Phoenix app that is serving more than one subdomain from the same endpoint. All it does is use a plug to select a different router at the end of the endpoint plugs depending on the subdomain of the request. That's all working OK.

The issue is with the force_ssl configuration.

My endpoint configuration looks like the following:

config :frontend, Frontend.Endpoint,
  on_init: {Frontend.Endpoint, :load_from_system_env, []},
  url: [scheme: "https", host: "myapp.com", port: 443],
  force_ssl: [rewrite_on: [:x_forwarded_proto]],
  cache_static_manifest: "priv/static/cache_manifest.json"

The host has to just be myapp.com as this endpoint configuration applies to all the subdomains.

This configuration is unfortunately causing all requests to foo.myapp.com (both HTTP and HTTPS) to be 301 redirected to https://myapp.com.

The behavior I want would be to 301:

  • http://foo.myapp.com to https://foo.myapp.com
  • http://bar.myapp.com to https://bar.myapp.com

Requests to https://foo.myapp.com and https://bar.myapp.com should not be getting redirected.

Help would be appreciated! If it is relevant, I'm on Phoenix 1.3.

1

1 Answers

2
votes

From the SSL plug source:

To dynamically redirect to the host of the current request, :host must be set nil.

Looks like:

config :myapp, MyApp.Endpoint,
  force_ssl: [
    host: nil,
    rewrite_on: [:x_forwarded_proto]
  ]

Though, using a proxy (i.e., NGINX) will provide far more granular control.