Nginx 代理或 root 取决于用户代理

Nginx proxy or root depending on user agent

我有一个页面 application/progressive Web 应用程序,我使用 Nginx 作为静态文件使用以下设置为它提供服务:

server {
        listen 80;
        listen [::]:80;

        root /var/www/mydomain.com/html;

        index index.html index.htm index.nginx-debian.html;

        server_name mydomain www.mydomain;

        location / {
                sendfile           on;
                tcp_nopush on;
                sendfile_max_chunk 1m;

                tcp_nodelay       on;
                keepalive_timeout 65;

                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ /index.html;
        }

}

现在我想检查用户代理是否是,例如,Facebook 爬虫在这种情况下我想使用本地 NodeJS 服务器的代理,我对 Nginx 不太了解但是在谷歌搜索这个问题之后我最终得到以下代码:

server {
    listen 80;
    listen [::]:80;

    server_name mydomain www.mydomain;

    location / {
        try_files $uri @prerender;
    }

    location @prerender {
        set $prerender 0;
        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
            set $prerender 1;
        }
        if ($args ~ "_escaped_fragment_") {
            set $prerender 1;
        }
        if ($http_user_agent ~ "Prerender") {
            set $prerender 0;
        }
        if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
            set $prerender 0;
        }

        #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
        resolver 8.8.8.8;

        if ($prerender = 1) {

            #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
            set $prerender "localhost:3030";


            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$prerender;


        }
        if ($prerender = 0) {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            root /var/www/mydomain.com/html;
        }
    }
}

以上代码不起作用,我不知道为什么任何帮助将不胜感激。

你能试试这个吗?

server {
    listen 80;
    listen [::]:80;

    server_name mydomain www.mydomain;

    location / {
        set $prerender 0;
        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
            set $prerender 1;
        }
        if ($args ~ "_escaped_fragment_") {
            set $prerender 1;
        }
        if ($http_user_agent ~ "Prerender") {
            set $prerender 0;
        }
        if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
            set $prerender 0;
        }

        #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
        resolver 8.8.8.8;

        if ($prerender = 1) {

            #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
            set $prerender "localhost:3030";


            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://$prerender;


        }

        try_files $uri $uri/ /index.html;
    }
}

这是对我有用的最终代码,以防有人遇到同样的问题:

server {
    listen 80;
    listen [::]:80;

    server_name mydomain.com www.mydomain.com;

    location / {
        set $prerender 0;
        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator") {
            set $prerender 1;
        }
        if ($args ~ "_escaped_fragment_") {
            set $prerender 1;
        }
        if ($http_user_agent ~ "Prerender") {
            set $prerender 0;
        }
        if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent|ttf|woff)") {
            set $prerender 0;
        }

        #resolve using Google's DNS server to force DNS resolution and prevent caching of IPs
        resolver 8.8.8.8;

        if ($prerender = 1) {

            #setting prerender as a variable forces DNS resolution since nginx caches IPs and doesnt play well with load balancing
            set $prerender "localhost:3030";
            proxy_pass http://$prerender;

        }

        root /var/www/mydomain.com/html;
        try_files $uri $uri/ /index.html;
    }
}