简体   繁体   English

Nginx重写一些匹配规则不起作用

[英]Nginx rewrite some matching rules are not working

I want to move from these working rules 我想摆脱这些工作规则

location = /contact {
  rewrite ^(.*)$ /index.php?v=contact last; break;
}

location = /terms {
  rewrite ^(.*)$ /index.php?v=terms last; break;
}

location = /faq {
  rewrite ^(.*)$ /index.php?v=faq last; break;
}

location = /twitter {
  rewrite ^(.*)$ /index.php?v2=twitter last; break;
}

location = /facebook {
  rewrite ^(.*)$ /index.php?v2=facebook last; break;
}

location = /login {
  rewrite ^(.*)$ /index.php?v2=login last; break;
}

location = /privacy {
  rewrite ^(.*)$ /index.php?v=privacy last; break;
}

to something like this 像这样

location / {
  try_files $uri $uri/ =404;
  rewrite ^/(contact|privacy|terms|faq)$ /index.php?v=$1 last;
  rewrite ^/(twitter|facebook|login)$ /index.php?v2=$1 last; break;
}

But the thing is that 'contact','terms','privacy','twitter','facebook' pages are working correctly but 'privacy' and 'login' pages are throwing 404 error . 但事实是,“联系”,“条款”,“隐私”,“推特”,“ facebook”页面正常工作,但“隐私”和“登录”页面却抛出404错误

There are no other rewrite rules involving 'login' and 'privacy' 没有其他涉及“登录”和“隐私”的重写规则

Actually i don't like neither of the methods, it might be working but it's not really the best way to write it, so lets try something different. 实际上,我既不喜欢这两种方法,也可能有用,但是它并不是真正的最佳编写方法,因此请尝试其他方法。

location *~ ^/(contact|privacy|terms|faq)/?$ {
    try_files $uri $uri/ /index.php?v=$1;
}
location *~ ^/(twitter|facebook|login)/?$ {
    try_files $uri $uri/ /index.php?v2=$1;
}
location / {
    try_files $uri $uri/ /index.php;
}

o and I never heard of a last; break; o,我从没听说过last; break; last; break; it's probably working only because nginx is ignoring the last part of it, it's either a last or a break , 之所以可行,可能是因为nginx忽略了它的最后一部分,它要么是last一个,要么是一个break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM