nginx:将.php文件的执行仅限于某些目录

时间:2018-07-11 07:24:56

标签: nginx nginx-location

现在,我的nginx配置为将php文件请求从所有目录重定向到php5-fpm,如下所示:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}

如何将php文件的执行限制为仅几个预定义目录,例如“ dir1”,“ dir2”和“ dir3”,而对其他目录中的php文件的访问应被拒绝或忽略?

1 个答案:

答案 0 :(得分:1)

location ~ ^/dir1/subdir1/.+\.php(/|$) {
                include php.conf;
        }
location ~ ^/dir2/subdir2/.+\.php(/|$) {
                include php.conf;
        }
location ~ ^/dir3/subdir3/.+\.php(/|$) {
                include php.conf;
        }

php.conf的内容

fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;