htaccess到nginx配置

时间:2016-09-01 15:05:04

标签: apache .htaccess nginx

我有两个htaccess文件,一个用于网站(根文件夹),另一个用于管理面板(管理文件夹):

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]

/管理员

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

我能够将网站部分转换为nginx配置规则但是当我添加管理部分时它不起作用。任何帮助表示赞赏。

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

1 个答案:

答案 0 :(得分:1)

注意,apache首先检查root-.htaccess,然后转到/ admin。 Nginx使用location作为首选匹配。这是两种完全不同的方法。

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

最后添加到您告诉​​nginx“执行重写”的每次重写中,所以实际上,您的 / admin 位置现在不是任何进程。

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

此外,该规则到底应该做什么?在我看来,它只是重定向到自己一次:

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

如果我说明你的配置正确,那样的话应该有效:

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