如何在传递给脚本之前从url中删除子目录? [英] How to remove a subdirectory from the url before passing to a script?

查看:92
本文介绍了如何在传递给脚本之前从url中删除子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO之前已问过,但答案尚未概括.这样吧.

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

我有两个不同的Web应用程序.它们被设置为位于根目录的不同服务器上.但是现在它们将被放置在同一服务器上的子目录中.下面的脚本将子目录和URI发送到脚本.这是个问题.

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.

旧网址:

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

新网址:

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

商店站点只想看到/items/1时就会看到/store/items/1.后台站点也是如此.

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

我的尝试:

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

同样,商店站点正在获取带有/store前缀的URL,而后台办公室正在获取/backoffice.这些站点被编码为不希望使用这些前缀.在发送到实际站点之前,我需要删除/store/backoffice前缀.

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.

这是重写规则吗?

我根据此SO页面进行了尝试,但没有工作.我一定不了解语法.

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

更新

显然,这可行(添加了break;),这是一个完整的解决方案吗?

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

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

推荐答案

您需要重写uri,而不会触发使用break指出的重定向.

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

根据设置的细节,是否一切都应放在后端,还是Nginx是否应满足某些请求(如静态文件),您将需要...

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

...或

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

这篇关于如何在传递给脚本之前从url中删除子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆