具有多个命名位置的 NGINX try_files [英] NGINX try_files with multiple named locations

查看:43
本文介绍了具有多个命名位置的 NGINX try_files的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据请求中的自定义标头有条件地从缓存中获取文件.

I want to fetch a file from the cache conditionally, based on a custom header in the request.

如果请求中存在 X-Proxy 标头,则仅当它存在于缓存中时才返回文件.否则,如有必要,请从互联网上获取.

If the X-Proxy header is present in the request, return the file only if it's present in the cache. Otherwise fetch it from the internet if necessary.

这是我的 .conf 文件:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    proxy_cache_path /home/nginx/proxy levels=1:2 keys_zone=one:15m inactive=7d max_size=1000m;
    proxy_temp_path  /home/nginx/temp;
    proxy_buffering                 on;
    proxy_set_header   X-Real-IP            $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;
    proxy_set_header   Host                   $http_host;
    proxy_set_header   X-NginX-Proxy    true;
    proxy_set_header   Connection "";
    proxy_http_version 1.1;

    server {
        listen       8000;

        location / {
            root /home/nginx/preload;
            try_files /$uri @local @remote;
        }

        location @local {
            internal;
            add_header X-Local true;
            add_header X-Cache $upstream_cache_status;

            proxy_pass http://$http_host$uri$is_args$args;
            proxy_cache             one;
            proxy_cache_key         backend$request_uri;
            proxy_cache_valid       200  1h;
            proxy_cache_use_stale   error timeout invalid_header;
        }

        location @remote {
            resolver 8.8.8.8;
            add_header X-Remote true;
            add_header X-Cache $upstream_cache_status;

            if ($http_x_proxy) {
                return 404;
            }

            proxy_pass http://$http_host$uri$is_args$args;
            proxy_cache             one;
            proxy_cache_key         backend$request_uri;
            proxy_cache_valid       200  1h;
            proxy_cache_use_stale   error timeout invalid_header;

        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

问题是 try_files 指令总是传递到我的 @remote 位置,即使获取的文件已被缓存.当它从 @local 返回时,我如何告诉它找到了该文件?

The problem is that the try_files directive always passes into my @remote location, even if the fetched file is cached. How do I tell it that the file was found when it returns from @local?

推荐答案

try_files 指令只接受一个命名位置,所以显然它适用于最后一个.这个博客文章提出了一个适用于您的情况的解决方法.如果你不阅读整篇文章,你可以在 @local 块的末尾添加以下几行:

try_files directive only accepts one named location, so apparently it goes for the last one. This blog post proposes a workaround that works in your case. In case you don't won't read the whole post, you can add the following lines at the end of @local block:

proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = @remote;

并将您的 try_files 更改为:

try_files /$uri @local;

这篇关于具有多个命名位置的 NGINX try_files的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆