Nginx 代理 - proxy_set_header - 来自 site.conf 的自定义 header(对于清漆缓存)
Nginx proxy - proxy_set_header - custom header from site.conf (For Varnish Cache)
是否可以在您的网站中发送 自定义 header,然后使用 proxy_set_header
将其转发到 Varnish?
信息
我稍微移动了文件夹位置 - 所有内容都正确包含并且可以使用。
我有 /etc/nginx/conf.d/params/proxy.params/proxy_params
个文件:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port-Nginx $server_port;
proxy_set_header X-Forwarded-DocumentRoot $document_root;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Ssl-Offloaded $scheme;
proxy_set_header X-Forwarded-Custom-Location-Site 'Custom Site'; ### THIS IS THE HEADER I WANT TO BE DYNAMIC
fastcgi_param HTTPS on;
然后在我的 https 服务器块中 sites-enabled/custom-site.conf
:
server {
listen 443 ssl default_server;
server_tokens off;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Custom-Location-Site' 'mysite1';
root /var/www/html;
server_name nginx-glo 192.168.1.105 localhost:443;
location / {
access_log /var/log/nginx/access.log main;
add_header 'Custom-Nginx-Server-Location-Root-Redirect' 'From /'; #### This header does not appear in varnishlog (testing only)
include /etc/nginx/conf.d/params/proxy.params/proxy_params_destination.conf;
相关proxy_params_destination.conf
:
server_tokens off;
proxy_redirect off;
include /etc/nginx/conf.d/params/proxy.params/proxy_params;
# Try not to add headers using add_header, this will reset all headers from other locations
# that are redirected here.
# Also do not log to file here, log in locations above.
proxy_headers_hash_bucket_size 356;
proxy_read_timeout 3600;
proxy_connect_timeout 60;
# Cannot use https:// for proxy calls - use http, outside connection is https
proxy_pass http://varnish-cache-magento2.3.5:6081;
#proxy_pass http://web-server-apache2-magento2.3.5:8080;
我想做的事情:
我有这个 header 定义:
add_header 'Custom-Location-Site' 'mysite1'
在网站配置文件中。
我想从那个 header 接收 _value,每个 website_and 转发到这里:
proxy_set_header X-Forwarded-Custom-Location-Site $how_can_I_get_Custom-Location-Site_here;
然后让 Varnish 缓存在 vcl_recv{}
中接收这个 header
此转发header我会在vcl_recv{}
处理:
使用下面示例中描述的 req.http.X-Forwarded-Custom-Location-Site
。
原因:
Varnish cache setup for multiple Magento 2 sites.
- 下面来自 Varnish 文章的示例显示了我如何使用 paths 来完成它。我不能用Paths,我有多个Docker容器运行不同的网站,然后转发到Varnish容器。例如运行
PHP 7.2
、其他 PHP 7.3
网站的容器,两者都在 运行 内部作为 /var/www/html, https
Link to sample multiple backends
示例中的相关部分:
sub vcl_recv {
if (req.url ~ "^/java/") {
set req.backend_hint = java;
} else {
set req.backend_hint = default;
}
}
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Now let’s add a new backend:
backend java {
.host = "127.0.0.1";
.port = "8000";
}
清漆日志
如此处所示:varnishlog
已经在 Docker 容器中收到我的自定义 header。我现在需要自定义 header 的值是动态的。
- ReqHeader X-Real-IP: 192.168.1.103
- ReqHeader X-Forwarded-For: 192.168.1.103
- ReqHeader X-Forwarded-Port-Nginx: 443
- ReqHeader X-Forwarded-DocumentRoot: /var/www/html
- ReqHeader X-Forwarded-Proto: https
- ReqHeader Ssl-Offloaded: https
- ReqHeader X-Forwarded-Custom-Location-Site: Custom Site
如果我没理解错的话,这就是你需要的:
proxy_set_header X-Forwarded-Custom-Location-Site $http_custom_location_site;
这使得 X-Forwarded-Custom-Location-site
动态,基于您配置 Custom-Location-Site
header.
的 server
块
例如,如果 Custom-Location-Site
的值为 mysite1
,则 X-Forwarded-Custom-Location-site
的值将如下所示:
X-Forwarded-Custom-Location-site: mysite1
根据 http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_,您可以将 $http_
与任何 header 一起使用。您只需将值小写,并将破折号替换为下划线。
是否可以在您的网站中发送 自定义 header,然后使用 proxy_set_header
将其转发到 Varnish?
信息
我稍微移动了文件夹位置 - 所有内容都正确包含并且可以使用。
我有 /etc/nginx/conf.d/params/proxy.params/proxy_params
个文件:
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Port-Nginx $server_port;
proxy_set_header X-Forwarded-DocumentRoot $document_root;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Ssl-Offloaded $scheme;
proxy_set_header X-Forwarded-Custom-Location-Site 'Custom Site'; ### THIS IS THE HEADER I WANT TO BE DYNAMIC
fastcgi_param HTTPS on;
然后在我的 https 服务器块中 sites-enabled/custom-site.conf
:
server {
listen 443 ssl default_server;
server_tokens off;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Custom-Location-Site' 'mysite1';
root /var/www/html;
server_name nginx-glo 192.168.1.105 localhost:443;
location / {
access_log /var/log/nginx/access.log main;
add_header 'Custom-Nginx-Server-Location-Root-Redirect' 'From /'; #### This header does not appear in varnishlog (testing only)
include /etc/nginx/conf.d/params/proxy.params/proxy_params_destination.conf;
相关proxy_params_destination.conf
:
server_tokens off;
proxy_redirect off;
include /etc/nginx/conf.d/params/proxy.params/proxy_params;
# Try not to add headers using add_header, this will reset all headers from other locations
# that are redirected here.
# Also do not log to file here, log in locations above.
proxy_headers_hash_bucket_size 356;
proxy_read_timeout 3600;
proxy_connect_timeout 60;
# Cannot use https:// for proxy calls - use http, outside connection is https
proxy_pass http://varnish-cache-magento2.3.5:6081;
#proxy_pass http://web-server-apache2-magento2.3.5:8080;
我想做的事情:
我有这个 header 定义:
add_header 'Custom-Location-Site' 'mysite1'
在网站配置文件中。我想从那个 header 接收 _value,每个 website_and 转发到这里:
中接收这个 header
proxy_set_header X-Forwarded-Custom-Location-Site $how_can_I_get_Custom-Location-Site_here;
然后让 Varnish 缓存在vcl_recv{}
此转发header我会在
vcl_recv{}
处理:
使用下面示例中描述的req.http.X-Forwarded-Custom-Location-Site
。
原因:
Varnish cache setup for multiple Magento 2 sites.
- 下面来自 Varnish 文章的示例显示了我如何使用 paths 来完成它。我不能用Paths,我有多个Docker容器运行不同的网站,然后转发到Varnish容器。例如运行
PHP 7.2
、其他PHP 7.3
网站的容器,两者都在 运行 内部作为/var/www/html, https
Link to sample multiple backends
示例中的相关部分:
sub vcl_recv {
if (req.url ~ "^/java/") {
set req.backend_hint = java;
} else {
set req.backend_hint = default;
}
}
backend default {
.host = "127.0.0.1";
.port = "8080";
}
Now let’s add a new backend:
backend java {
.host = "127.0.0.1";
.port = "8000";
}
清漆日志
如此处所示:varnishlog
已经在 Docker 容器中收到我的自定义 header。我现在需要自定义 header 的值是动态的。
- ReqHeader X-Real-IP: 192.168.1.103
- ReqHeader X-Forwarded-For: 192.168.1.103
- ReqHeader X-Forwarded-Port-Nginx: 443
- ReqHeader X-Forwarded-DocumentRoot: /var/www/html
- ReqHeader X-Forwarded-Proto: https
- ReqHeader Ssl-Offloaded: https
- ReqHeader X-Forwarded-Custom-Location-Site: Custom Site
如果我没理解错的话,这就是你需要的:
proxy_set_header X-Forwarded-Custom-Location-Site $http_custom_location_site;
这使得 X-Forwarded-Custom-Location-site
动态,基于您配置 Custom-Location-Site
header.
server
块
例如,如果 Custom-Location-Site
的值为 mysite1
,则 X-Forwarded-Custom-Location-site
的值将如下所示:
X-Forwarded-Custom-Location-site: mysite1
根据 http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_,您可以将 $http_
与任何 header 一起使用。您只需将值小写,并将破折号替换为下划线。