slash197 slash197 - 1 month ago 6
Apache Configuration Question

htaccess to nginx configuration

I have two htaccess files one for a website (root folder) and one for its admin panel (admin folder):

root

RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteRule ^admin/(.*)$ admin/$1 [NC,L,QSA]
RewriteRule ^sign-out$ index.php?act=auth-signOut [NC,L,QSA]

RewriteRule ^get/(.*)$ index.php?pag=download&id=$1 [NC,L,QSA]
RewriteRule ^g/(.*)$ callback.php?act=download-download&id=$1 [NC,L,QSA]
RewriteRule ^activate/(.*)$ index.php?act=auth-activate&u=$1 [NC,L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pag=cms&title=$1 [NC,L,QSA]


/admin

RewriteRule ^sign-in$ views/login.html [NC,L,QSA]
RewriteRule ^sign-out$ index.php?act=auth-logout [NC,L,QSA]


I was able to convert the website part to nginx config rules but when I add the admin section it doesn't work. Any help is appreciated.

server {
listen 80;
server_name fileorbs.com;

root /var/www/fileorbs/public_html;
index index.php index.html;


location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.

if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?pag=cms&title=$1&code=$2;
}

rewrite ^/admin/(.*)$ /admin/$1 last;
rewrite ^/sign-out$ /index.php?act=auth-signOut last;
rewrite ^/get/(.*)$ /index.php?pag=download&id=$1 last;
rewrite ^/g/(.*)$ /index.php?act=download-download&id=$1 last;
rewrite ^/activate/(.*)$ /index.php?act=auth-activate&u=$1 last;

try_files $uri $uri/ =404;
}

location /admin {
rewrite ^/sign-in$ /views/login.html last;
rewrite ^/sign-out$ /index.php?act=auth-logout last;
}
}

Answer

Note, that apache first checks root-.htaccess, then goes to /admin. Nginx uses location as preferred match. These are two totally different methods.

http://nginx.org/en/docs/http/request_processing.html

Adding last to every rewrite you tell nginx "perform that rewrite", so in fact, your /admin location is not processes anywhere now.

http://nginx.org/ru/docs/http/ngx_http_rewrite_module.html#rewrite

Also, what exactly should that rule do? As I undestand, it just redirects to itself one time:

RewriteRule ^admin/(.*)$ admin/$1 [NC,L,QSA]
rewrite ^/admin/(.*)$ /admin/$1 last;

If I understanded your config right, something like that should work:

server {
    listen       80;
    server_name  fileorbs.com;

        root /var/www/fileorbs/public_html;
        index index.php index.html;


        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass   127.0.0.1:9000;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.

        if (!-e $request_filename){
            rewrite ^/(.*)$ /index.php?pag=cms&title=$1&code=$2;
        }

        rewrite ^/sign-out$ /index.php?act=auth-signOut last;
        rewrite ^/get/(.*)$ /index.php?pag=download&id=$1 last;
        rewrite ^/g/(.*)$ /index.php?act=download-download&id=$1 last;
        rewrite ^/activate/(.*)$ /index.php?act=auth-activate&u=$1 last;

        try_files $uri $uri/ =404;
    }

    location /admin {
        rewrite ^/admin/(.*)$ /admin/$1 break;
        rewrite ^/sign-in$ /views/login.html last;
        rewrite ^/sign-out$ /index.php?act=auth-logout last;
        try_files $uri $uri/ =404;
    }
}
Comments