大网数据广告活动
服务器运维问答

NGINX: Redirect only search-bots to a given file

· 52CE 我爱测

I try to port my previous .htaccess (used with apache) to nginx: <IfModule rewrite_module> RewriteEngine on RewriteCond "%{HTTP_USER_AGENT}" "(Googlebot|bingbot|slackbot|vkShare|W3C_Validator)" [NC] RewriteRule .* bot.php [L] Rew…

问题原文

原帖链接 · suther · CC BY-SA 4.0

I try to port my previous .htaccess (used with apache) to nginx:

<IfModule rewrite_module>   
        RewriteEngine on
        RewriteCond "%{HTTP_USER_AGENT}" "(Googlebot|bingbot|slackbot|vkShare|W3C_Validator)" [NC]
        RewriteRule .* bot.php [L]
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule .* index.html [L]
    </IfModule>

Following is, what I currently try:

I have generate a map as List of SearchEngine:

map $http_user_agent $search_engines {
default 0;
"~bingbot.*" 1;
"~BingPreview.*" 1;
"~Googlebot.*" 1;
}
if ($search_engines = 1){
   rewrite ^/(.*) bot.php?$1 break;
} 

But this creates an invinite Loop.

Here is the full server-block:

server {
    server_name mypage.de www.mypage.de;
    listen 1.1.1.1;
    root /home/mypage/public_html;
    index index.html index.htm index.php;
    access_log /var/log/virtualmin/mypage.de_access_log;
    error_log /var/log/virtualmin/mypage.de_error_log;
        
    fastcgi_param GATEWAY_INTERFACE CGI/1.1;
    fastcgi_param SERVER_SOFTWARE nginx;
    fastcgi_param QUERY_STRING $query_string;
    fastcgi_param REQUEST_METHOD $request_method;
    fastcgi_param CONTENT_TYPE $content_type;
    fastcgi_param CONTENT_LENGTH $content_length;
    fastcgi_param SCRIPT_FILENAME /home/mypage/public_html$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param REQUEST_URI $request_uri;
    fastcgi_param DOCUMENT_URI $document_uri;
    fastcgi_param DOCUMENT_ROOT /home/mypage/public_html;
    fastcgi_param SERVER_PROTOCOL $server_protocol;
    fastcgi_param REMOTE_ADDR $remote_addr;
    fastcgi_param REMOTE_PORT $remote_port;
    fastcgi_param SERVER_ADDR $server_addr;
    fastcgi_param SERVER_PORT $server_port;
    fastcgi_param SERVER_NAME $server_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    fastcgi_param HTTPS $https;
    location ~ \.php(/|$) {
        try_files $uri =404;
        fastcgi_pass unix:/var/php-nginx/123123123123123123.sock/socket;
    }
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    listen 1.1.1.1:443 ssl;
    ssl_certificate /home/mypage/ssl.combined;
    ssl_certificate_key /home/mypage/ssl.key;
    
    if ($blocked_bots = 1) {
        return 444; # Connection closed without response
    }
    if ($search_engines = 1){
        rewrite ^/(.*) /bot.php?$1 break;
    } 
    
    if ($scheme = http) {
        rewrite ^/(?!.well-known)(.*) https://mypage/$1 break;
    }
    
    location / {
        try_files $uri /index.html;
        auth_basic "Administrator’s Area";
        auth_basic_user_file /home/mypage/.htpasswd;
    }
    
    # Cache-Controll
    include /etc/nginx/conf.d/manuallyInclude/cache-policy.conf;
}

2nd Question: I have another mapped variarble for social_network bots. Do I really need to add for each mapping an own if-clause like this:

    if ($search_engines = 1){
        rewrite ^/(.*) /bot.php?$1 break;
    } 
    if ($social_networks = 1){
        rewrite ^/(.*) /bot.php?$1 break;
    } 

or is there an easier way to combine those to one rewrite-rule?

已采纳解答

原帖链接 · Ivan Shatsky · CC BY-SA 4.0

Answering your second question, use an empty string instead of "0" on your map translations (and since the default map value is exactly an empty string, you can omit the default line at all):

map $http_user_agent $search_engines {
    "~bingbot" 1;
    "~BingPreview" 1;
    "~Googlebot" 1;
}
map $http_user_agent $social_networks {
    "~*facebook" 1;
    "~*twitter" 1;
}

and use concatenation of variables for the final condition decision:

map $search_engines$social_networks $is_bot {
    ""      "";
    default 1;
}

server {
    ...
    if ($is_bot) {
        rewrite ^/(.*) /bot.php?$1 break;
    } 

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2020-12-05)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。