Home > Software engineering >  How to remove slash from $http_referer variable
How to remove slash from $http_referer variable

Time:09-30

I want to use NGINX as dynamic local CDN with proxy_pass based on Referer header and $http_refere variable. The problem is that the Referer (variable too) header contains a "/" at the end of the URL. I want to delete trailing slash in $http_referer. How can I do it?

My NGINX location:

location / {
  add_header 'Access-Control-Allow-Origin' '*';
  add_header Referrer-Policy 'strict-origin';
  proxy_pass $http_referer;
}

CodePudding user response:

You can do it either via if block:

set $proxy $http_referer;
if ($http_referer ~ ^(.*)/$) { set $proxy $1; }

or via map directive:

map $http_referer $proxy {
    ~^(.*)/$  $1;
    default   $http_referer;
}

Then use the $proxy variable with the proxy_pass directive:

proxy_pass $proxy;
  • Related