Nginx - 如何读取自定义响应标头?

问题描述

我正在使用 Nginx 并尝试从内部页面读取自定义标头。 $http_name 变量不适用于我的 Nginx/1.14.2 服务器。

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

你知道怎么做吗?

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;
    }
}

}

解决方法

已解决,nginx.conf 文件中的变量 $http_name 应该这样设置。

文件:nginx.conf

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

文件:默认

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