nginx 在 404 上提供备用位置

Ste*_*ett 5 rewrite configuration nginx proxy http-status-code-404

我正在尝试按如下方式设置 nginx 配置:当收到如下请求时/tile/SteveCountryVic/1/2/3.png

  1. 尝试将其传递到 http://localhost:5005/1/2/3.png
  2. 如果是 404s,则将其传递给另一台服务器作为 /tile/SteveCountryVic/1/2/3.png

这是我的配置,它不太工作:

server {
   listen 80;
   server_name localhost;   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;        
        #rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;        
        #proxy_set_header Host $http_host;
        #proxy_pass http://127.0.0.1:5005;
        proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/;

   location @dynamiccycletour {
        rewrite_log on;
        #rewrite ^(\d+)/(\d+)/(\d+).*$ /tile/SteveCountryVic/$1/$2/$3.png break;
        proxy_pass http://115.x.x.x:20008;


   }

   location /tile/ {
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:20008;


        proxy_cache my-cache;
        proxy_cache_valid  200 302  60m;
        proxy_cache_valid  404      1m;
    }
    ...
Run Code Online (Sandbox Code Playgroud)

在此配置中,所有请求似乎都被重定向到代理服务器,但最终会提供图像。此外,错误日志包含以下几行:

2013/09/10 09:44:11 [error] 564#0: *138 open() "/etc/nginx/html/tile/SteveCountryVic/13/7399/5027.png" failed (2: No such file or directory), client: 118.x.x.x, server: localhost, request: "GET /tile/SteveCountryVic/13/7399/5027.png?updated=15 HTTP/1.1", host: "mydomain.org"
Run Code Online (Sandbox Code Playgroud)

如果不是使用proxy_redirect,我使用rewriteproxy_pass

        rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;
        proxy_pass http://127.0.0.1:5005;
Run Code Online (Sandbox Code Playgroud)

然后现在我实际上在浏览器中看到了 404 消息(即,它们没有被拦截)。

我的问题:

  1. 我究竟做错了什么?
  2. 为什么 nginx 会在 /etc/nginx/html/... 中寻找文件?
  3. 有没有办法获得更多的日志信息(特别是为了更好地理解 proxy_redirect)?

Ste*_*ett 7

替代版本,使用rewriteproxy_pass表现完美 - 问题是另一台服务器返回 200 而不是 404。所以为了完整起见,这里是工作配置:

server {
   listen 80;
   server_name localhost;
   error_log  /tmp/nginx.error.log notice;
   access_log   /tmp/nginx.access.log;
   location /tile/SteveCountryVic/ {
        rewrite_log on;
        rewrite ^.*/(\d+)/(\d+)/(\d+.*)$ /$1/$2/$3 break;

        proxy_intercept_errors on;
        error_page 404 = @dynamiccycletour;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:5005;
  }

   location @dynamiccycletour {
        rewrite_log on;
        rewrite ^/(\d+)/(\d+)/(\d+.*)$ /tile/SteveCountryVic/$1/$2/$3 break;
        proxy_pass http://115.x.x.x:20008;

   }
Run Code Online (Sandbox Code Playgroud)