Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a subdirectory from the url before passing to a script?

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:

  • http://store.example.com/items/1
  • http://backoffice.example.com/accounts/1234

new urls:

  • http://www.example.com/store/items/1
  • http://www.example.com/backoffice/accounts/12345

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;
}
like image 216
101010 Avatar asked Jan 12 '17 04:01

101010


2 Answers

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;
}
like image 66
Dayo Avatar answered Nov 19 '22 03:11

Dayo


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.

like image 36
Nikolay Dimitrov Avatar answered Nov 19 '22 02:11

Nikolay Dimitrov