1

I have a standard Rails app deployed to Heroku. I am using custom buildpacks to install nginx so I can create some rewrite rules and a reverse proxy. I have it mostly working save for one issue.

I have the following location definition to proxy pass all urls starting with /blog/ to another app.

location ~* ^/blog/?(.*) {
  set $forward_host       'another.app.com';
  set $url_full           '$1';

  resolver                8.8.8.8 valid=300s;
  resolver_timeout        10s;

  # always add trailing slash
  rewrite ^([^.]*[^/])$   $1/ permanent;

  index index.html;

  proxy_hide_header       Set-Cookie;
  proxy_set_header        X-Real-IP $remote_addr;
  proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header        X-Scheme $scheme;
  proxy_set_header        Host $forward_host;
  proxy_ignore_headers    "Set-Cookie";
  proxy_buffering         off;
  proxy_intercept_errors  on;
  proxy_redirect          off;
  proxy_pass              http://$forward_host/$url_full;
}

I want all my URL's to end with a trailing slash, which is why I added:

rewrite ^([^.]*[^/])$   $1/ permanent;

Everything works fine when I hit:

http://nginx-playground.herokuapp.com/blog/

But when I don't have the trailing slash and the rewrite rule kicks in, the proxy_pass adds the internal Heroku PORT number to the url and it looks something like this:

http://nginx-playground.herokuapp.com:27348/blog/

I've tried many different things, like setting proxy_redirect but haven't been able to figure it out.

Here is a link to the github project of the sample app I've setup. You can fork/clone it and try deploying to heroku to see what happens for yourself.

https://github.com/WeConnect/nginx-playground

You will need the following ENV vars if you do:

BUILDPACK_URL: https://github.com/ddollar/heroku-buildpack-multi.git
LANG:          en_US.UTF-8
RACK_ENV:      production

This is the blog article I used as a staring point: http://blog.codeship.com/how-to-deploy-nginx-on-heroku/

Any help or clues would be greatly appreciated.

Ramin B.
  • 11
  • 1

0 Answers0