遵循陷阱指南后,我正在尝试使用try_files指令清理我的URL重写。 但是,当我在安装Nginx 0.8.54-4(来自dotdeb)时使用这个命令时,当试图访问该站点(其中包含index.php PHP脚本的源代码)时,会提示下载BIN文件。 那么,这些指令是否与我的nginx版本不兼容,或者这个vhost有什么问题?
server { listen 80; server_name site.loc; root /home/user/www/site.loc/docroot; index index.html index.php; location / { try_files $uri $uri/ @runphp; } # Pass all .php files to PHP-FastCGI location @runphp { include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } }
另外,这是甚至是这样做的最好方法吗? 我的原始(工作)configuration看起来像这样:
server { listen 80; server_name site.loc; root /home/user/www/site.loc/docroot; index index.html index.php; # Route all requests for non-existent files to index.php if (!-e $request_filename) { rewrite ^ /index.php last; } # Pass PHP scripts to php-fastcgi listening on port 9000 location ~ \.php { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; } }
更新:以下重写工作正常 – 我不得不使用wgettesting,因为浏览器caching响应。 我也更新到Nginx 1.0.5(Ubuntu 11.10默认)。
server { listen 80; server_name site.loc; root /home/user/www/site.loc; index index.html index.php; # Directives to send expires headers and turn off 404 error logging. location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires 24h; log_not_found off; } # Route all requests for non-existent files to index.php location / { try_files $uri $uri/ /index.php$is_args$args; } # Pass PHP scripts to php-fastcgi listening on port 9000 location ~ \.php$ { # Zero-day exploit defense. # http://forum.nginx.org/read.php?2,88845,page=3 # Won't work properly (404 error) if the file is not stored on # this server, which is entirely possible with php-fpm/php-fcgi. # Comment the 'try_files' line out if you set up php-fpm/php-fcgi # on another machine. And then cross your fingers that you won't get hacked. try_files $uri =404; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; } }
你的configuration是错误的。 因为PHP不能parsing不存在的后备URI @runphp ,所以你得到了一个下载提示。
尝试这样的事情:
location / { try_files $uri $uri/ /index.php; } location ~ \.php$ { include fastcgi.conf; fastcgi_pass 127.0.0.1:9000; }