19

This has been asked on SO before but the answers have not been generalized. So here goes.

I have two different web apps. They were made to be on different servers at the root. But now they are going to be placed into subdirectories on the same server. The script below is sending the subdirectory along with the URI's to the scripts. This is a problem.

old urls:

new urls:

The store site is seeing /store/items/1 when it wants to just see /items/1. The same goes for the backoffice site.

My attempt:

    location /store {
            try_files @store_site;
    }

    location /backoffice {
            try_files @backoffice_site;
    }

    location @store_site {
            include uwsgi_params;
            uwsgi_pass unix:/var/run/uwsgi/store_site.sock;
            proxy_set_header Host $host;
    }

    location @backoffice_site {
            include uwsgi_params;
            uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock;
            proxy_set_header Host $host;
    }

Again, the store site is getting URLs with the /store prefix and backoffice is getting /backoffice. Those sites were coded to not expect those prefixes. I need to remove the /store and /backoffice prefix before sending to the actual site.

Is this a rewrite rule thing?

I tried this based on this SO page and it didn't work. I must not understand something about the syntax.

location /store {
        rewrite  ^/store/(.*) /$1;
        try_files $uri @store_site;
}

Update

Apparently, this works (added break;) Is this a complete solution?

location /store {
        rewrite  ^/store/(.*) /$1 break;
        try_files $uri @store_site;
}
Community
  • 1
  • 1
101010
  • 14,866
  • 30
  • 95
  • 172

2 Answers2

20

You need to rewrite the uri without triggering a redirection as noted by using break.

Depending on the details of your set up, whether everything should go to the backend or whether Nginx should serve some requests such as static files, you will need either ...

location /store {
    try_files $uri @store_site;
}

location /backoffice {
    try_files $uri @backoffice_site;
}

location @store_site {
    rewrite  ^/store/(.*) /$1 break;
    include uwsgi_params;
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock;
}

location @backoffice_site {
    rewrite  ^/backoffice/(.*) /$1 break;
    include uwsgi_params;
    uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock;
}

... or

location /store {
    rewrite  ^/store/(.*) /$1 break;
    include uwsgi_params;
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock;
}

location /backoffice {
    rewrite  ^/backoffice/(.*) /$1 break;
    include uwsgi_params;
    uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock;
}
Dayo
  • 12,413
  • 5
  • 52
  • 67
8

I suggest you try this for an item:

location /store/ {
    rewrite  ^/store/(.*) /$1 break;
    include uwsgi_params;
    proxy_set_header Host $host;
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock;
}

Haven't tested it but I think it should work.

Nikolay Dimitrov
  • 1,766
  • 1
  • 14
  • 23
  • 1
    I'd like to avoid mentioning the top-level urls from the store in here. It's because there are a number of them and I'd end up enumerating them all. Is there no way to just snip off the /store part and send along the rest? – 101010 Jan 12 '17 at 10:35
  • 1
    I see. I have changed it slightly, but definitely the store location should have / slashes on both sides, otherwise /storeajshas will match too – Nikolay Dimitrov Jan 13 '17 at 09:09