NGINX:僅將搜尋機器人重新導向到給定文件

NGINX:僅將搜尋機器人重新導向到給定文件

我嘗試將以前的 .htaccess (與 apache 一起使用)移植到 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>

以下是我目前正在嘗試的:

我已經產生了一個地圖作為搜尋引擎列表:

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

但這會產生無限循環。

這是完整的伺服器區塊:

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

第二個問題: 我有另一個用於社交網路機器人的映射變數。我真的需要為每個映射添加一個自己的 if 子句,如下所示:

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

或者是否有一種更簡單的方法將這些組合到一個重寫規則中?

答案1

回答你的第二個問題,在翻譯中使用空字串而不是“0” map(並且由於預設值map正是空字串,因此你可以default完全省略該行):

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

並使用變數串聯進行最終條件決策:

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

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

相關內容