nginx重写url隐藏index.php

以TP框架为例

rewrite 重写

server {

    server_name $host;

    listen 8070;

    index index.html index.htm index.php;

    root /apps/www/php/;

    access_log  logs/test.log;

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

    location ~ .*\.php.* {

      include fastcgi.conf;

      # set PATH_INFO, SCRIPT_FILENAME, SCRIPT_NAME
      set $fastcgi_script_name2 $fastcgi_script_name;
      if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
                set $fastcgi_script_name2 $1;
                set $path_info $2;
      }


      fastcgi_param   PATH_INFO $path_info;
      fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;
      fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;
      fastcgi_pass upstream_php;
      fastcgi_index index.php;

    }

}

try_files

    server {

        server_name $host;

        listen 8070;

        index index.html index.htm index.php;

        root /apps/www/php/;

        access_log  logs/test.log;

        location / {

        try_files $uri $uri/ /index.php/$uri?$query_string;  
        //注意需要加上$query_string 才能接受到get参数

        }


        location ~ .*\.php.* {


          include fastcgi.conf;

          # set PATH_INFO, SCRIPT_FILENAME, SCRIPT_NAME
          set $fastcgi_script_name2 $fastcgi_script_name;
          if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
                    set $fastcgi_script_name2 $1;
                    set $path_info $2;
          }

          fastcgi_param   PATH_INFO $path_info;
          fastcgi_param   SCRIPT_FILENAME   $document_root$fastcgi_script_name2;
          fastcgi_param   SCRIPT_NAME   $fastcgi_script_name2;
          fastcgi_pass upstream_php;
          fastcgi_index index.php;

        }

    }

你可能感兴趣的:(php,nginx)