Nginx에서 체인 재작성을 어떻게 하나요?

Nginx에서 체인 재작성을 어떻게 하나요?

저는 nginx의 재작성 엔진을 처음 사용합니다. 이전 htaccess 파일을 nginx 형식으로 변환하려고 하는데 문제가 발생했습니다.

# ------------------------------------------------------ #
# 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://twitstamp.com/$1 [R=301,L]

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

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

    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]

그것은 내 htaccess 파일입니다. 이제 내가 지금까지 얻은 것은 다음과 같습니다 ...

# ------------------------------------------------------ #
# This redirects index.php to /                          #
# ------------------------------------------------------ #
if ($request_uri ~* "^/index.php\??$") {
    rewrite ^.*$ http://$host? permanent;
}


# ------------------------------------------------------ #
# This rewrites 'directories' to their PHP files,        #
# fixes trailing-slash issues, and redirects .php        #
# to 'directory' to avoid duplicate content.             #
# ------------------------------------------------------ #
if (!-e $request_filename) {
    rewrite ^(.*)$ $1.php;
    last;
}


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

if (!-e $request_filename) {
    rewrite ^([a-z0-9_\-@#\ "'\+]+)/?([a-z0-9_\-]+)?(\.png|/)?$ generation/image.php?user=$1&template=$2;
    break;
}

index.php 리디렉션은 "디렉토리" 이름 -> php 파일 리디렉션과 마찬가지로 잘 작동합니다. 그러나 여러 가지 작업을 수행하는 방법을 알 수 없습니다. 후행 슬래시 수정을 구현하고 중복된 파일이 없도록 .php 파일을 외부로 리디렉션합니다. /help, /about 등과 같이 모든 페이지가 깔끔하게 보이도록 하고 싶습니다. 서버의 실제 페이지는 /about.php 형식입니다. 또한 이미지 작동에 대한 다시 쓰기 규칙을 얻을 수 없습니다. 실제 파일이나 디렉터리(-e 플래그)가 아니고 재기록 가능한 파일(예: /about)이 아닌 모든 항목을 생성/등으로 리디렉션하고 싶습니다.

답변1

당신은 잘못 생각하고 있습니다. Nginx는 위치 블록을 좋아합니다. 예를 들어 /index.php를 /로 리디렉션하는 것과 같습니다.

location = /index.php {
    rewrite ^ http://domain.com/$args permanent;
}

try_files도 살펴보고 싶을 것입니다. 일반적으로 Nginx에서 if를 사용하는 경우 잘못 수행할 가능성이 있습니다.

Nginx가 서버 블록과 위치 블록을 사용하는 방법에 대한 기본 소개를 보려면 이 글을 읽어 보시기 바랍니다.http://blog.martinfjordvald.com/2010/07/nginx-primer/

관련 정보