Nginx不支持thinkphp5.0以pathInfo路径访问网站页面的解决方法

pathInfo路径示例:www.tp5.com/index.php/Index/Index/index

pathInfo路径解读:域名/入口文件/模块/控制器/方法

1.打开php的配置文件php.ini,用Ctrl+F找到cgi.fix_pathinfo并修改cgi.fix_pathinfo=1。

2.打开nginx的配置文件nginx.conf,找到server大括号下面与php相关的location信息并修改如下:

#去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo
        location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
                root           H:/phpProject/tp5/public;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
                include        fastcgi_params;
        }

3.最后重启服务器即可解决问题

4.如果还需要隐藏路径中的入口文件如index.php的路径,可以在server大括号下面修改 location / {}中的信息,修改如下:

        location / {
            root   H:/phpProject/tp5/public;
            index  index.php index.html index.htm;
            #下面这三行为增加的内容
            if (!-e $request_filename) {            
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }
        }

如果项目入口文件是在一个子目录里面,则如下:

location / {
  if ( !e $request_filename ) {
    rewrite ^/目录/(.*)$ /目录/index.php/$1 last;
  }
}

总的nginx.conf内容如下:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    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  www.tp5.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   H:/phpProject/tp5/public;
            index  index.php index.html index.htm;
            if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=/$1  last;
            }
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo
        location ~ \.php(.*)$ { # 正则匹配.php后的pathinfo部分
                root           H:/phpProject/tp5/public;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  $DOCUMENT_ROOT$fastcgi_script_name;
                fastcgi_param PATH_INFO $1; # 把pathinfo部分赋给PATH_INFO变量
                include        fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.php index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.php index.html index.htm;
    #    }
    #}

}

  参考网址:https://www.cnblogs.com/laowenBlog/p/6729819.html

发布了54 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/baidu_35800355/article/details/103585669