Nginx代理-proxy_set_header-来自site.conf的自定义 header (用于Varnish缓存)

标签 nginx varnish magento-2.3.5

是否可以在您的网站中发送自定义 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;
然后在sites-enabled/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;

相关的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 :
    网站conf文件中的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{}中接收此 header
  • 然后我将使用vcl_recv{}处理此转发的 header :
    使用下面示例中描述的req.http.X-Forwarded-Custom-Location-Site

  • 原因:

    Varnish cache setup for multiple Magento 2 sites.


  • 下面的Varnish文章中的示例显示了如何使用路径做到这一点。我无法使用路径,我有多个运行不同网站的Docker容器,然后将其转发到Varnish容器。例如,运行PHP 7.2的Container以及其他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
    如此处所示: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 header 的server块使Custom-Location-Site动态化。
    例如,如果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 一起使用。您只需要小写该值,并用下划线替换破折号即可。

    关于Nginx代理-proxy_set_header-来自site.conf的自定义 header (用于Varnish缓存),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63857806/

    相关文章:

    nginx条件代理通过不同的上下文

    bash 脚本语法错误 : word unexpected (expecting "do")

    ruby-on-rails - 生产服务器上的 Bundler 找不到任何 gem

    magento - 电子商务中的 Varnish

    header - Varnish - 生成 http header 溢出

    apache - [proxy_fcgi :error](70007)The timeout specified has expired

    javascript - 如何更正 NginX 反向代理配置中的 Meteor base-url?