使用NGINX为UNICORN提供静态文件服务应用程序

时间:2014-10-03 20:08:36

标签: nginx unicorn

我刚刚设置了一个运行在Unicorn上的Sinatra应用程序,并通过NGINX的套接字提供服务。

我正试图让NGINX在我的nginx配置中使用它来提供我的静态资产:

upstream unicorn {
  server unix:/tmp/unicorn.app.sock fail_timeout=0;
}

server {
  listen 80 default;
  server_name localhost;
  root /home/ubuntu/app;

  location ^~ /public/ {
    root /home/ubuntu/app;
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  try_files $uri/index.html $uri @unicorn;

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

  error_page 500 502 503 504 /500.html;
  client_max_body_size 4G;
  keepalive_timeout 10;
}

但我的独角兽日志仍显示它正在提供文件

[03/Oct/2014 19:25:05] "GET /javascript/map.js HTTP/1.0" 304 - 0.0016
[03/Oct/2014 19:25:05] "GET /javascript/geopo.js HTTP/1.0" 304 - 0.0030
[03/Oct/2014 19:25:05] "GET /images/logo.png HTTP/1.0" 304 - 0.0022

我错过了什么?

1 个答案:

答案 0 :(得分:4)

您希望明确告诉Web服务器通过location指令加载这些文件。

以下是我使用Unicorn让Web服务器加载资产的示例:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
  root /home/ubuntu/app/public;
  expires max;
  add_header Cache-Control public;
  log_not_found off;
}
相关问题