Nginx将所有请求从子目录重定向到另一个子目录根 [英] Nginx redirect all requests from subdirectory to another subdirectory root

查看:2170
本文介绍了Nginx将所有请求从子目录重定向到另一个子目录根的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Nginx还是很陌生,所以请多多包涵.

I'm quite new to Nginx so please bear with me.

我正在尝试将所有请求从一个子目录(存储)重定向到另一子目录(交易)的根目录.请在下面查看我的进度.目标子目录(贸易)中的站点是magento站点,因此这是当前大多数规则的目的.

I'm trying to redirect all requests from one subdirectory (store) to the root of another subdirectory (trade). See my progress below. The site in the target subdirectory (trade) is a magento site so that is what most of the current rules are for.

server {
    server_name example.com *.example.com;
    root /usr/share/nginx/html/example.com/public_html;
    index index.php index.html index.htm;

    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;

    location / {
            try_files $uri $uri/ /index.html;
    }

    location /trade/ {
            index index.html index.php;
            try_files $uri $uri/ @handler;
            expires 30d;
    }

    location ~ /store {
            rewrite /trade permanent;
    }

    location ~ ^/trade/(app|includes|lib|media/downloadable|pkginfo|report/config.xml|var)/ { internal; }
    location /trade/var/export/ { internal; }
    location /. { return 404; }
    location @handler { rewrite / /trade/index.php; }

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
          root /usr/share/nginx/html;
    }

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;

    }


}

我用于重定向的部分如下:

The section I am using to redirect is the following:

location ~ /store {
        rewrite /trade permanent;
}

这适用于example.com/store,但不适用于example/store/index.php或其他带有args的uri.我觉得底部的php文件部分覆盖了该处理过程.这就是为什么我在文档的此处中将〜放在商店位置的前面表示此操作将首先被处理的原因.处理是停止还是继续?

This works for example.com/store but not example/store/index.php or any other uri with args. I have a feeling that the php file section at the bottom is overriding the processing. That is why I have put the ~ in front of the store location as the documentation here states this will be processed first. Does the processing stop or continue on?

我已经阅读过有关嵌套php规则的信息,但是我尝试了一下却无济于事.

I have read about nesting a php rule but I have tried this to no avail.

非常感谢您的帮助.

推荐答案

确定尝试类似的操作

location ^~ /store(.*) {
  return 301 $scheme://$http_host/trade$1$is_args$query_string;
}

尝试尽可能避免使用硬编码的内容并使用return,因为与永久重写相比,它更受欢迎

Trying to avoid hardcoded stuff as much as possible and using return because it's prefered over permanent rewrites

这篇关于Nginx将所有请求从子目录重定向到另一个子目录根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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