我试图让所有请求:
http://example.com/downloads/*
重定向到,http://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
我当前的问题是,要么重定向到 php 工作正常,但静态文件无法使用,要么相反。
这是我当前的 vhost 配置:(重定向正常,但静态文件发送到 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 创建的符号链接
編輯:可能复制,但这个解决方案对我来说似乎不起作用。
答案1
使用 index.php 作为下载位置的自定义错误页面(并且不要发送 404 标头):
location ^~ /downloads {
...
fastcgi_intercept_errors on;
error_page 404 =200 /downloads/index.php;
}