【问题标题】:Nginx rewrite some matching rules are not workingNginx rewrite 一些匹配规则不起作用
【发布时间】:2013-07-20 12:39:11
【问题描述】:

我想摆脱这些工作规则

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;
}

这样的事情

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;
}

但问题是“联系人”、“条款”、“隐私”、“推特”、“脸书”页面工作正常,但 “隐私”和“登录”页面抛出 404 错误.

没有其他涉及“登录”和“隐私”的重写规则

【问题讨论】:

  • 建议打开调试日志找出两个404 url​​的踪迹。
  • @TroyCheng open() "/usr/share/nginx/www/privacy" failed (2: No such file or directory) 所以它根本不是重写
  • 我的意思是当打开调试选项时,你可以在error_log中跟踪重写过程,这样你就可以知道它是如何重写的,哪里出错了。

标签: php nginx rewrite http-status-code-404


【解决方案1】:

实际上我不喜欢这两种方法,它可能有效,但它并不是最好的编写方法,所以让我们尝试一些不同的方法。

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;
}

我从来没有听说过 last; break; 它可能只是因为 nginx 忽略了它的最后一部分,它要么是 last 要么是 break

【讨论】:

  • 这会导致 nginx 配置文件失败,也不是最好的解决方案,因为如果 index.php 不可用,它将启动重定向循环并且不会抛出错误(仅在重定向过多之后)而且我相信'最后;'和'休息;'使用 mix 是因为 'last;'使脚本重复并执行 php,'break;'结束当前位置部分http://stackoverflow.com/a/13981316/1612250
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-18
  • 2018-10-24
  • 2017-08-11
  • 2013-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多