Nginx - how to read the custom response header?

I am using nginx and trying to read a custom header from an internal page. $http_name variable doesn't work with my nginx/1.14.2 server.

if ($http_crawler != '1') {...}

Do you have any idea how to do this?

location /download/ {
    
# preserves bandwidth
limit_req zone=limit_by_addr burst=5 nodelay;
limit_req_status 429;

# all types of files except directories 
location ~ .+(?<!/)$ { 

    # force login for download
    auth_request /account/auth.php;
    error_page 401 = @login;
    
    # check if not googlebot
    # auth.php returns a custom header if detect googlebot "header('crawler: 1');"

    if ($http_crawler != '1') {
        
        # preserves bandwidth
        limit_conn conn_limit_per_ip 5;
        limit_rate_after 1000m;
        limit_rate 100k;
    }
}

}

1 answer

  • answered 2021-07-31 03:07 Sandra

    Resolved, the variable $http_name should be set with map in the nginx.conf file like this.

    file: nginx.conf

    map $http_crawler $crawler {
         default '0';
    }
    

    file: default

    if ($http_crawler = '1') {
            
       your code here
    }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum