Is it possible to send a custom header in your website and then forward it using proxy_set_header
to Varnish?
I have moved folder locations a little bit - everything are included correctly and works.
I have /etc/nginx/conf.d/params/proxy.params/proxy_params
file with:
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;
Then in my https server block in 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;
The relevant 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;
I have this header defined :
add_header 'Custom-Location-Site' 'mysite1'
in the website conf file.
I want to receive the _value from that header for each website_and forward it here:
proxy_set_header X-Forwarded-Custom-Location-Site $how_can_I_get_Custom-Location-Site_here;
and then let Varnish cache receive this header in vcl_recv{}
This forwarded header I will then handle in vcl_recv{}
:
using req.http.X-Forwarded-Custom-Location-Site
as descibed in sample below.
Varnish cache setup for multiple Magento 2 sites.
PHP 7.2
, other PHP 7.3
websites, both running inside as /var/www/html, https
Link to sample multiple backends
Relevant section in sample:
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";
}
As shown here: varnishlog
already receives my custom headers in the Docker container. I just now need the value of the custom header to be dynamic.
- 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
If I understood correctly, this is what you need:
proxy_set_header X-Forwarded-Custom-Location-Site $http_custom_location_site;
This makes the X-Forwarded-Custom-Location-site
dynamic, based on the server
block where you configured the Custom-Location-Site
header.
If for example the value of Custom-Location-Site
is mysite1
, the X-Forwarded-Custom-Location-site
value will be the following:
X-Forwarded-Custom-Location-site: mysite1
According to http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_, you can use $http_
with any header. You just have to lowercase the value, and replace dashes with underscores.