1. 程式人生 > >Nginx伺服器下ThinkPHP5訪問出現404以及URL隱藏index.php

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

專案結構