How to hide index.php on nginx

4 0
12
Viewed: 110 936 times
Version: 1.1
Category: Tutorials
Written by: miles miles
Updated by: Darwin Wen Darwin Wen
Created: Feb 20, 2009
Updated: 14 years ago

For a complete sample Nginx+PHP-FPM config, view this how-to: Nginx & PHP-FPM

In order to use path URL format and hide index.php on nginx+fastcgi+php, we need the following configurations.

First, we need to add PATH_INFO to the server configuration. Otherwise, we will not be able to use path URL format in Yii:

location ~ \.php {      
    fastcgi_pass  127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_script_name;
    access_log off;
}

Second, we need the following rewrite rule to hide index.php:

nginx versions .7 and higher:

location /yiiGuestbook {
    try_files $uri $uri/ /yiiGuestbook/index.php?r=$request_uri;
}

nginx versions prior to .7:

location /yiiGuestbook {
    if (!-e $request_filename){
        rewrite (.*) /yiiGuestbook/index.php/$1;
    }
}

Please refer to the Guide for hiding index.php on Apache httpd server.

Links ΒΆ

Russian version

Chinese version

FYI, it is good practice to have your 'root' declaration outside of the location blocks. Please refer to this article if you are new to nginx:

nginx-pitfalls