Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have following .htaccess file

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ index.php [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
<ifModule mod_php5.c>
    php_flag display_errors Off
</IfModule>

My code is work fine using Apache. But now I'm using Nginx, here are my conf

server {
    listen 80;
    server_name example.com;
    root /srv/www/example.com/public_html;
    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;
    server_tokens off;
    if (!-e $request_filename) {
        rewrite ^(.*)$ index.php last;
    }
    location / {
        index index.php;
    }

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public_html$fastcgi_script_name;
    }
}

But my code is not work, please help

Thanks

share|improve this question

migrated from stackoverflow.com Dec 30 '11 at 0:57

This question came from our site for professional and enthusiast programmers.

3 Answers 3

The simple answer is that rewrite directives are not the same in apache as in nginx.

Have a look at the nginx rewrite documentation: http://wiki.nginx.org/HttpRewriteModule

Fortunately, the nginx HttpRewriteModule is quite easy to get your head around, in terms of building rewrite logic, compared to mod_rewrite rules that some people find to be black magic

share|improve this answer

first you should really avoid using if on nginx. Search for ifIsEvil on nginx wiki.

Then you do not need nginx rexrite for such a simple thing as serving directory, existing files and at last index.php. A simple try_files will do the job. Read nginx FAQ and search for try_file.

check this answer: http://stackoverflow.com/questions/8199797/convert-this-apache-rewrite-to-nginx-rules/8200728#8200728

share|improve this answer

If I'm correct, you have a Zend/some other framework bootstrapper. In that case, use this config:

server {
    listen 80;
    server_name example.com;
    root /srv/www/example.com/public_html;
    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;
    server_tokens off;

    try_files $uri $uri/ /index.php?$args;

    location = /index.php {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public_html$fastcgi_script_name;
    }
}

OR

server {
    listen 80;
    server_name example.com;
    root /srv/www/example.com/public_html;
    access_log /srv/www/example.com/logs/access.log;
    error_log /srv/www/example.com/logs/error.log;
    server_tokens off;

    try_files $uri $uri/ /index.php?$args;

    location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/example.com/public_html$fastcgi_script_name;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.