Nginx下alias支持PHP的解决

nginx+php-fpm

需要使用一个alias来引用从项目根目录以外的目录,Nginx没有虚拟目录的概念,可以使用location配合alias来解决

如果你是按照以下配置,那么nginx是不会把php的代码交给php解析的

location /uploadphp/ {
                alias /data/uploadphp/;
                index  index.html index.htm index.php;
                }

location ~\.php$ {
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /www/lijun.me/$fastcgi_script_name;
                include fastcgi_params;
        }

如果这样配置,访问 /data/uploadphp目录下的静态文件是可以的,但是访问php文件,就会报错”No input file specified.”因为该alias下的php没有交给php去解析,增加如下配置,即可把alias下的php代码交给php解析

原理是:采用rewrite的方法,对于/uploadphp下的php类型的请求交给后端的php-fpm处理,并且指定了php脚本的位置.

location /uploadphp/ {
                alias /www/lijun.me/uploadphp/;
                index  index.html index.htm index.php;
                }
location ~ /uploadphp/.+\.php$ {
                rewrite /uploadphp/(.*\.php?) /$1 break;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /www/lijun.me/uploadphp/$fastcgi_script_name;
                include fastcgi_params;
                }
location ~\.php$ {
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /www/lijun.me/$fastcgi_script_name;
                include fastcgi_params;
        }

需要注意的是:

location ~\.php$ {
                root html;
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME /www/lijun.me/$fastcgi_script_name;
                include fastcgi_params;
        }

要放在alias的后面.

此条目发表在Nginx分类目录,贴了, , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已用*标注