Nginx RegEx 对多个站点进行重写或内部重定向循环
Nginx RegEx to multiple sites make rewrite or internal redirection cycle
我正在尝试将我的应用程序从 Apache 迁移到 Nginx。首先,我有 mod_rewrite 模块,可以像这样自动将我的应用程序重定向到 index.php 页面:
http://127.0.0.1/fwsibe/acesso <- URL displayed
http://127.0.0.1/fwsibe/index.php/acesso <- url accessed
所以,我在我的位置使用带有别名的 try_files 来自动重定向页面并且一切正常,但我有必要为每个应用程序设置一个唯一的位置,使用 "sibe" 在 RegEx 上捕获所有站点。
每个应用的一个位置有效:
location /fwsibe/ {
alias /var/www/fwsibe/;
try_files $uri $uri/ /fwsibe/index.php;
}
location /sibe/ {
alias /var/www/sibe/;
try_files $uri $uri/ /sibe/index.php;
}
location /portalsibe/ {
alias /var/www/portalsibe/;
try_files $uri $uri/ /portalsibe/index.php;
}
location /sibemovel/ {
alias /var/www/sibemovel/;
try_files $uri $uri/ /sibemovel/index.php;
}
但是,当我尝试使用 RegEx 为所有 "sibe" 应用设置唯一位置时,出现 "rewrite or internal redirection cycle while internally" 错误。
location ~ /([a-z]*sibe[a-z]*)/ {
set $subdomain ;
alias /var/www/$subdomain/;
try_files $uri $uri/ /$subdomain/index.php;
}
这与 try_files
的工作方式有关 — http://nginx.org/r/try_files — 最后一个参数只是指向指定 URL:
的内部重定向
If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
据推测,您希望您的 try_files 会涉及 .php
处理程序,但是在随后的 location
搜索中发生的情况是您最终处于与您完全相同的状态开始于.
根据 http://nginx.org/r/location,位置指令的顺序很重要——第一个匹配的正则表达式获胜:
Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used.
因此,您希望将 .php
放在 site-based 处理程序之前。
我正在尝试将我的应用程序从 Apache 迁移到 Nginx。首先,我有 mod_rewrite 模块,可以像这样自动将我的应用程序重定向到 index.php 页面:
http://127.0.0.1/fwsibe/acesso <- URL displayed
http://127.0.0.1/fwsibe/index.php/acesso <- url accessed
所以,我在我的位置使用带有别名的 try_files 来自动重定向页面并且一切正常,但我有必要为每个应用程序设置一个唯一的位置,使用 "sibe" 在 RegEx 上捕获所有站点。
每个应用的一个位置有效:
location /fwsibe/ {
alias /var/www/fwsibe/;
try_files $uri $uri/ /fwsibe/index.php;
}
location /sibe/ {
alias /var/www/sibe/;
try_files $uri $uri/ /sibe/index.php;
}
location /portalsibe/ {
alias /var/www/portalsibe/;
try_files $uri $uri/ /portalsibe/index.php;
}
location /sibemovel/ {
alias /var/www/sibemovel/;
try_files $uri $uri/ /sibemovel/index.php;
}
但是,当我尝试使用 RegEx 为所有 "sibe" 应用设置唯一位置时,出现 "rewrite or internal redirection cycle while internally" 错误。
location ~ /([a-z]*sibe[a-z]*)/ {
set $subdomain ;
alias /var/www/$subdomain/;
try_files $uri $uri/ /$subdomain/index.php;
}
这与 try_files
的工作方式有关 — http://nginx.org/r/try_files — 最后一个参数只是指向指定 URL:
If none of the files were found, an internal redirect to the uri specified in the last parameter is made.
据推测,您希望您的 try_files 会涉及 .php
处理程序,但是在随后的 location
搜索中发生的情况是您最终处于与您完全相同的状态开始于.
根据 http://nginx.org/r/location,位置指令的顺序很重要——第一个匹配的正则表达式获胜:
Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used.
因此,您希望将 .php
放在 site-based 处理程序之前。