15、在apahce、nginx上搭建thinkPHP项目
https://www.cnblogs.com/dee0912/p/5208002.html
https://blog.csdn.net/qq_38738033/article/details/81172370
https://blog.csdn.net/li_haijiang/article/details/75221343
https://blog.csdn.net/liuyubing2018/article/details/80953213
thinkphp
http://www.thinkphp.cn/topic/44839.html
https://blog.csdn.net/donglingjiu/article/details/80672523
https://www.cnblogs.com/tylerdonet/p/3803197.html
ThinkPHP3.2.3 的 URL_MODEL 包括普通模式(0)、PATHINFO 模式(1)、REWRITE 模式(2)、兼容模式(3)等 4 种 URL 模式。在 Apache 下只要在配置文件 config.php 中配置 URL_MODEL 配合 .htaccess 就可以很容易地支持 REWRITE 模式。
1.在apache上配置thinkphp项目:
测试的环境是 CentOS 6.6 + LNMP 1.2 (Nginx 1.8.0,MySQL5.6.23,PHP 5.6.9)+ ThinkPHP 3.2.3
编辑 nginx.conf 文件:
[root@localhost conf]# vim nginx.conf
项目完整的 Server 段:
server { listen 80; server_name www.tpblog.com; index index.html index.htm index.php; root html/www.tpblog.com; #error_page 404 /404.html; include enable-php.conf; location /nginx_status { stub_status on; access_log off; } #rewrite location / { try_files $uri @rewrite; } location @rewrite { set $static 0; if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) { set $static 1; } if ($static = 0) { rewrite ^/(.*)$ /index.php?s=/$1; } } location ~ /Uploads/.*\.php$ { deny all; } location ~ \.php/ { if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_NAME $1; fastcgi_param PATH_INFO $2; fastcgi_param SCRIPT_FILENAME $document_root$1; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } #rewrite结束 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } location ~ /\. { deny all; } access_log /home/wwwlogs/access_tpblog.log access; }
2.在nginx上配置thinkphp项目:
默认nginx服务器是不识别pathinfo模式和rewrite模式的路由的。
一个配置文件,完美支持普通,兼容,pathinfo,rewrite4种url模式. 常见的静态文件404时也不会再去跑一遍fastcgi浪费资源。
server { listen 80; server_name thinkphp.lo; root /var/www; index index.html index.htm index.php; error_page 404 /404.html; location = /404.html { return 404 'Sorry, File not Found!'; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; # windows用户替换这个目录 } location / { try_files $uri @rewrite; } location @rewrite { set $static 0; if ($uri ~ \.(css|js|jpg|jpeg|png|gif|ico|woff|eot|svg|css\.map|min\.map)$) { set $static 1; } if ($static = 0) { rewrite ^/(.*)$ /index.php?s=/$1; } } location ~ /Uploads/.*\.php$ { deny all; } location ~ \.php/ { if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { } fastcgi_pass 127.0.0.1:9000; include fastcgi_params; fastcgi_param SCRIPT_NAME $1; fastcgi_param PATH_INFO $2; fastcgi_param SCRIPT_FILENAME $document_root$1; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
3.Nginx服务器下使Thinkphp URL模式支持PATHINFO模式和REWRITE模式
默认nginx服务器是不识别pathinfo模式和rewrite模式的路由的,下面我们做一些配置,让nginx服务器支持这些路由模式。
PATHINFO:
找到location ~ \.php { #\.php$ 里面的$去掉,并在里面加上下面两句 fastcgi_split_path_info ^(.+\.php)(.*)$; #增加 fastcgi_param PATH_INFO $fastcgi_path_info; #增加
加好之后大致是这个样子的:
location ~ \.php { root html fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_split_path_info ^(.+\.php)(.*)$; #添加 fastcgi_param PATH_INFO $fastcgi_path_info; #添加 }
REWRITE去掉index.php
找到location / { 在里面加上 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?s=$1 last; break; }
加好之后是这样子的
location / { root html index index.html index.htm index.php; if (!-e $request_filename) { #添加 rewrite ^/(.*)$ /index.php?s=$1 last; #添加 break; #添加 } #添加 }
如果thinkphp不是部署在网站根目录下,即需要把
rewrite ^/(.*)$ /index.php?s=$1 last; 改成 rewrite ^/子目录/(.*)$ /子目录/index.php?s=$1 last;
如果根目录下有多个项目的话,可以写多个rewrite:
if (!-e $request_filename) { rewrite ^/项目1/(.*)$ /项目1/index.php?s=$1 last; rewrite ^/项目2/(.*)$ /项目2/index.php?s=$1 last; rewrite ^/项目3/(.*)$ /项目3/index.php?s=$1 last; break; }
4.Nginx兼容php框架的pathinfo模式与URL重写
几乎所有的框架(ThinkPHP,Zend Framework,CI,Yii,laravel等)都会使用URL重写或者pathinfo模式,使URL看起来更美观,比如可以隐藏掉入口文件,并且有利于搜索引擎优化,其实让Nginx支持pathinfo或者重写也不难,首先在php.ini中设置cgi.fix_pathinfo=1
再就是添加配置文件(新版nginx) 内容如下:
location ~ \.php { fastcgi_pass 127.0.0.1:9056; fastcgi_index index.php; #pathinfo 支持开始 以上配置信息根据实际情况配置 fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; #pathinfo 支持结束 include fastcgi_params_modified; }
如果是老版本的nginx需要按如下配置:
if ($request_filename ~* (.*).php) { set $php_url $1; } if (!-e $php_url.php) { return 403; } fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; set $path_info ""; set $real_script_name $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+?.php)(/.+)$") { set $real_script_name $1; set $path_info $2; } fastcgi_param SCRIPT_FILENAME $document_root$real_script_name; fastcgi_param SCRIPT_NAME $real_script_name; fastcgi_param PATH_INFO $path_info; fastcgi_param GATEWAY_INTERFACE CGI/1.1; fastcgi_param SERVER_SOFTWARE nginx; fastcgi_param QUERY_STRING $query_string; fastcgi_param REQUEST_METHOD $request_method; fastcgi_param CONTENT_TYPE $content_type; fastcgi_param CONTENT_LENGTH $content_length; #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #fastcgi_param SCRIPT_NAME $fastcgi_script_name; fastcgi_param REQUEST_URI $request_uri; fastcgi_param DOCUMENT_URI $document_uri; fastcgi_param DOCUMENT_ROOT $document_root; fastcgi_param SERVER_PROTOCOL $server_protocol; fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_PORT $remote_port; fastcgi_param SERVER_ADDR $server_addr; fastcgi_param SERVER_PORT $server_port; fastcgi_param SERVER_NAME $server_name;
这样Nginx就支持pathinfo了。
对于URL重写也比较简单 例如对于THINKPHP可以这样设置:
location / { if (!-e $request_filename){ #rewrite ^(.*)$ /index.php?s=/$1 last; rewrite ^(.*)$ /index.php/$1 last; } }
2.3.4点,实际上意思差不多。就是怎么让nginx支持php框架的pathinfo和url重写。
参考:
http://www.thinkphp.cn/topic/34380.html
https://www.cnblogs.com/dee0912/p/5208002.html
https://blog.csdn.net/li_haijiang/article/details/75221343
https://blog.csdn.net/liuyubing2018/article/details/80953213
posted on 2019-03-04 00:03 myworldworld 阅读(804) 评论(0) 收藏 举报