我希望Nginx能够自己处理任何对静态文件的请求,但是如果文件不存在,那么可以使用index.php来处理这一切
目前我的configuration看起来像这样,
server { listen 80; listen [::]:80; root /home/www/example.com/htdocs; index index.php; server_name www.example.com; location ~* ^[^\?\&]+\.(html|jpg|jpeg|json|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|svg|woff|ttf)$ { # First attempt to serve request as file, then # as directory, then fall back to index.php try_files $uri $uri/ /index.php; #try_files /favicon.ico =404; } location / { add_header X-Is-PHP true; try_files /index.php =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; # With php5-fpm: fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } }
这是尽可能接近,它提供任何请求静态文件,如果它不存在,将index.php作为明文文件。 我怎样才能得到index.php传递给PHP解释器?
尝试这个
服务器{
听80;
听[::]:80;
root /home/www/example.com/htdocs;
index index.php;
server_name www.example.com;
位置〜* ^ [^ \?\&] + \。(html | jpg | jpeg | json | gif | png | ico | css | zip | tgz | gz | rar | bz2 | doc | xls | pdf | ppt | txt | tar | mid | mid | wav | bmp | rtf | js | svg | woff | ttf)$ {
#首先尝试作为文件服务请求,然后
#作为目录,然后回退到index.php
try_files $ uri $ uri / /index.php;
#try_files /favicon.ico = 404;
}
error_page 404 /index.php;
位置〜\ .php $ {
add_header X-Is-PHP true;
#try_files $ uri = 404;
try_files /index.php = 404;
fastcgi_split_path_info ^(。+ \。php)(/.+)$;
#使用php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
包括fastcgi.conf;
}
}
变化
1)增加了error_page 404 /index.php; 以便在服务器上找不到的所有请求都被redirect到index.php
2)添加“〜.php $”到位置属性。
3)如果你想要解释其他PHP文件,取消注释“#try_files $ uri = 404;”行 并注释行“try_files /index.php = 404;”
location / { if (!-e $request_filename) { rewrite ^/(.*)$ /index.php; } }