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

送分小仙女□ 提交于 2019-12-23 09:31:46

问题


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;
}

回答1:


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;
}



回答2:


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.



来源:https://stackoverflow.com/questions/41605137/how-to-remove-a-subdirectory-from-the-url-before-passing-to-a-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!