Skip to content

Instantly share code, notes, and snippets.

@geekforbrains
Last active November 5, 2020 04:37
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekforbrains/01df7c31fd62f84f453b8bc22217f3c6 to your computer and use it in GitHub Desktop.
Save geekforbrains/01df7c31fd62f84f453b8bc22217f3c6 to your computer and use it in GitHub Desktop.
Nginx reverse proxy for Heroku
upstream heroku {
server myapp.herokuapp.com;
}
server {
server_name ~^(www\.)(?<domain>.+)$;
return 301 $scheme://$domain$request_uri;
}
server {
listen 80 default;
listen 443 ssl;
#server_name _;
ssl off;
ssl_certificate /etc/nginx/ssl/myapp.com.crt;
ssl_certificate_key /etc/nginx/ssl/myapp.com.key;
location ~ \.php$ {
return 444;
}
location / {
proxy_pass http://heroku;
proxy_redirect off;
proxy_read_timeout 5m;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto http;
}
}
@geekforbrains
Copy link
Author

This is so you can use a custom domain with an A record as apposed to an ALIAS or CNAME. This can be helpful when building a product that allows an end user to map their own custom domain. An A record is much easier for them to do (and doesn't force www.)

@george-i
Copy link

george-i commented Dec 11, 2018

I'm trying to serve two nodejs apps on Heroku.
One on app.example.com and one on example.com.
I setup the DNS via cloudflare correctly (but I've used CNAME).
I've added nginx from https://elements.heroku.com/buildpacks/heroku/heroku-buildpack-nginx
I'm stuck with configuring the nginx file (first time when touching it). I feel that I'm close, but I'm not sure where to place this code, as yours.

@mandarvaze
Copy link

@george-i As per the link :

You can provide your own NGINX config by creating a file named nginx.conf.erb in the config directory of your app

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