lnmp Nginx 配置Thinkphp rewrite
网上看到很多例子试来试去 没一个能用的 = =自己综合研究后 总结如下方案
ThinkPHP的四种URL模式:0(普通模式);1(PATHINFO模式);2(REWRITE模式);3(兼容模式)
nginx需要PATHINFO模式,但需要更改nginx配置文件让其支持PATHINFO模式。
首先确保原始path index.php?m=模型&c=控制器&a=方法 可以使用
修改配置文件
1.修改php配置文件php.ini,将其中cgi.fix_pathinfo = 0,值改为1
重启php-fpm 或者重启整个服务器
2.ssh里执行:
cat > /usr/local/nginx/conf/pathinfo.conf << 'EOF'
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;
EOF
执行后打开pathinfo.conf 内容如下
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;
三、
再将虚拟主机配置文件里(/usr/local/nginx/conf/vhost/okweex.com.conf)的 location ~ [^/]\.php(/|$) 替换为:location ~ .*\.php
再在include fcgi.conf; 下面添加一行include pathinfo.conf;
重启nginx
网上有案例不完整 只能带index.php 访问rewrite 我们需要 清除index.php
添加thinkphp隐藏index.php支持:
如果入口文件在子目录中则为:
location / {
root E:/www;
index index.html index.php index.htm;
if (!-e $request_filename) {
rewrite ^/子目录/(.*)$ /子目录/index.php?s=$1 last;
break;
}
}
如果入口文件在web目录中为:
location / {
root E:/www;
index index.html index.php index.htm;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
}
完整的虚拟主机配置文件如下:
server { listen 80; server_name okweex.com www.okweex.com; index index.html index.htm index.php; root /alidata1/home/okweex; #开启根域名全站跳转www rewrite if ( $host != 'www.okweex.com' ) { rewrite "^/(.*)$" http://www.okweex.com/$1 permanent; } location / { root /alidata1/home/okweex; index index.html index.php index.htm; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } location ~ .*\.php { try_files $uri =404; fastcgi_pass unix:/tmp/php-cgi.sock; fastcgi_index index.php; include fastcgi.conf; include pathinfo.conf; } location /status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/okweex.com.log access; }
ThinkPHP就可以在nginx中运行了。
如果 您还想给域名 做LNMP下带www与不带www的301跳转设置方法