最完美解决Nginx部署ThinkPHP项目的办法
2015-07-14 15:50
716 查看
网上通用解决方法的配置如下:
其实应该使用更简单的方法,fastcgi模块自带了一个fastcgi_split_path_info指令专门用来解决此类问题的,该指令会根据给定 的正则表达式来分隔URL,从而提取出脚本名和path info信息,使用这个指令可以避免使用if语句,配置更简单。
另外判断文件是否存在也有更简单的方法,使用try_files指令即可。
转载:http://blog.csdn.net/tinico/article/details/18033573
1、修改thinkphp让他可以在nginx上运行
2、修改nginx让它支持pathinfo
网上说nginx开启pathinfo是有一定风险的,能不用pathinfo最好不用,所以还是折腾thinkphp吧,个人觉得这种方法相对第2种方法来得简单
修改nginx的rewrite
然后项目配置下url模式改为2
如果是多个项目,布署项目时要把项目布署到目录里,如后台的项目放到Admin目录里,那么在nginx的rewrite里再写一条
最后也不要忘记把这个项目的url模式改为2。
转载:http://www.3lian.com/edu/2012/12-14/49363.html
Nginx环境
在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:
其实内部是转发到了ThinkPHP提供的兼容模式的URL,利用这种方式,可以解决其他不支持PATHINFO的WEB服务器环境。
如果你的ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。
转载:http://doc.thinkphp.cn/manual/hidden_index.html
server { ... location / { index index.htm index.html index.php; #访问路径的文件不存在则重写URL转交给ThinkPHP处理 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } location ~ \.php/?.*$ { root /var/www/html/website; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #加载Nginx默认"服务器环境变量"配置 include fastcgi.conf; #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量 set $fastcgi_script_name2 $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") { set $fastcgi_script_name2 $1; set $path_info $2; } fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2; fastcgi_param SCRIPT_NAME $fastcgi_script_name2; } }
其实应该使用更简单的方法,fastcgi模块自带了一个fastcgi_split_path_info指令专门用来解决此类问题的,该指令会根据给定 的正则表达式来分隔URL,从而提取出脚本名和path info信息,使用这个指令可以避免使用if语句,配置更简单。
另外判断文件是否存在也有更简单的方法,使用try_files指令即可。
server { ... location / { index index.htm index.html index.php; #如果文件不存在则尝试TP解析 try_files $uri /index.php$uri; } location ~ .+\.php($|/) { root /var/www/html/website; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量, #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置 fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; #加载Nginx默认"服务器环境变量"配置 include fastcgi.conf; } }
转载:http://blog.csdn.net/tinico/article/details/18033573
修改thinkphp让他可以在nginx上运行
最近在用thinkphp做一个项目,基本完成后部署到nginx服务器上才发觉nginx是不支持pathinfo的,网上搜索了别人的解决方法,有两种思路:1、修改thinkphp让他可以在nginx上运行
2、修改nginx让它支持pathinfo
网上说nginx开启pathinfo是有一定风险的,能不用pathinfo最好不用,所以还是折腾thinkphp吧,个人觉得这种方法相对第2种方法来得简单
修改nginx的rewrite
location / { if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
然后项目配置下url模式改为2
'URL_MODEL'=>2,
如果是多个项目,布署项目时要把项目布署到目录里,如后台的项目放到Admin目录里,那么在nginx的rewrite里再写一条
location /Admin/ { if (!-e $request_filename) { rewrite ^/Admin/(.*)$ /Admin/index.php?s=$1 last; break; } }
最后也不要忘记把这个项目的url模式改为2。
转载:http://www.3lian.com/edu/2012/12-14/49363.html
Nginx环境
在Nginx低版本中,是不支持PATHINFO的,但是可以通过在Nginx.conf中配置转发规则实现:
location / { // …..省略部分代码 if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } }
其实内部是转发到了ThinkPHP提供的兼容模式的URL,利用这种方式,可以解决其他不支持PATHINFO的WEB服务器环境。
如果你的ThinkPHP安装在二级目录,Nginx的伪静态方法设置如下,其中youdomain是所在的目录名称。
location /youdomain/ { if (!-e $request_filename){ rewrite ^/youdomain/(.*)$ /youdomain/index.php?s=$1 last; } }
转载:http://doc.thinkphp.cn/manual/hidden_index.html
相关文章推荐
- nginx代理配置403
- 软负载与nginx的相关配置
- nginx搭建图片服务器
- LNMP(Linux+Nginx+Mysql+PHP)环境下安装yaf框架并编写一个MVC例子
- nginx增加ssl服务方法
- nginx 安装手记
- nginx 安装手记 分类: Nginx 服务器搭建 2015-07-14 14:28 15人阅读 评论(0) 收藏
- RedHat Enterprise Linux 6.4使用Centos 6 的yum源 分类: 服务器搭建 Nginx 2015-07-14 14:11 5人阅读 评论(0) 收藏
- nginx+webpy+uswgi+jwplayer组合搭建流媒体服务器
- Nginx Rewrite可以用到的全局变量
- Nginx配置文件详细说明
- python+django+nginx+uwsgi环境解决timeout 504
- 基于nginx和ffmpeg搭建HLS流媒体服务器
- 解析nginx负载均衡
- awk 处理 nginx 日志
- nginx配置文件
- nginx支持ssi使用../
- 菜鸟学习-C语言函数参数传递详解-结构体与数组 分类: C/C++ Nginx 2015-07-14 10:24 89人阅读 评论(0) 收藏
- nginx配置中rewrite中变量判断conditon
- web前段跨域nginx代理配置