Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In the near future I'm going to have 3 nginx servers. One is a reverse proxy for SSL for the two others. So, for example, I go to:

The two other servers in this example are site1 and site2. I installed the SSL certificate on the proxy and I want to use a reverse proxy (SSL not required since all 3 are on an internal network).For testing purposes I'm having nginx listen on 443 for SSL/reverse-proxy, listen on port 8081 which is a rails app for site1, and 8082 is for site2.

I have this...

server {
    listen                  443;
    server_name   ;

    ssl                     on;
    ssl_certificate         ssl/;
    ssl_certificate_key     ssl/;
    keepalive_timeout       60;

    location /site1 {
        proxy_pass http://localhost:8081;
        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_redirect http:// https://;

    location /site2 {
        proxy_pass http://localhost:8082;
        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_redirect http:// https://;

So when I visit I want it to basically return what would normally come from localhost:8081 (or later down the line an internal IP for another server).

Is there a way to have it strip the "site1" from the localhost call? What it seems to be doing is using localhost:8081/site1. The site1 and site2 sites are of the nature "/login/index" or "/whatever/list", etc, with no "site1".

There are also redirects in the site controllers (using redirect_to) that go from things like /login/index to /whatever/list.

Am I going to have to redesign the URL's of the site to use site1? Or can the NGINX proxy figure it out?


share|improve this question
up vote 20 down vote accepted


If proxy_pass is specified with URI, when passing a request to the server, part of a normalized request URI matching the location is replaced by a URI specified in the directive:

location /name/ {

That is, you have to use proxy_pass like this:

location /site1/ {
    proxy_pass http://localhost:8081/;

Note the trailing / in the proxy_pass directive - it will replace part of the original URI matched by the location, i.e. /site1/.

share|improve this answer
All it took was a trailing slash to fix the problem!? Thanks for that. Now to figure out how I'm going to handle the redirect_to - but that's not a server question so I'll figure that out elsewhere. – Chromag Nov 2 '12 at 13:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.