{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"never","results":{"codes":[]},"settings":"","params":[]},"next":{"description":"","pages":[]},"title":"Phoenix Behind a Proxy","type":"basic","slug":"serving-your-application-behind-a-proxy","excerpt":"","body":"In order to serve an application behind a proxy webserver such as `nginx` or `apache`, we will need to configure a specific port for our application to listen on. This will ensure the url helper functions will use the correct proxy port number.\n\nThere are two ways we can approach this. If we are sure that we can pick a port number which will not need to change, we can hard-code it as `http: [port: 8080]` line of our `config/prod.exs` file.\n\n```elixir\nuse Mix.Config\n\n. . .\n\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  http: [port: 8080],\n  url: [host: \"example.com\"],\n  cache_static_manifest: \"priv/static/manifest.json\"\n\n. . .\n```\n\nIf we need our port configuration to be flexible, perhaps even change for every host we deploy do, we can get the port value from an existing environment value set on the system. Again, here is our `config/prod.exs` file.\n\n```elixir\nuse Mix.Config\n\n. . .\n\nconfig :hello_phoenix, HelloPhoenix.Endpoint,\n  http: [port: {:system, \"PORT\"}],\n  url: [host: \"example.com\"],\n  cache_static_manifest: \"priv/static/manifest.json\"\n\n. . .\n```","updates":[],"order":8,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"559218ad1da5250d001e9678","__v":0,"category":{"sync":{"isSync":false,"url":""},"pages":["559218ad1da5250d001e9675","559218ad1da5250d001e9676","559218ad1da5250d001e9677","559218ad1da5250d001e9678","559218ad1da5250d001e9679","559218ad1da5250d001e967a","559218ad1da5250d001e967b","559218ad1da5250d001e967c","559218ad1da5250d001e967d","55921ed65068e60d002ba044"],"title":"Bonus Guides","slug":"bonus-guides","order":4,"from_sync":false,"reference":false,"_id":"559218ad1da5250d001e9672","version":"559218ac1da5250d001e966f","__v":2,"createdAt":"2014-12-03T21:36:49.014Z","project":"54348ec95b10711400c6c445"},"createdAt":"2014-12-03T22:14:34.391Z","project":"54348ec95b10711400c6c445","githubsync":"","user":"5435e00ad7d8700800bbec51","version":{"version":"0.14.0","version_clean":"0.14.0","codename":"","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":false,"categories":["559218ad1da5250d001e9670","559218ad1da5250d001e9671","559218ad1da5250d001e9672","559218ad1da5250d001e9673","559218ad1da5250d001e9674"],"_id":"559218ac1da5250d001e966f","releaseDate":"2015-06-30T04:18:52.132Z","__v":1,"createdAt":"2015-06-30T04:18:52.132Z","forked_from":"5558c642eb56ae2f00f714fc","project":"54348ec95b10711400c6c445"}}

Phoenix Behind a Proxy


In order to serve an application behind a proxy webserver such as `nginx` or `apache`, we will need to configure a specific port for our application to listen on. This will ensure the url helper functions will use the correct proxy port number. There are two ways we can approach this. If we are sure that we can pick a port number which will not need to change, we can hard-code it as `http: [port: 8080]` line of our `config/prod.exs` file. ```elixir use Mix.Config . . . config :hello_phoenix, HelloPhoenix.Endpoint, http: [port: 8080], url: [host: "example.com"], cache_static_manifest: "priv/static/manifest.json" . . . ``` If we need our port configuration to be flexible, perhaps even change for every host we deploy do, we can get the port value from an existing environment value set on the system. Again, here is our `config/prod.exs` file. ```elixir use Mix.Config . . . config :hello_phoenix, HelloPhoenix.Endpoint, http: [port: {:system, "PORT"}], url: [host: "example.com"], cache_static_manifest: "priv/static/manifest.json" . . . ```