H
H
Hazrat Hajikerimov2016-12-31 14:34:19
Nginx
Hazrat Hajikerimov, 2016-12-31 14:34:19

Setting up rewrite in Nginx?

Today I switched to Nginx from Apache, and faced with the need to configure rewrite, or rather transfer mod_rewrite .htaccess to nginx.
Here is my config in .htaccess:

AddDefaultCharset UTF-8
php_flag magic_quotes_gpc Off
php_flag magic_quotes_runtime Off
php_flag register_globals Off
Options -Indexes

RewriteEngine    on

RewriteCond %{REQUEST_URI} !^/favicon.ico
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]

I found a service that converts .htaccess to nginx server, it gave me the following:
charset utf-8;
location = /favicon.ico {
}
autoindex off;
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?route=$1 break;
}
}

But it doesn't work, nginx gives empty files.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
wostex, 2017-02-11
@wostex

Try like this

charset utf-8;
autoindex off;

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

If the files are empty, the problem may not be in nginx, but for example, SCRIPT_FILENAME is not specified in php-fpm:
location ~ \.php$ {
                include fastcgi_params;
                fastcgi_pass unix:/var/run/php-fpm.sock;
                fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question