我正在使用nginx v1.6.2为基于Web的应用程序提供dynamicnodeJS和静态文件内容。
在一个特定的位置(这恰好是服务于静态PNG文件),我想任何未find的图像请求返回一个特定的占位符图像,“blank.png”。
例如,path为%nginxroot%/html/tiles/sectionals/0/0/0.png确实存在,所以URL http://myhost/tiles/sectionals/0/0/0.png应该返回要求的图像文件。 但是path%nginxroot%/html/tiles/sectionals/0/0/99.png中的文件不存在,所以请求http://myhost/tiles/sectionals/0/0/99.png应该返回文件在%nginxroot%/html/tiles/blank.png 。
这里是我的(为简洁起见)nginx.conf:
events { worker_connections 4096; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 20; gzip on; gzip_comp_level 6; gzip_vary on; gzip_min_length 1000; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; gzip_buffers 16 8k; server { listen 80; server_name *.myhost.com; root html; #dynamic content at root url location / { 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; proxy_pass http://127.0.0.1:9000; proxy_redirect off; rewrite ^/auth/(.*)$ /$1 break; } # tiles are all static .png files location /tiles/ { proxy_intercept_errors on; error_page 404 = blank.png; } } }
我看到这个configuration的行为是,如果文件存在,它的工作原理,如果文件不存在浏览器告诉我,我有一个redirect循环,控制台显示GET http://myhost/tiles/sectionals/0/0/blank.png net::ERR_TOO_MANY_REDIRECTS 。
我已经尝试了可能导致path问题的解决scheme,如error_page 404 = ./blank.png,closures或省略proxy_intercept_errors,在html和tiles目录中放置blank.png,我似乎无法得到它工作 – 只有一个例外,那就是在可能请求图像的每个目录中放置一个blank.png。 这似乎是一个愚蠢的解决方法,但是。
所以我明白,nginx无法findblank.png,因此无限redirect(或超出某个阈值),但.conf文件应该具有哪个位置和/或blank.png位于何处?
您需要指定要提供的文档的绝对path。 否则,它被视为相对于最初请求的URL的path。
error_page 404 = /tiles/blank.png;