Yii 1.1: How to hide index.php on nginx


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_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:


Total 3 comments

#9874 report it
bool.dev at 2012/09/18 03:54pm
Another try_files option

In some cases try_files last uri should be changed to:

try_files $uri $uri/ /yiiGuestbook/index.php$request_uri;

I think(in my case) this depends on if you are splitting path_info, i.e using fastcgi_split_path_info.

#4903 report it
Ben. at 2011/08/26 07:01pm

When using try_files with index.php?r=$request_uri; I had an endless 302 loop (using nginx 1.0.5). using try_files with just index.php as suggested by SniperZero fixed the problem.

#2269 report it
SniperZero at 2010/12/05 02:54am
Problems in root directory solved.

So I originally had issues with multipaged listviews. I was running scripts from root directory things weren't getting parsed correctly. You might want to add this to the wiki entry.

My nginx (v. 0.8.5) rule is: location / { try_files $uri $uri/ /index.php; }

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

Was causing problems.

Hope this helps someone!

Leave a comment

Please to leave your comment.

Write new article
  • Written by: miles
  • Updated by: Darwin Wen
  • Category: Tutorials
  • Yii Version: 1.1
  • Votes: +4 / -1
  • Viewed: 61,952 times
  • Created on: Feb 19, 2009
  • Last updated: Oct 19, 2011
  • Tags: URL