Nginx 重写规则“^/([a-z0-9]{32})\.png$”不起作用(以前在 Apache 上可以正常工作)

Nginx 重写规则“^/([a-z0-9]{32})\.png$”不起作用(以前在 Apache 上可以正常工作)

我有以下重写规则(我都尝试过但无济于事):

location ~* "^/([a-z0-9]{32})\.png$" {
  rewrite ^ /index.php?page=log&id=$1 last;
} 

location ~* "/(?<hash>[a-z0-9]{32})\.png" {
  rewrite ^ /index.php?page=log&id=$hash;
}

location / {
  try_files $uri $uri/ @rewrites;
}

location @rewrites {
  rewrite "^/([a-zA-Z0-9]{32})\.png$" /index.php?page=log&id=$1 last;
  #...
}

基本上,我希望 URLhttp://example.com/aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.png真正将参数传递给我的index.php脚本,在脚本中,我得到:

$db->save_hash($_GET['id']);
header('Content-type: image/png');
readfile('images/beacon.png');
break;

但是 nginx 给出“未找到”的错误信息,而其他重写工作正常。这是怎么回事?

答案1

根据 nginx 文档location指导作品:

位置可以通过前缀字符串或正则表达式来定义。...正则表达式的搜索在第一次匹配时终止,并使用相应的配置。

根据附加信息从你自己的回答中对于这个问题,看起来您打算同时应用多个位置指令,但根据明确的文档,这明确是不允许的。

请记住,nginx 的设计更快、更简洁。所以,不要指望它有什么怪癖!

答案2

这个对我有用:

    location / { 
    rewrite "/([a-z0-9]{32})\.png" /index.php?page=log&id=$1 break;
    }

或者如果您想要分开的位置:

location ~* "/([a-z0-9]{32})\.png" {
rewrite /(.*) /index.php?page=log&id=$1 last;
}

答案3

虽然@unlo的答案对我来说似乎是正确的(我自己在干净的虚拟主机上尝试过),但我看到了另一个优化。您可能不想通过脚本提供静态文件,而是想要使用发送文件功能使得 nginx 提供这些文件的服务。

答案4

马克指出,所以我开始删除所有包含的文件,并归结为:

/etc/nginx/conf.d/h5bp.conf

包含

# Basic h5bp rules

include /etc/nginx/conf.d/expires.conf;
include /etc/nginx/conf.d/x-ua-compatible.conf;
include /etc/nginx/conf.d/protect-system-files.conf;

得到expires.conf

# Expire rules for static content

# No default expire rule. This config mirrors that of apache as outlined in the
# html5-boilerplate .htaccess file. However, nginx applies rules by location,
# the apache rules are defined by type. A concequence of this difference is that
# if you use no file extension in the url and serve html, with apache you get an
# expire time of 0s, with nginx you'd get an expire header of one month in the
# future (if the default expire rule is 1 month). Therefore, do not use a
# default expire rule with nginx unless your site is completely static

# cache.appcache, your document html and data
location ~* \.(?:manifest|appcache|html|xml|json)$ {
  expires -1;
  access_log /var/log/nginx/static.log;
}

# Feed
location ~* \.(?:rss|atom)$ {
  expires 1h;
  add_header Cache-Control "public";
}

# Favicon
location ~* \.ico$ {
  expires 1w;
  access_log off;
  add_header Cache-Control "public";
}

# Media: images, video, audio, HTC
location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

# CSS and Javascript
location ~* \.(?:css|js)$ {
  expires 1y;
  access_log off;
  add_header Cache-Control "public";
}

# WebFonts
# If you are NOT using cross-domain-fonts.conf, uncomment the following directive
location ~* \.(ttf|ttc|otf|eot|woff|font.css)$ {
  expires 1M;
  access_log off;
  add_header Cache-Control "public";
}

评论说expires.conf,然后它开始工作,我该如何解决这个问题?

相关内容