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]

엔진엑스:

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] 

엔진엑스:

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]

엔진엑스:

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

관련 정보