將 HTACCESS mod_rewrite 指令轉換為 nginx 格式?

將 HTACCESS mod_rewrite 指令轉換為 nginx 格式?

我是 nginx 的新手,我正在嘗試轉換我從 Apache 編寫的應用程序,因為我需要能夠同時為大量客戶端提供服務,而無需大量開銷!我正在掌握設定 nginx 和 FastCGI PHP 的竅門,但我還無法理解 nginx 的重寫格式。我知道你必須寫一些簡單的腳本,放入 nginx 設定中的 server {} 區塊中,但我還不熟悉語法。有 Apache 和 nginx 經驗的人可以幫我將其轉換為 nginx 格式嗎?謝謝!

# ------------------------------------------------------ #
# Rewrite from canonical domain (remove www.)            #
# ------------------------------------------------------ #

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

# ------------------------------------------------------ #
# This redirects index.php to /                          #
# ------------------------------------------------------ #

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
    RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] 

# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files,        #
# fixes trailing-slash issues, and redirects .php        #
# to 'directory' to avoid duplicate content.             #
# ------------------------------------------------------ #

    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)$ $1.php [L]

    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L]

    RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

# ------------------------------------------------------ #
# If it wasn't redirected previously and is not          #
# a file on the server, rewrite to image generation      #
# ------------------------------------------------------ #

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=${escapemap:$1}&template=${escapemap:$2} [NC,L]

答案1

我逐一轉換以便更容易理解。

阿帕契:

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

nginx:

server {
    listen 80;
    server_name www.domain.com;
    root /var/www/localhost/htdocs;

    rewrite ^ http://domain.com$request_uri permanent;
}

阿帕契:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(index|index\.php)\ HTTP/
RewriteRule ^(index|index\.php)$ http://domain.com/ [R=301,L] 

nginx:

location = /index {
    try_files $uri @homepage;
}
location @homepage {
    rewrite ^ http://domain.com break;
}
location ~ \.php$ {
    if ($request_uri ~* /index\.php$) {
        rewrite ^ http://domain.com break;
    }
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi.conf;

    fastcgi_intercept_errors        on;
    error_page 404 /error/404.php;
}

阿帕契:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/$ http://domain.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\.php\ HTTP/
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)\.php$ http://domain.com/$1 [R=301,L]

nginx:

rewrite ^/(.*)/$ /$1 permanent;

if (-f $document_root/$request_uri.php) {
    rewrite ^(.*)$ $1.php last;
}

location ~ \.php$ {
    if (-f $document_root/$request_uri) {
        rewrite ^([^.]+)\.php$ http://domain.com$1 permanent;
    }
    ...
}

相關內容