大网数据广告活动
服务器运维问答

NGINX configuration regex in location

· 52CE 我爱测

Coming from 15+ years of using Apache's web server, I wanted to give the new kid a chance. ;) I have been trying to tweak my NGINX configuration files for brevity. The site generally rewrites all the traffic to a specific index.p…

问题原文

原帖链接 · Joseph · CC BY-SA 4.0

Coming from 15+ years of using Apache's web server, I wanted to give the new kid a chance. ;) I have been trying to tweak my NGINX configuration files for brevity.

The site generally rewrites all the traffic to a specific index.php, but I have two directories privacy and eula which contain individual index.phps, so for those two directories I want it to just call the files in the directories.

For example:

location ~ /(privacy|eula) {
  try_files $uri $uri/ =404;
}

causes my browser to download the pages content as a .dms file.

If I split the location into two like so:

location /privacy {
  try_files $uri $uri/ =404;
}

location /eula {
  try_files $uri $uri/ =404;
}

It works as intended.

I searched and also found the suggestion to do it this way, but that had the same effect location ~ ^/(privacy|eula). Do I just have to live with the fact that NGINX is very verbose, or what am I doing wrong?

Thanks

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 4.0

The regular expression location blocks are evaluated in order until a match is found.

In your case, you have placed location ~ /(privacy|eula) above the location ~ \.php$ block, so that a URI like /eula/index.php will not be processed by the correct location and will be downloaded instead.

Place the location ~ /(privacy|eula) block after the location ~ \.php$ block.

See this document for details.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-07-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。