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

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

但是我的 unicorn 日志仍然显示它正在提供文件

[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

等等

我错过了什么?

最佳答案

您需要明确告诉 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;
}

关于nginx - 使用 NGINX 为 UNICORN 服务的应用程序提供静态文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26186048/

相关文章:

ruby 彩虹/ unicorn 开始 faye and rails

ruby-on-rails - unicorn 无法分配内存

docker - Dockerized Nginx无法加载证书

django - 如何让 NGINX/Django 检测何时删除了 CAC/智能卡?

对 gitlab 6.5 的 HTTPS 请求超时

ruby-on-rails - 我可以对我的 heroku rails 应用程序上的 unicorn::clientshutdown 错误做些什么?

ruby-on-rails - Nginx:无法上传 > 10MB 的文件。客户打算发送过大的正文

nginx - API网关与反向代理

node.js - 设置 nginx 来提供静态文件(如果存在),或者让服务器创建它(如果不存在)

nginx - 在 Nginx 中随机返回备用页面以进行 A/B 测试