nginx: 일부 파일 이름(*.txt)에 대해서만 역방향 프록시에서 404를 직접 처리하는 방법은 무엇입니까?

nginx: 일부 파일 이름(*.txt)에 대해서만 역방향 프록시에서 404를 직접 처리하는 방법은 무엇입니까?

포트 80과 443의 nginx전면이 TLS를 포함한 모든 외부 액세스를 처리하는 복잡한 설정이 있습니다 .nginx

frontend-nginx 에 있는 파일의 경우 /textsCPU 및 기타 리소스를 사용하여 복잡한 프로세스에서 기존 텍스트 파일을 즉시 수정하는 두 번째 backend-nginx로 요청을 프록시해야 합니다.

존재하지 않는 파일 *.txt(404)의 경우 백엔드를 전혀 방해하지 않고 대신 클라이언트에 기본 파일을 /texts/default.txt직접 제공하고 싶습니다. 그러나 현재 존재하지 않는 파일은 여전히 ​​백엔드 error_page 404라인에서만 처리됩니다. 기존 파일은 문제 없이 제공되며 프록시가 작동합니다.

이것은 내 구성입니다.

frontend-nginx.conf:
http {
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  frontend.example.org;
        root         /srv/www;

        location /texts/ {

            location ~ \*.txt$ {
                root /srv/www/backend;

                ####### the next line has absolutely no effect
                try_files $uri /texts/default.txt;
            }

            proxy_pass          http://localhost:90;
            proxy_redirect      http://localhost:90/ /;
            proxy_set_header    Host             $host;
            proxy_set_header    X-Real-IP        $remote_addr;
            proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header    X-Client-Verify  SUCCESS;
            proxy_set_header    Upgrade          $http_upgrade;
            proxy_set_header    Connection       "upgrade";
            proxy_http_version  1.1;

            proxy_redirect off;
        }
    }
    # https goes here, all the same except TLS
}
backend-nginx.conf:
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;

    server {
        listen       127.0.0.1:90;

        root /srv/www/backend;
        charset utf-8;

        expires -1;  # no-cache
        location ~ /..*\.txt$ {
            # longer cache time for text files
            expires 10m;

            # this actually works but only here in the backend
            error_page  404 @404;
        }

        location @404 {
            return 302 $scheme://frontend.example.org/texts/default.txt
        }
    }
}

프론트엔드 구성 파일에 404 리디렉션을 처리할 수 있는 것처럼 보이는 쓸모없는 설명이 있지만 default.txt처리할 때

wget -v http://frontend.example.org/texts/notexist.txt

백엔드 내부에서만 리디렉션을 받습니다(따라서 프록싱이 발생합니다).

답변1

location /texts/ {
    proxy_set_header ...;
    proxy_pass ...;

    location ~ \.txt$ {
        root /path/to/root;
        try_files $uri /texts/default.txt;
        proxy_pass ...;
    }
}
location = /texts/default.txt {
    root /path/to/root;
}

명령문 에 대한 올바른 정규 표현식을 확인하세요 location. 명령문은 proxy_set_header상속되지만 proxy_pass중첩된 에서 명령문을 반복해야 합니다 location.

try_files명령문은 파일이 있는지 확인하고 파일이 없으면 URI를 변경합니다.

기본 파일에는 location해당 파일이 올바른 루트에서 정적 파일로 제공될 수 있도록 전용 파일이 있습니다.

파일 경로는 의 값 root과 URI를 연결하여 구성되므로 파일은 /texts/default.txt에 위치합니다 /path/to/root/texts/default.txt.

관련 정보