【问题标题】:Nginx proxy - proxy_set_header - custom header from site.conf (For Varnish Cache)Nginx 代理 - proxy_set_header - 来自 site.conf 的自定义标头(用于 Varnish 缓存)
【发布时间】:2020-12-30 14:40:18
【问题描述】:

是否可以在您的网站中发送自定义标头,然后使用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;

我想做什么:

  • 我定义了这个标题: add_header 'Custom-Location-Site' 'mysite1' 在网站配置文件中。

  • 我想从每个网站的标头中接收_value_并将其转发到这里:
    proxy_set_header X-Forwarded-Custom-Location-Site $how_can_I_get_Custom-Location-Site_here;,然后让 Varnish 缓存在vcl_recv{} 中接收此标头

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

原因:

为多个 Magento 2 站点设置清漆缓存。

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

-   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

【问题讨论】:

    标签: nginx magento2 varnish magento-2.3


    【解决方案1】:

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

    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://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_,您可以将$http_ 与任何标题一起使用。您只需要将值小写,并用下划线替换破折号。

    【讨论】:

    • 这确实是我想做的,我希望能够从位置或站点块添加_header,然后将其转发到proxy_params。我不知道使用自定义标头$http_,我一直认为默认 proxy_params 和其他配置中的那些是内置变量。我会试试这个谢谢!
    • 我刚刚尝试过,但现在X-Forwarded-Custom-Location-Site 没有到达varnishlog 中的Varnish 缓存,它现在已经消失了。只要我在proxy_params 中再次添加一个静态值(就像我在示例中显示的那样),标题就会再次出现。我已经在 server {} 根块和 location / 块中尝试了 add_header 'Custom-Location-Site' 'site1-magento2.3.5'; 它,就像我上面的原始示例一样。
    • 如果我禁用 Varnish,#proxy_pass http://varnish-cache-magento2.3.5:6081; 并将位置设置为 Apache 网络服务器:proxy_pass http://web-server-apache2-magento2.3.5:8080; 我实际上在 Response 中看到了我的两个标题:Custom-Nginx-Server-Location-Root-Redirect: From / Custom-Location-Site: site1-magento2.3.5 。这确认 位置块已使用 但未随请求一起发送?有没有办法在使用proxy_pass 之前查看已处理/发送的 debug 标头信息。
    • 这里遗漏了一些东西,我在location / {} 块中创建了一个error_log /var/location/root-debug.log debug 条目,我看到了标题,但它记录在之后 http proxy header 条目X-Forwarded。这些自定义 $http 是否仅用于响应? /现在很困惑
    猜你喜欢
    • 2017-08-31
    • 2017-04-17
    • 2012-01-08
    • 2022-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    • 2021-09-10
    相关资源
    最近更新 更多