我对Nginx很新,所以请耐心等待.
我正在尝试将所有请求从一个子目录(存储)重定向到另一个子目录(交易)的根目录.请参阅下面的进度.目标子目录(交易)中的站点是一个magento站点,因此这是当前大多数规则的用途.
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; } }
我用来重定向的部分如下:
location ~ /store { rewrite /trade permanent; }
这适用于example.com/store,但不适用于example/store/index.php或任何其他带有args的uri.我有一种感觉,底部的php文件部分覆盖了处理.这就是为什么我把〜放在商店位置的前面,因为这里的文档声明这将首先处理.处理是停止还是继续?
我已经阅读了关于嵌套php规则的内容,但我试过这个无济于事.
我非常感谢任何帮助.
好,
回到这一点,我可以看到问题。
在Nginx中,当在位置指令前加上〜时,这意味着您要在指令中处理正则表达式(区分大小写,〜*表示不区分大小写)。我相信所有正则表达式指令都将在其他任何指令之前进行处理,但我有待纠正。
因此,当我使用时:
location ~/store { rewrite /trade permanent; }
那里没有正则表达式。它只是匹配/ store并重定向到贸易。
经过一番调查(并完善了我的正则表达式(垃圾)),我回到了书上,并提出了一个可行的解决方案。
location ~ ^/store/(.*) { rewrite ^/store(.*) /trade permanent; }
在这里,我要求指令通过输入〜来处理正则表达式,然后将任何与/ store /匹配的网址匹配。
然后,根据文档,重写语法为:
重写正则表达式替换[标志]
所以我将所有URL与其中的store匹配,并将它们永久重定向到新的子文件夹。
真的很尴尬,所以实际上,但是嘿,每天都是上学的日子。我愿意对所有这些问题进行纠正,希望它能对某人有所帮助。
好吧尝试这样的事情
location ^~ /store(.*) { return 301 $scheme://$http_host/trade$1$is_args$query_string; }
试图尽可能避免使用硬编码的东西并使用return,因为它更喜欢永久性的重写