Nginx将所有不匹配文件的请求redirect到一个php文件

我试图获得所有请求http://example.com/downloads/* : http://example.com/downloads/*redirecthttp://example.com/downloads/index.php除非请求的文件存在于/downloads/

例如:

  • http://example.com/downloads => /downloads/index.php
  • http://example.com/downloads/unknowfile => /downloads/index.php
  • http://example.com/downloads/existingfile => /downloads/existingfile

我目前的问题是我有redirect到PHP工作,但静态文件没有送达或相反。

这是我目前的虚拟主机conf :(redirect罚款,但静态文件发送到PHP和失败)

 server { listen 80; ## listen for ipv4; this line is default and implied server_name domain.com; root /data/www; index index.php index.html; location / { try_files $uri $uri/ /index.html; } error_page 404 /404.html; # redirect server error pages to the static page /50x.html error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/www; } location ^~ /downloads { fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_index index.php; include fastcgi_params; try_files $uri @downloads; } location @downloads { rewrite ^ /downloads/index.php; } # pass the PHP scripts to FastCGI server # location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi_params; } } 

精度:静态文件是由/downloads/index.php创build的符号链接

编辑:可能重复 ,但这个解决scheme似乎并不适合我。

使用index.php作为下载位置的自定义错误页面(不要发送404头):

 location ^~ /downloads { ... fastcgi_intercept_errors on; error_page 404 =200 /downloads/index.php; }