使用nginx发布服务Ember app

时间:2015-06-18 15:24:59

标签: ember.js nginx

下面我的nginx配置为嵌套页面返回404,例如/博客经过一些更改后,将静态页面提供给抓取工具。问题是由这一行引起的:

if ($request_uri ~* "([^/]*$)")

似乎这个测试实际上是在重写request_uri - 但我该如何解决?

完整配置

server {
    root /home/user/code/app-client/dist;
    index index.html index.htm;
    listen 443 ssl;
    server_name www.example.com;
    listen [::]:80 default_server ipv6only=on;
    access_log  /home/user/code/app-server/log/nginx_access.log;
    error_log   /home/user/code/app-server/log/nginx_error.log info;

    location / {  
         set $snapshot 0;

# Extract all text after last / from url
    if ($request_uri ~* "([^/]*$)") {
  set $last_path_component $1;
}

if ($last_path_component = "") {
  set $last_path_component "home";
}

    # Google crawler
    if ($args ~* "(^|.*&)_escaped_fragment_=.*") {
        set $snapshot 1;
    }

    # Social sharing bots
    if ($http_user_agent ~* "Facebot|FacebookExternalHit|LinkedInBot|TwitterBot|Baiduspider") {
        set $snapshot 2;
    }

# Google crawler for pages other than homepage
    if ($arg__escaped_fragment_) {
       set $snapshot 3;
    }

# Google crawl for nested pages e.g. article/the-title
    if ($arg__escaped_fragment_ ~* "(/)") {
       set $snapshot 2;
    }

    #excluded suffixes (assumed static). Broken into two conditionals for readability
    if ($uri ~ "\.(jpe?g|png|svg|gif|ico|tiff?|css|less|js|doc|zip|rar|exe|iso|dmg)") {
        set $snapshot 0;
    }
    if ($uri ~ "\.(dat|ppt|psd|pdf|xls|mp3|mp4|m4a|wav|avi|mpe?g|swf|flv|mkv|torrent)") {
        set $snapshot 0;
    }

    #prevent loops
    if ($http_x_ajs_calltype) {
        set $snapshot 0;
    } 

# Google crawl for the homepage
if ($snapshot = 1) {
       rewrite ^(.*)$ /snapshot-proxyhome.html last;
    }

# Social media crawlers and crawler nested urls
if ($snapshot = 2) {
       rewrite ^(.*)$ /snapshot-proxy$last_path_component.html last;
}

# Google crawl for pages (non-nested)  other than homepage
    if ($snapshot = 3) {
   rewrite ^(.*)$ /snapshot-proxy$arg__escaped_fragment_.html last;
    }

try_files $uri /index.html;
    }
    location /static {
        root /home/user/code/app-server/;
    }
    location /assets {
        root /home/user/code/app-client/dist/; # do nothing and let nginx handle this as usual
    }
}

#redirect  to https
server {
    listen 80;
    server_name www.app.com;
    return 301 https://$server_name$request_uri;
}

2 个答案:

答案 0 :(得分:2)

try_files $uri /index.html;

这应该对你有用..它应该尝试从磁盘获取文件,如果它找不到它,它将获得index.html ..我不知道为什么你有{ {1}}在您的配置中,但删除它应该可以解决您的问题。

答案 1 :(得分:0)

好的,最后用地图解决了。一些说明:

  • 如果IsIsEvil在位 - 很多人指出了这一点,但我找不到另一种方法来实现我想要的 - 如果有人这样做,请评论或回答。此外,IfisEvil文档说如果不一致,那么如果在您的设置中进行了适当的测试,则可以使用它。
  • 谷歌抓取(_escaped_fragment_) - 很多人指出,如果你不使用#urls就不再需要了,但似乎并非如此。根据此Google doc,即使您不使用#urls,您仍需要告诉Google抓取您的网站 - 您可以通过在index.html中添加标头标记来实现此目的。我在几个地方读到谷歌这些天确实执行JS,但我们的网站并非如此。即使Google确实开始为所有网站执行JS,但目前没有一个主要的社交机器人,所以你需要能够为他们提供静态页面,否则你不会被他们编入索引,并且从你的网站分享看起来不会漂亮。
  • 我们网站的预渲染/静态页面是从我为此目的设置的cloudfront实例(snapshotproxy)提供的。有各种各样的服务可以为您完成所有这些,但它们需要花钱并且对社交共享有一些限制,例如:要使fb / twitter上的博客页面与图像,标题和摘录正确共享,您需要将必要的信息移动到头部的标题和描述标签中 - 这些服务的自动快照不会这样做)

_

map $request_uri $last_path_component {
       ~*(?P<last>[^/]*$) $last;
}

server {
        #your setup here
}

location / {
        set $snapshot 0;

        if ($last_path_component = "") {
           set $last_path_component "home";
        }

        # Google crawler
        if ($args ~* "(^|.*&)_escaped_fragment_=.*") {
            set $snapshot 1;
        }

        # Social sharing bots
        if ($http_user_agent ~* "Facebot|FacebookExternalHit|LinkedInBot|TwitterBot|Baiduspider") {
            set $snapshot 2;
        }

        # Google crawler for pages other than homepage
        if ($arg__escaped_fragment_) {
           set $snapshot 3;
        }

        # Google crawl for nested pages e.g. article/the-title
        if ($arg__escaped_fragment_ ~* "(/)") {
           set $snapshot 2;
        }

        #excluded suffixes (assumed static). Broken into two conditionals for readability
        if ($uri ~ "\.(jpe?g|png|svg|gif|ico|tiff?|css|less|js|doc|zip|rar|exe|iso|dmg)") {
            set $snapshot 0;
        }
        if ($uri ~ "\.(dat|ppt|psd|pdf|xls|mp3|mp4|m4a|wav|avi|mpe?g|swf|flv|mkv|torrent)") {
            set $snapshot 0;
        }

        #prevent loops
        if ($http_x_ajs_calltype) {
            set $snapshot 0;
        } 

       # Google crawl for the homepage
       if ($snapshot = 1) {
           rewrite ^(.*)$ /snapshot-proxyhome.html last;
       }

       # Social media crawlers and crawler nested urls
       if ($snapshot = 2) {
           rewrite ^(.*)$ /snapshot-proxy$last_path_component.html last;
       }

       # Google crawl for pages (non-nested)  other than homepage
        if ($snapshot = 3) {
          rewrite ^(.*)$ /snapshot-proxy$arg__escaped_fragment_.html last;
        }

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