1

如果在服务器上找不到图像,我想将图像请求重定向到 github repo。我在哪里做错了?

    location ~* \.(gif|jpg|jpeg|png)$ {
            try_files $uri /gitpipe =404;
    }

    location ~* /gitpipe$ {
            proxy_pass https://raw.github.com/Org/$arg_repo/master/$uri;
    }

我将配置更改为下一个

location ~* \.(gif|jpg|jpeg|png)$ {
    try_files $uri /gitpipe =404;   
}

location = /gitpipe {
            proxy_pass http://websaints.net/rlog.php?save;
}

但结果还是一样。Nginx 不会将请求重定向到静态 url http://websaints.net/rlog.php?save;所以问题不在 $uri 中。


顺便说一句,此配置有效,但它重定向到 uri,而不是代理并缓存它

location ~* \.(gif|jpg|jpeg|png)$ {
    try_files $uri @redirect;
}

location @redirect {
            return 301 http://websaints.net/rlog.php?save&req=$request_uri;
}

嗯……

4

1 回答 1

1

首先,您可以轻松地将您的更改location为:

location = /gitpipe { 

因为它的含义与 `^/gitpipe$' 相同,但在 nginx 中具有最高优先级,因此该位置将始终被视为第一个。

接下来,您将尝试返回文件(如果存在),因此使用 url 如下:

http://your.domain.com/some_dir/another_dir/some_file.gif

通过第一个location块,您尝试从root指令中获取文件:

root_directive/some_dir/another_dir/some_file.gif

如果这个文件不存在,那么你告诉try_files指令它应该在第二个location块中搜索,在这里你是代理传递这样的 url:

proxy_pass https://raw.github.com/Org/$arg_repo/master/$uri;

但对于我们的示例,这将意味着:

proxy_pass https://raw.github.com/Org/$arg_repo/master//root_directive/some_dir/another_dir/some_file.gif

$arg_repo它的价值将在哪里改变。

对您的指令所做的解释是否正是您的想法?

以下是您可以轻松重定向到所需内容的方法:

location ~ \.(jpg|png|gif) {
   try_files $uri /proxy$request_url =404;
}

location ^~ /proxy(.*) {
   proxy_pass https://your.proxy.domain/$1;
}
于 2013-01-27T14:40:39.443 回答