以前thinphp3.2项目是运行在ceontos6.5 服务器下的apache 和 php 5.6的环境,由于公司项目迁移至新服务器上,现将项目部署在centos7.2 服务器下的 nginx 和php7.0的环境运行,但是以前项目用的伪静态。迁移过程中出现一些问题,网上查阅资料后将问题解决了,特此记录下来,以备后用。
server { listen 80; server_name www.test.cn; index index.html index.htm index.php; root /data/wwwroot/html; #error_page 404 /404.html; location / { #ThinkPHP3.2 REWRITE支持 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?s=$1 last; } #301 跳转设置 if ($host = 'test.cn') { rewrite ^/(.*) http://www.test.cn/$1 permanent; } } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwroot/logs/access.log access; }
在如上代码中,添加了:
if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?s=$1 last; }
如果ThinkPHP项目安装在二级目录,Nginx的伪静态方法设置如下,其中subdir是所在的目录名称
location /subdir/ { if (!-e $request_filename){ rewrite ^/subdir/(.*)$ /subdir/index.php?s=$1 last; } }