Nginx proxy-proxy\u set\u header-site.conf中的自定义头(用于清漆缓存)

Nginx proxy-proxy\u set\u header-site.conf中的自定义头(用于清漆缓存),nginx,varnish,magento-2.3.5,Nginx,Varnish,Magento 2.3.5,是否可以在您的网站中发送自定义标题,然后使用proxy\u set\u标题将其转发到Varnish 信息 我已经移动了一点文件夹的位置-所有的东西都包括正确的和工程 我有/etc/nginx/conf.d/params/proxy.params/proxy\u params文件,其中包含: proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-

是否可以在您的网站中发送自定义标题,然后使用
proxy\u set\u标题
将其转发到Varnish

信息 我已经移动了一点文件夹的位置-所有的东西都包括正确的和工程

我有
/etc/nginx/conf.d/params/proxy.params/proxy\u 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;
然后在
站点启用/custom site.conf
中的我的https服务器块中:

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;

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;
相关的
代理参数\u destination.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;

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;
我想做的是:
  • 我已定义此标题:
    在网站配置文件中添加标题“自定义位置站点”“mysite1”

  • 我想从每个网站的标题中接收\u值,并将其转发到此处:
    proxy\u set\u header X-Forwarded-Custom-Location-Site$如何在此处获取\u Custom-Location-Site
    然后让Varnish缓存在
    vcl_recv{}

  • 然后,我将在
    vcl_recv{}
    中处理这个转发头:
    使用
    req.http.X-Forwarded-Custom-Location-Site
    ,如下例所述

原因: 多个Magento 2站点的清漆缓存设置

  • 下面这篇Varnish文章中的示例展示了如何使用路径执行此操作。我不能使用路径,我有多个Docker容器运行不同的网站,然后将其转发到Varnish容器。例如,运行
    PHP7.2
    的容器,其他
    PHP7.3
    网站,两者都以
    /var/www/html、https
    的形式在内部运行

样本中的相关部分:

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容器中接收我的自定义标题。我现在需要自定义头的值是动态的

-   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
头的
server
块动态运行

例如,如果
Custom-Location-Site
的值为
mysite1
,则
X-Forwarded-Custom-Location-Site
的值如下:

X-Forwarded-Custom-Location-site: mysite1

根据,您可以对任何标题使用
$http
。您只需将值小写,并用下划线替换破折号。

如果我理解正确,这就是您需要的:

proxy_set_header X-Forwarded-Custom-Location-Site $http_custom_location_site;
这使得
X-Forwarded-Custom-Location-site
根据配置
Custom-Location-site
头的
server
块动态运行

例如,如果
Custom-Location-Site
的值为
mysite1
,则
X-Forwarded-Custom-Location-Site
的值如下:

X-Forwarded-Custom-Location-site: mysite1

根据,您可以对任何标题使用
$http
。您只需将值小写,并用下划线替换破折号。

这确实是我想要做的,我希望能够从位置或站点块添加\u头,然后将其转发到
代理\u参数
。我不知道使用自定义头
$http\
,我总是假设默认代理参数和其他配置中的头是内置变量。我会试试的,谢谢!我刚刚试过,但是现在
X-Forwarded-Custom-Location-Site
没有到达
varnishlog
中的Varnish缓存,它现在不见了。只要我在
proxy_params
中再次添加一个静态值(如我在示例中所示),标题就会再次出现。我尝试了
添加_标题“自定义位置站点”“site1-magento2.3.5”服务器{}
根块中以及
位置/
块中,如我上面的原始示例中所示,使用代码>将其保存。如果禁用清漆,
#代理(u pass)http://varnish-cache-magento2.3.5:6081;
并将位置设置为Apache web服务器:
proxy\u passhttp://web-server-apache2-magento2.3.5:8080;
我实际上看到了两个标题的响应:
自定义Nginx服务器位置根重定向:From/
自定义位置站点:site1-magento2.3.5
。这将确认位置块已使用,但未随请求一起发送?。在使用
proxy\u pass
之前,是否有方法查看处理/发送的调试头信息。这里缺少一些内容,我在
location/{}
块中创建了一个
error\u log/var/location/root-debug.log debug
条目,我看到了头,但它是在
http proxy header
条目
X-Forwarded
之后记录的。这些自定义$http是否仅用于响应/现在,这确实是我想要做的,我希望能够从一个位置或站点块添加_头,然后将其转发到
代理_参数
。我不知道使用自定义头
$http\
,我总是假设默认代理参数和其他配置中的头是内置变量。我会试试的,谢谢!我刚刚试过,但是现在
X-Forwarded-Custom-Location-Site
没有到达
varnishlog
中的Varnish缓存,它现在不见了。只要我在
proxy_params
中再次添加一个静态值(如我在示例中所示),标题就会再次出现。我尝试了
添加_标题“自定义位置站点”“site1-magento2.3.5”服务器{}
根块中以及
位置/
块中,如我上面的原始示例中所示,使用代码>将其保存。如果禁用清漆,
#代理(u pass)http://varnish-cache-magento2.3.5:6081;
并将位置设置为Apache web服务器:
proxy\u passhttp://web-server-apache2-magento2.3.5:8080;
我实际上看到了两个标题的响应:
自定义Nginx服务器位置根重定向:From/
自定义位置站点:site1-magento2.3.5
。这将确认位置块已使用,但未随请求一起发送?。是否有办法查看在代理之前处理/发送的调试头信息