nginx,prerender和角度路由问题

时间:2016-04-28 13:48:57

标签: angularjs nginx seo prerender

我正在开发一个动态angular网站(使用html5mode删除#)。它使用Elasticsearch来获取一些数据并呈现内容。 nginx负责映射到Elasticsearch和我的网站root。

为了让整个网站具备SEO功能,我尝试使用本地实例(http://localhost:3000)集成prerender

我正在拼命尝试将prerender魔法与nginx location /angular_test结合使用。默认的nginx prerender中间件配置不适用于location。在下面找到我的nginx配置:

upstream elasticsearch {
    zone elasticsearch 64K;
    server localhost:9200;
}

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

    root /var/www/html;

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

    server_name angular_test_server;

    location /angular_test {
        rewrite_log on;
        error_log /var/log/nginx/localhost.error_log notice;

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

        if ($prerender = 1) {
        rewrite .* /$scheme://$host$request_uri?;
        proxy_pass http://localhost:3000;
        break;
        }

        # support html5mode of angular (fallback to index.html and let angular do the routing)
    try_files $uri $uri/ /angular_test/index.html;
    }

    location /es/ {
        rewrite /es/(.*) /$1 break;
        proxy_pass http://elasticsearch;
        proxy_redirect http://elasticsearch /es/;
    }

}

当我访问http://localhost/angular_test/search?_escaped_fragment_=时,我得到404,因为nginx尝试加载文件/var/html/www/angular_test/search。这应该由angulars路由覆盖(请参阅try_files在index.html上进行尝试)。 http://localhost/angular_test?_escaped_fragment_=工作得很好

当我使用以下内容时(如https://gist.github.com/thoop/8165802所述),它甚至不会进入@prerender位置部分。

location /angular_test {
    # support html5mode of angular (fallback to index.html and let angular do the routing)
    try_files $uri $uri/ /angular_test/index.html @prerender;
}

有没有办法让这个设置与prerender一起运行?我觉得我没有在这里以正确的方式使用nginx ...

1 个答案:

答案 0 :(得分:2)

我找到了解决方案。 nginx位置If is really evil

if(...)逻辑移出locationserver时,它会起作用。这是我的最终配置:

upstream elasticsearch {
    zone elasticsearch 64K;
    server localhost:9200;
}

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

    root /var/www/html;

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

    server_name angular_test_server;

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

    location /angular_test {
        rewrite_log on;
        error_log /var/log/nginx/localhost.error_log notice;

        #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 "$host:3000";
            rewrite .* /$scheme://$host$request_uri? break;
            proxy_pass http://localhost:3000;
        }

        # support html5mode of angular (fallback to index.html and let angular do the routing)
        try_files $uri $uri/ /angular_test/index.html;
    }

    location /es/ {
        rewrite /es/(.*) /$1 break;
        proxy_pass http://elasticsearch;
        proxy_redirect http://elasticsearch /es/;
    }

}