nginx服务404上的备用位置

我试图build立一个nginxconfiguration如下:当收到像/tile/SteveCountryVic/1/2/3.png的请求:

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

这是我的configuration,这是不是很工作:

 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.xxx: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; } ... 

在这个configuration中,所有的请求似乎都被redirect到了代理服务器,但是图像最终被提供。 另外,错误日志包含这些行:

 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.xxx, server: localhost, request: "GET /tile/SteveCountryVic/13/7399/5027.png?updated=15 HTTP/1.1", host: "mydomain.org" 

如果不使用proxy_redirect ,我使用rewriteproxy_pass

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

那么现在我实际上在浏览器中看到了404条消息(即它们不会被拦截)。

我的问题:

  1. 我究竟做错了什么?
  2. 为什么Nginx在/ etc / nginx / html / …中查找文件?
  3. 有没有办法获得更多的日志信息(具体来说,为了更好地理解proxy_redirect)?

首先你没有正确地设置你的root指令 – >那就是为什么你得到404 – >这就是为什么所有的请求都被redirect到你的@dynamiccycletour(openstreetmap?)

顺便说一句,最后/瓷砖/和/瓷砖/ SteveCountryVic /?

所以我们首先在这里需要一点清理:

 server { .... # define where to find files # be sure to have it like /path/to/tile root /path/to/tiles/; location /tile/SteveCountryVic/ { # if file not found -> remote server try_files $uri @dynamiccycletour rewrite_log on; # this should cover /1/2/3.png. no? rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break; # i'm not sure this will match due the the rewrite proxy_redirect /tile/SteveCountryVic/ http://localhost:5005/; location @dynamiccycletour { rewrite_log on; # this should cover /1/2/3.png. no? rewrite /tile/SteveCountryVic/(.*).png$ /$1.png break; proxy_pass http://115.xxx:20008; } } 

替代版本,使用rewriteproxy_passperformance完美 – 问题是其他服务器返回200而不是404的。 所以为了完整性,这里是工作configuration:

 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.xxx:20008; }