0

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

3 Answers 3

0

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

0

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: https://stackoverflow.com/questions/8199797/convert-this-apache-rewrite-to-nginx-rules/8200728#8200728

0

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;
    }
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .