阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

配置Nginx目录别名Alias支持PHP解析

206次阅读
没有评论

共计 1251 个字符,预计需要花费 4 分钟才能阅读完成。

关于 Nginx 配置基础知识,PHP 用 FastCGI,在 Apache 里,有 alias,比较方便,在 Nginx 下没有虚拟目录概念的,是用 location 配合 alias 使用,但使用 alias 标签的目录块中不能使用 rewrite 的 break。

一、例子说明:

1)我的环境是:web 根目录在 /var/www/html/ 中,但是我要加上一个类似于 apache 的别名目录 /bbs , 此目录不在 web 根目录中。
我的配置文件如下:

server {
    listen      80;
    server_name    localhost;
    default_type text/plain;
    location / {
        root    /var/www/html;
        index    index.php index.htm index.html;
    }
    location /bbs {
        alias /opt/bbs;
        index index.html index.htm index.php;
    }
    location ~ ^/bbs/.+\.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME /opt$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }
location ~ \.php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html$fastcgi_script_name;
        include        fastcgi_params;
        #include fastcgi.conf;
    }
}

说明:上面这个就是成功的例子。

二、但是又如下几点需要注意:

1)location ~ \.php$ {}  段,必须放在 location ~ ^/bbs/.+\.php$ {} 段后面, 否则 /bbs/ 的 url 打不开
2)location ~ ^/bbs/.+\.php$ {} 里面也可以写成如下:

location ~ ^/bbs/.+\.php$ {
        root /opt;
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

也就是用变量名 $document_root 代替 /opt; 其实每个 location {} 中的 $document_root 都是局部变量,都是在本段配置 root 指令指定的路径。

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计1251字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中