M
M
MaKS_Ibl42017-07-19 09:38:00
Nginx
MaKS_Ibl4, 2017-07-19 09:38:00

How can I convert .htaccess to work in Nginx?

Hello.
The crux of the matter is this.
I have an Nginx + php-fpm server, I'm trying to install the script, it basically works, but some of the links don't work.
.htaccess looks like this:

AddType image/x-icon .ico
AddDefaultCharset UTF-8

<IfModule mod_rewrite.c>
Options +FollowSymlinks
Options -Indexes

RewriteEngine on
#RewriteBase /
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml|zip|txt|doc)$
RewriteRule ^(.*) $1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} \.(ini|ph.*)$
RewriteRule ^(.*) index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L,QSA]
</IfModule>

<IfModule mod_php5.c> 
php_flag magic_quotes_gpc Off
</IfModule>

Nginx is like this:
location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;

    if ($request_uri ~ "\.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml|zip|txt|doc)$"){
      rewrite ^/(.*) /$1 break;
    }
    if (!-e $request_filename){
      rewrite ^/(.*) /index.php;
    }

        location ~ [^/]\.php(/|$) {
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            if (!-f $document_root$fastcgi_script_name) {
                return  404;
            }

            fastcgi_pass    127.0.0.1:9002;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

I tried to convert via winginx, but it didn't work. Maybe someone knows more powerful tools for converting or in general will help with this issue?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2017-07-19
@MaKS_Ibl4

I know the best tool - the brain.
1. Read and understand what .htaccess does
2. Write the necessary lines in the nginx config.
Reading:

RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml|zip|txt|doc)$
RewriteRule ^(.*) $1 [QSA,L]

We understand that static should just be given as is without trying to process it in PHP.
Prishem
location ~ \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml|zip|txt|doc)$ {
  # ничего не нужно, просто отдаём файлы
}

Reading
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_URI} \.(ini|ph.*)$
RewriteRule ^(.*) index.php [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L,QSA]

There are two different conditions here.
The first is if the file does not exist, the second is if a file ending in .ini or .ph<something> was requested (by the way, the regular expression is idiotic, because too much falls under it, for example my.physics.html ).
When executing any of them, the request must be passed to index.php.
The first condition is written in the standard
location / {
  try_files $uri /index.php;
}

Second
# скорее всего нужно что-то типа \.(ini|php|phps|php4|php5)$
# или хотя бы \.(ini|ph\w*)$
location ~ \.(ini|ph.*)$ {
  rewrite ^ /index.php break;

  fastcgi_pass 127.0.0.1:9002;
  # и прочие fastcgi_*
}

Total:
location / {
  try_files $uri /index.php;
}
location ~ \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml|zip|txt|doc)$ {
  # ничего не нужно, просто отдаём файлы
}

location ~ \.(ini|ph.*)$ {
  rewrite ^ /index.php break;

  fastcgi_pass 127.0.0.1:9002;
  # и прочие fastcgi_*
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question