当前位置: > 学习笔记 > ThinkPHP > Nginx下ThinkPHP5的配置,配置Pathinfo

Nginx下ThinkPHP5的配置,配置Pathinfo

ThinkPHP默认使用Pathinfo的URL模式,而在ThinkPHP5.0就已经取消了URL模式的概念,不再支持所谓的普通模式URL访问。

首先解释一下Pathinfo是什么东西,Pathinfo并不是Nginx或者Apache的功能,而是PHP的功能,主要被用作URL路径格式优化的。详细可以参考PHP相关文档。

Apache以模块化运行PHP,默认支持PHP的Pathinfo所以默认不需要配置,而Nginx默认没有配置,自然就不支持ThinkPHP。

1.让nginx支持pathinfo

最基础的配置如下:

server
{
  listen 80;
  #listen [::]:80;
  server_name www.lightfun.cn;
  index index.html index.htm index.php default.html default.htm default.php;
  root /home/wwwroot/lightfun;

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  }

  location ~ .*\.(js|css)?$
  {
    expires 12h;
  }

  location ~ /\.
  {
    deny all;
  }

  access_log /home/wwwlogs/lightfun.cn.log;
}

然后添加pathinfo支持

location ~ [^/]\.php(/|$)
{
  fastcgi_pass unix:/tmp/php-cgi.sock;
  fastcgi_index index.php;
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  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_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 REQUEST_SCHEME $scheme;
  fastcgi_param HTTPS $https if_not_empty;

  fastcgi_param GATEWAY_INTERFACE CGI/1.1;
  fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

  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;

  fastcgi_split_path_info ^(.+?\.php)(/.*)$;
  set $path_info $fastcgi_path_info;
  fastcgi_param PATH_INFO $path_info;
  try_files $fastcgi_script_name =404;
}

2.把index.php隐藏掉

location / {
  if (!-e $request_filename) {
    rewrite ^(.*)$ /index.php?s=$1 last;
    break;
  }
}

3.最终配置文件如下,大家可以去参考nginx相关配置说明文档。

server
{
  listen 80;
  #listen [::]:80;
  server_name www.lightfun.cn;
  index index.html index.htm index.php default.html default.htm default.php;
  root /home/wwwroot/lightfun;

  location ~ [^/]\.php(/|$)
  {
    fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    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_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 REQUEST_SCHEME $scheme;
    fastcgi_param HTTPS $https if_not_empty;

    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

    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;

    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    set $path_info $fastcgi_path_info;
    fastcgi_param PATH_INFO $path_info;
    try_files $fastcgi_script_name =404;
  }
  location / {
    if (!-e $request_filename) {
      rewrite ^(.*)$ /index.php?s=$1 last;
    break;
  }

  location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  {
    expires 30d;
  }

  location ~ .*\.(js|css)?$
  {
    expires 12h;
  }

  location ~ /\.
  {
    deny all;
  }
  access_log /home/wwwlogs/lightfun.cn.log;
}

顺便说一下apache下隐藏index.php配置,.htaccess文件。

<IfModule mod_rewrite.c>
  Options +FollowSymlinks -Multiviews
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php?/$1 [QSA,PT,L]
</IfModule>

 

Nginx下ThinkPHP5的配置,配置Pathinfo:等您坐沙发呢!

发表评论

表情
还能输入210个字