try_files 동적 콘텐츠 유형

try_files 동적 콘텐츠 유형

각 파일에 대해 어떻게 다른 콘텐츠 유형을 제공할 수 있는지 궁금했습니다.
예를 들어 다음과 같은 nginx 구성이 있습니다.

server {
    listen 80;
    server_name site.com;

    location ~ ^/(.*) {
        root /home/site/;
        try_files $uri
            $uri.png
            $uri.bin
            =404;
        }
}

예를 들어 모든 항목 에는 콘텐츠 유형 헤더가 $uri.png있고 . image/png$uri.binapplication/octet-stream

file문제: 어느 것이 try_files선택되었는지 확인하는 방법을 모르겠습니다 .
필요할 경우를 대비해 Lua 모듈을 설정하고 작동하고 있습니다.

다른 사람이 이런 것을 시도한 적이 있습니까?

답변1

내가 당신이 원하는 것을 이해했다고 가정하면 이것은 나에게 효과적입니다.

if ($request_uri ~* \.png$) {
    more_set_headers "Content-type: image/png";
}
if ($request_uri ~* \.(jpg|jpeg)$) {
    more_set_headers "Content-type: image/jpeg";
}
if ($request_uri ~* \.gif$) {
    more_set_headers "Content-type: image/gif";
}

표준 add_header 함수가 Content-type을 덮어쓸 수 있도록 최근 nginx가 변경되지 않은 한 비표준 모듈이 필요합니다.http://wiki.nginx.org/HttpHeadersMoreModule

반대 방향으로 가고 싶다면(즉, 원래 URI에는 확장자가 없고 파일 시스템에는 있습니다) $request_uri를 $uri로 바꾸십시오. try_files 전/후 순서가 중요할 수 있습니다.

관련 정보