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;
}
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With