哪些内容直接提供

哪些内容直接提供

本文介绍了nginx 中的什么决定了哪些内容进入 Rails,哪些内容直接提供?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 nginx + Thin + Rails 3.2 设置.目前我正在尝试设置 nginx,以便它可以直接提供缓存页面.

I have a nginx + thin + Rails 3.2 setup. Currently I am trying to setup nginx so it can serve cached pages directly.

然而,nginx 仍然在以下 nginx 配置文件中将 *.html 请求传递给 Rails.html 文件存在于 public 文件夹中,nginx 确实找到了它们,只是它们仍然传递给 Rails.

However nginx still pass *.html request to Rails in the following nginx configuration files. The html files exists in public folder, and nginx does find them, just that they are still passed to Rails.

upstream site {
  server unix:/home/site/deploy/site/shared/pids/thin.0.sock;
}

server {
  listen 80;
  server_name www.example.com;
  rewrite ^(/.*) http://example.com$1 permanent;
}

# asset server
server {
  listen 80;
  server_name assets.example.com;
  expires max;
  add_header Cache-Control public;
  charset utf-8;
  root   /home/site/deploy/site/current/public/;
}

# frontend
server {
  listen 80;
  server_name .example.com;
  charset utf-8;

  root   /home/site/deploy/site/current/public/;
  index  index.html;

  location ~* ^.+.(jpg|jpeg|gif|png|swf|zip|rar|doc|xls|exe|pdf|ppt|txt|tar)$ {
    root /home/site/deploy/site/current/public/;
    expires max;
    break;
  }

  # serve static files
  if (-f $request_filename) {
    break;
  }

  gzip on;

  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (-f $request_filename) {
      break;
    }

    if (-f $document_root/cache/$host/$uri/index.html) {
      rewrite (.*) /cache/$host/$1/index.html break;
    }

    if (-f $document_root/cache/$host/$uri.html) {
      rewrite (.*) /cache/$host/$1.html break;
    }

    if (-f $document_root/cache/$host/$uri) {
      rewrite (.*) /cache/$host/$1 break;
    }

    proxy_pass http://site;
    break;
  }
}

我是 nginx 的新手,这个配置文件是从我之前没有做过的项目中复制过来的,所以这可能是一个非常菜鸟的问题.

I am new to nginx, and this configuration file is copied from previous projects I have not worked on, so this is probably a very newbie question.

推荐答案

为了让 nginx 提供静态缓存页面,你需要使用 try_files,让我分享一下配置:

To allow nginx to serve static cached pages you need to use try_files, let me share the config:

upstream site {
  server          unix:/home/site/deploy/site/shared/pids/thin.0.sock fail_timeout=0;
}

server {
  listen          80;
  server_name     www.example.com;
  rewrite ^(/.*)  http://example.com$1 permanent;
}

server {
  listen          80;
  server_name     example.com;
  charset         utf-8;

  root            /home/site/deploy/site/current;
  try_files       $uri/index.html $uri @thin;

  gzip on;

  location @thin {
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header  Host $http_host;
    proxy_redirect    off;
    proxy_pass        http://site;
  }

  location ~* ^.+.(jpg|jpeg|gif|png|swf|zip|rar|doc|xls|exe|pdf|ppt|txt|tar)$ {
    root             /home/site/deploy/site/current/public/;
    expires          max;
    break;
  }
}

这篇关于nginx 中的什么决定了哪些内容进入 Rails,哪些内容直接提供?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 08:44