Regex Nginx正则表达式失败
当我访问或仍然访问handleURL.html时 这就是我所拥有的Regex Nginx正则表达式失败,regex,nginx,Regex,Nginx,当我访问或仍然访问handleURL.html时 这就是我所拥有的 server { listen 80; listen [::]:80; root /var/www/vhosts/example.com/public; index index.html; server_name example.com www.example.com; access_log /var/log/nginx/example.com/access.log;
server {
listen 80;
listen [::]:80;
root /var/www/vhosts/example.com/public;
index index.html;
server_name example.com www.example.com;
access_log /var/log/nginx/example.com/access.log;
error_log /var/log/nginx/example.com/error.log;
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1;
rewrite "^/.{10,500}$" /handleURL.html;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
如果url的字符数在10500之间,我想显示不同的html文件。Nginx可能不在乎,但看起来正则表达式不应该用引号括起来。除非您打算在替换url中使用输入的字符串,否则您不需要捕获它,因此括号是不必要的
如果要为每个字符串选择不同的文件,可以在替换字符串中使用捕获的字符串。e、 g.rewrite“^/(.{10500})$”/wiki/$1 last
将匹配example.com/agreatpage.html
并将其转换为example.com/wiki/agreatpage.html
只需根据长度重定向到另一个页面,比如rewrite“^/{10500}$”/redirect.html last代码>应该可以工作
参考资料:
编辑:
进一步阅读后,如果正则表达式用双引号括起来,nginx是可以的,如果表达式包含{或},则必须这样做,否则解析器会将这些错误解释为打开块和关闭块
Edit2:
我目前看到的唯一问题是:
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1;
rewrite "^/.{10,500}$" /handleURL.html;
据我所知,应该是:
rewrite "^/([a-z0-9]{4,8})$" /forward.php?shortcode=$1 last;
rewrite "^/.{10,500}$" /handleURL.html last;
解决这个问题的唯一方法是删除所有重写并添加index.php并将所有内容传递给index.php,最后我完成了以下配置
server {
listen 80;
listen [::]:80;
root /var/www/vhosts/example.com/public;
index index.php index.html;
server_name example.com www.example.com;
log_format compression '$host - $remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$gzip_ratio"';
access_log /var/log/nginx/example.com/access.log compression;
error_log /var/log/nginx/example.com/error.log;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
谢谢你们 我的意思是,如果我在/10-500个字符之后有任何内容,则显示handleURL.html这不是正则表达式模式的工作方式。这也不是OP要求的。很抱歉,我想我写答案时有点迷糊。已修复。@Ersin Demirtas您是否尝试删除引号以查看它是否改变了什么?我猜这只是一些有趣的处理过程。当你不完全按照nginx的要求去做时。rewrite^/{10500}$/handleURL.html last
2014/09/07 21:57:25[emerg]3327#0:指令“重写”的结尾不是“;”in/etc/nginx/sites enabled/example.com:18我的意思是,如果我在/if之后有任何内容,如果它在10-500个字符之间,那么就显示handleURL.html。我已经用它的完美特性测试了正则表达式,但出于某种原因,它在服务器上不起作用。