我正在使用docker并在varnish旁边运行nginx。
因为我正在运行docker,所以我在nginx配置(resolver 127.0.0.11 ipv6=off valid=10s;
)的顶部手动设置了解析器,以便容器ip的更改将被拾取,而无需重新启动nginx。
这是配置中给我带来麻烦的相关部分:
location ~^/([a-zA-Z0-9/]+)$ {
set $args ''; #clear out the entire query string
set $card_name $1;
set $card_name $card_name_lowercase;
rewrite ^ /cards?card=$card_name break;
proxy_set_header x-cache-key card-type-$card_name;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_set_header REQUEST_URI $request_uri;
proxy_http_version 1.1;
set $backend "http://varnish:80";
proxy_pass $backend;
proxy_intercept_errors on;
proxy_connect_timeout 60s;
proxy_send_timeout 86400s;
proxy_read_timeout 86400s;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
error_page 503 /maintenance.html;
}
当我访问这个URL时,例如https://example.com/Test
,我得到500内部服务器错误。
在nginx错误日志中,我看到以下内容:
2022/04/27 23:59:45 [error] 53#53: *1 invalid URL prefix in "", client: 10.211.55.2, server: example.com, request: "GET /Test HTTP/2.0", host: "example.com"
我不确定是什么原因导致这个问题-http://
包含在后端,所以它确实有一个适当的前缀。
如果我只使用proxy_pass http://varnish:80
,它工作得很好,但后端需要是一个变量,以便强制docker使用解析器。
我也遇到过类似的问题。我不知道为什么,但是定义
设置后端"美元;http://varnish: 80";;
location block外