欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

NGINX 1.4.1 + PHP 5.2.6 虚拟目录alias配置

程序员文章站 2022-06-13 16:57:47
...

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".错误。

NGINX 1.4.1 + PHP 5.2.6 虚拟目录alias配置

No input file specified. 意为文件没有找到,而我们 开启“autoindex on;”选项时,明明可以看到 此php文件存在。

网上查找了不少资料,在nginx官方找到http://wiki.nginx.org/ChsFcgiExample,需要给虚拟目录添加rewrite。

下面是我的配置:

#user  nobody;
worker_processes  1;
error_log    logs/error.log;
#error_log    logs/error.log  notice;
#error_log    logs/error.log  info;
pid    logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include            mime.types;
    default_type    application/octet-stream;
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                            '$status $body_bytes_sent "$http_referer" '
    #                            '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log;
    #sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  120;
     gzip  on;
     gzip_http_version 1.1;
     gzip_vary on;
     gzip_comp_level 6;
     gzip_proxied any;
     gzip_types text/plain text/css application/json application/x-javascript text/javascript;
     gzip_buffers 16 8k;
     # Disable gzip for certain browsers.
     gzip_disable "MSIE [1-6].(?!.*SV1)";

    server {
        listen        88;
        server_name    192.168.1.155;
        root   d:/document/test;
        charset utf-8;
        access_log  logs/access.log;
        location / {
            root   d:/document/test;
            index  index.php index.html index.htm;
            allow all;
            autoindex on;
        }
        #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   html;
        }
        #------------------虚拟目录支持PHP start----------------------------#
        location /public {
            alias d:/document/public;
            index index.php index.html index.htm;
            allow all;
            autoindex on;
        }
        location ~ ^/public/.+\.php$ {
            #root  d:/document;
            alias d:/document/public;
            rewrite /public/(.*\.php?) /$1 break;
        #关键处
include fastcgi_params; fastcgi_pass 127.0.0.1:9999; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME d:/document/public$fastcgi_script_name; } #------------------虚拟目录支持PHP end----------------------------# # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # # location ~ \.php$ { # proxy_pass http://127.0.0.1; # } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root d:/document/test; fastcgi_pass 127.0.0.1:9999; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } }

重新访问:

NGINX 1.4.1 + PHP 5.2.6 虚拟目录alias配置

关键处:

location /public {
    alias d:/document/public; #虚拟目录地址
    index index.php index.html index.htm;#首页文件
    allow all; #访问控制
    autoindex on; #add浏览目录权限
}
location ~ ^/public/.+\.php$ {
    alias d:/document/public;#虚拟目录地址
    rewrite /public/(.*\.php?) /$1 break;#重写
    include        fastcgi_params;#加载cgi配置
    fastcgi_pass        127.0.0.1:9999;#调用php-cgi.exe 
    fastcgi_index        index.php;    #nginx默认首页文件
    fastcgi_param        SCRIPT_FILENAME d:/document/public$fastcgi_script_name; 
    fastcgi_param        SCRIPT_FILENAME d:/document/public$fastcgi_script_name; 
                        # 脚本文件请求的路径,
                        #注意 默认的   fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                        #$document_root需要重写成 虚拟目录的绝对路径 }