我正在嘗試按如下方式設定 nginx 配置:當收到如下請求時/tile/SteveCountryVic/1/2/3.png
:
- 嘗試將其傳遞給
http://localhost:5005/1/2/3.png
- 如果出現 404 錯誤,則將其傳遞到另一台伺服器
/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;
}
...
在此配置中,所有請求似乎都被重定向到代理伺服器,但最終會提供圖像。此外,錯誤日誌包含以下幾行:
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"
如果我不使用 ,而是proxy_redirect
使用rewrite
和proxy_pass
:
rewrite ^.*/(\d+)/(\d+)/(\d+).*$ /$1/$2/$3.png break;
proxy_pass http://127.0.0.1:5005;
然後現在我實際上在瀏覽器中看到了 404 訊息(即它們沒有被攔截)。
我的問題:
- 我究竟做錯了什麼?
- nginx到底為什麼要在/etc/nginx/html/...中找檔案?
- 有沒有辦法獲得更多日誌資訊(特別是為了更好地理解 proxy_redirect)?
答案1
替代版本,使用rewrite
和proxy_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;
}
答案2
首先你沒有root
正確設定 - 指令 -> 這就是為什麼你得到 404 -> 這就是為什麼所有請求都被重定向到你的 @dynamiccycletour (openstreetmap?)
順便說一句,/tile/ 和 /tile/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.x.x.x:20008;
}
}