让 nginx 支持thinkphp 的 PATH_INFO 和 URL Rewrite模式支持

时间:2022-10-18 09:18:23

情况简介:

   我们的网站是thinkphp框架开发的,之前一直在apache+php下面运行.很想换到nginx上试试,经过百般折腾无果.无奈去官方发现是nginx不支持thinkphp的 PATH_INFO 和 URL Rewrite模式,不过还好官方也给出了相应的解决办法。下面就是实验的过程全记录。


实验环境介绍:

   系统 : centos 6.4 x86_64

   ip   : 192.168.80.141

   环境 : lnmp

   版本 : nginx-1.4.1 , PHP-5.4.0 ,mysql-5.1.62 ,thinkphp 3.0


至于lnmp 环境的搭建在这里就不多说了,网上很多. 下面在介绍一下我的目录存放的介绍。这个地方要仔细看 因为跟nginx 里面的配置有很多的联系。


  nginx :  /usr/local/nginx/

  php   :  /usr/local/php/

  网站根目录: /usr/local/nginx/html/


------------------开始配置nginx支持thinkphp----------------------------

原始配置内容:

user  nobody;worker_processes  1;
error_log logs/error.log info;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
}
location /status {
stub_status on;
access_log on;
allow 192.168.80.0/24;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}

-----------------------------------------------------------------------------------

修改后支持thinkphp的内容:

user  nobody;worker_processes  1;error_log  logs/error.log  info;pid        logs/nginx.pid;events {    use epoll;    worker_connections  1024;}http {    include       mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  logs/access.log  main;    sendfile        on;    keepalive_timeout  65;                                                                                                                                  gzip on;    gzip_min_length  1k;    gzip_buffers     4 16k;    gzip_http_version 1.0;    gzip_comp_level 2;    gzip_types       text/plain application/x-javascript text/css application/xml;    gzip_vary on;    server {        listen       80;        server_name  localhost;        location / {            root   html;            index  index.php index.html index.htm;        }        error_page   500 502 503 504  /50x.html;        location = /50x.html {            root   html;        }        location ~ \.php {                   #去掉后面的$            root           html;            fastcgi_pass   127.0.0.1:9000;            fastcgi_index  index.php;             fastcgi_split_path_info ^(.+\.php)(.*)$;                             #增加这一句             fastcgi_param PATH_INFO $fastcgi_path_info;                          #还有这一句            #####fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;  # 这个是在配置nginx+php整合的时候就改好的$前面的是网站的主目录            include        fastcgi_params;        }        if (!-e $request_filename)            {            #地址作为将参数rewrite到index.php上。             rewrite ^/(.*)$ /index.php/$1;            #   若是子目录则使用下面这句,将subdir改成目录名称即可。            # rewrite ^/subdir/(.*)$ /subdir/index.php/$1;            #    }            }    }server {        listen       80;        server_name  s.abc.org;        location / {            root   html;            index  index.html index.htm;        }        location /status {            stub_status on;            access_log   on;            allow 192.168.80.0/24;        }    }}

-----------------至此nginx已经支持了thinkphp---------------------------------

有个小的问题,nginx的状态查看页面。即:http://IP/status 在配置好了支持thinkphp之后,竟然无法正常工作了,所以我把它单独建立了一个虚拟机来解决。主机名s.abc.org

错误的status

让 nginx 支持thinkphp 的 PATH_INFO 和 URL Rewrite模式支持

再来个搞定之后的测试截图:

让 nginx 支持thinkphp 的 PATH_INFO 和 URL Rewrite模式支持

--------------------祝各位早日搞定----------------------------------

     


   

本文出自 “振兴的空间” 博客,请务必保留此出处http://renzhenxing.blog.51cto.com/728846/1256856