Nginx服务器下ThinkPHP5访问出现404以及URL隐藏index.php

thinkphp5.0标准的 URL 访问格式

http://serverName/index.php/模块/控制器/操作

浏览器访问出现404


nginx配置

server {
    listen       84;
    server_name localhost;
    root /usr/share/nginx/html;
    index index.php;

    access_log /var/log/nginx/html-access.log;
    error_log  /var/log/nginx/html-error.log;

    location / {
        try_files $uri $uri/ /index.php?$args =404;
        #try_files $uri$args $uri$args/ index.php;
    }

    location ~ .*\.(php|php7.0)?$ {
        #fastcgi_pass   127.0.0.1:9000;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include        fastcgi_params;
    }

    location ~ /\.(ht|svn|git) {
            deny all;
    }
}


参考http://www.thinkphp.cn/topic/40391.html

更改后

server {
    listen 84;
    server_name localhost;
    access_log /var/log/nginx/html-access.log;
    error_log  /var/log/nginx/html-error.log;
    #root是下面设计到文件路径的根目录
    root /usr/share/nginx/html;
    index index.html index.php;


    #定义变量
    set $root /usr/share/nginx/html;
 
    #匹配url中server_name之后的部分
    location /tp5/public/ {
        #重写url 为了隐藏tp5中的index.php
        if ( !-e $request_filename) {
            #将url中server_name之后的部分与 /tp5/public/* 匹配 如果匹配则改写URl为/tp5/public/index.php/*
            rewrite ^/tp5/public/(.*)$ /tp5/public/index.php/$1 last;
            break;
        }
    }


    #pathinfo配置 使支持tp5的标准url
    location ~ .+\.php($|/) {
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_split_path_info ^((?U).+.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $root$fastcgi_script_name;
        include fastcgi_params;
    }
}

web目录

/usr/share/nginx/html

项目结构




猜你喜欢

转载自blog.csdn.net/u014788227/article/details/72458259