我为 openstreetmaps 设置了一个非常基本的 nginx 反向代理:
worker_processes 1;
error_log logs/error.log notice;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
postpone_output 0;
log_format logresponsetime '$remote_addr - $remote_user [$time_local] "$request" $status $bytes_sent "$http_referer" "$http_user_agent" $request_time $upstream_response_time';
access_log logs/access.log logresponsetime;
resolver 192.168.0.1 ipv6=off;
#default proxy settings:
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_read_timeout 600;
proxy_ssl_server_name on;
upstream tile.openstreetmap.org {
server tile.openstreetmap.org:443;
keepalive 10;
keepalive_time 5m;
}
server {
listen 480;
access_log logs/osm.http.access.log logresponsetime;
server_name $hostname;
location / {
resolver 192.168.0.1 ipv6=off;
proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36";
proxy_pass https://tile.openstreetmap.org;
}
}
}
有一行用于设置 User-Agent 标头,因为 OSM 需要它,而且我需要与不发送用户代理字符串的客户端兼容。但是,当 nginx 设置此标头时,大约 10% 的请求会失败(nginx 向客户端返回 502),并且我的 nginx 错误日志中充满了以下内容:
2021/06/13 14:37:49 [error] 67240#87020: *71 connect() failed (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: server, request: "GET /16/64153/41313.png HTTP/1.1", upstream: "https://151.101.166.217:443/16/64153/41313.png", host: "localhost:480"
如果没有 proxy_set_header,一切都很好。在这种情况下,请求都是从 chrome 发出的,因此 OSM 服务器看到的 User-Agent 在两种情况下应该相同。nginx 版本是 1.19.10(Windows)。我做错了什么?