NGINX + PHP 使用虚拟目录(alias or root )时,如果不做其他设置,会出现 "No input file specified".错误。 No input file specified. 意为文件没有找到,而我们 开启autoindex on;选项时,明明可以看到 此php文件存在。 网上查找了不少资料,在nginx官方
NGINX + PHP 使用虚拟目录(alias or root )时,如果不做其他设置,会出现 "No input file specified".错误。
No input file specified. 意为文件没有找到,而我们 开启“autoindex on;”选项时,明明可以看到 此php文件存在。
网上查找了不少资料,在nginx官方找到http://wiki.nginx.org/ChsFcgiExample,需要给虚拟目录添加rewrite。
下面是我的配置:
#user nobody<span>; </span>worker_processes 1<span>; </span>error_log logs/error.log<span>; </span>#error_log logs/error.log notice<span>; </span>#error_log logs/error.log info<span>; </span>pid logs/nginx.pid<span>; </span><span> events { worker_connections </span>1024<span>; </span><span>} http { include mime.types</span><span>; </span> default_type application/octet-stream<span>; </span> #log_format main '$remote_addr - $remote_user <span>[</span><span>$time_local</span><span>]</span> "$request"<span> ' # '$status $body_bytes_sent </span>"$http_referer"<span> ' # '</span>"$http_user_agent" "$http_x_forwarded_for"'<span>; </span> access_log logs/access.log<span>; </span> #sendfile on<span>; </span> #tcp_nopush on<span>; </span> keepalive_timeout 120<span>; </span> gzip on<span>; </span> gzip_http_version 1.1<span>; </span> gzip_vary on<span>; </span> gzip_comp_level 6<span>; </span> gzip_proxied any<span>; </span> gzip_types text/plain text/css application/json application/x-javascript text/javascript<span>; </span> gzip_buffers 16 8k<span>; </span><span> # Disable gzip for certain browsers. gzip_disable </span>"MSIE [1-6].(?!.*SV1)"<span>; </span><span> server { listen </span>88<span>; </span> server_name 192.168.1.155<span>; </span> root d:/document/test<span>; </span> charset utf-8<span>; </span> access_log logs/access.log<span>; </span><span> location / { root d:/document/test</span><span>; </span> index index.php index.html index.htm<span>; </span> allow all<span>; </span> autoindex on<span>; </span><span> } #error_page </span>404 /404.html<span>; </span><span> # redirect server error pages to the static page /50x.html # error_page </span>500 502 503 504 /50x.html<span>; </span> location =<span> /50x.html { root html</span><span>; </span><span> } #------------------虚拟目录支持PHP start----------------------------# location /public { alias d:/document/public</span><span>; </span> index index.php index.html index.htm<span>; </span> allow all<span>; </span> autoindex on<span>; </span><span> } location ~ ^/public/.+\.php$ { #root d:/document</span><span>; </span> alias d:/document/public<span>; </span> rewrite /public/(.*\.php?) /$1 break<span>;<br> #关键处 </span> include fastcgi_params<span>; </span> fastcgi_pass 127.0.0.1:9999<span>; </span> fastcgi_index index.php<span>; </span> fastcgi_param SCRIPT_FILENAME d:/document/public$fastcgi_script_name<span>; </span><span> } #------------------虚拟目录支持PHP end----------------------------# # proxy the PHP scripts to Apache listening on </span>127.0.0.1:80<span> # # location ~ \.php$ { # proxy_pass http://</span>127.0.0.1<span>; </span><span> # } # pass the PHP scripts to FastCGI server listening on </span>127.0.0.1:9000<span> # location ~ \.php$ { root d:/document/test</span><span>; </span> fastcgi_pass 127.0.0.1:9999<span>; </span> fastcgi_index index.php<span>; </span> fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name<span>; </span> include fastcgi_params<span>; </span><span> } } }</span>
重新访问:
关键处:
<span>location /public { alias d:/document/public</span><span>;</span><span> #虚拟目录地址</span> index index.php index.html index.htm<span>;</span><span>#首页文件</span> allow all<span>;</span><span> #访问控制</span> autoindex on<span>;</span><span> #add浏览目录权限</span> <span>} location ~ ^/public/.+\.php$ { alias d:/document/public</span><span>;</span><span>#虚拟目录地址</span> rewrite /public/(.*\.php?) /$1 break<span>;</span><span>#重写</span> include fastcgi_params<span>;</span><span>#加载cgi配置</span> fastcgi_pass 127.0.0.1:9999<span>;</span><span>#调用php-cgi.exe </span> fastcgi_index index.php<span>;</span><span> #nginx默认首页文件</span> fastcgi_param SCRIPT_FILENAME d:/document/public$fastcgi_script_name<span>;</span> fastcgi_param SCRIPT_FILENAME d:/document/public$fastcgi_script_name<span>;</span> <span> # 脚本文件请求的路径, #注意 默认的 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name</span><span>; </span><span> #</span>$document_root<span>需要重写成 虚拟目录的绝对路径 }</span>