前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >配置Nginx目录别名Alias支持PHP解析

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

作者头像
星哥玩云
发布2022-07-26 21:02:17
1.4K0
发布2022-07-26 21:02:17
举报
文章被收录于专栏:开源部署开源部署

关于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指令指定的路径。

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com