【问题标题】:Serving static files with NGINX for UNICORN served app使用 NGINX 为 UNICORN 服务的应用程序提供静态文件
【发布时间】:2023-03-15 19:15:01
【问题描述】:

我刚刚设置了一个在 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

等等

我错过了什么?

【问题讨论】:

  • 这不是 Ruby 或 Sinatra 问题。这是一个 nginx 配置问题,真的应该在Webmasters 上询问。
  • @theTinMan 服务器故障怎么办?他们甚至在网站管理员上都没有独角兽标签。我已经发布了服务器故障,但没有得到任何回复,所以我在这里尝试。 Stack 上有大量的 nginx 配置问题。
  • Server Fault 用于“专业系统和网络管理员”。那是系统管理员和网络工程师类型。虽然它们可能与 nginx 配置和管理重叠,但Webmasters 的重叠要大得多。而且,虽然Stack Overflow 处理了一些对编程工具的应用程序支持,但 nginx 配置与 vim、emacs、svn、git 等相比并没有那么密切的关系。所以对于各种主题,会有更多的 Q/A,因为它存在的时间更长。这些问题仍然存在并不能使相同的主题现在有效。这就是新的特殊兴趣部分的开始方式。

标签: nginx unicorn


【解决方案1】:

您需要明确告诉网络服务器通过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;
}

【讨论】:

  • 我刚试过这个,我认为这是一个很好的第一步,因为现在根本没有静态文件通过,我的页面没有样式和脚本。 directory index of "/home/ubuntu/app/" is forbidden我猜我可能需要以某种方式定位公用文件夹,以便实际找到文件......有什么想法吗?
  • 修正你的答案以包含这些更改` location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { root /home/ubuntu/app/public;最大过期; log_not_found 关闭; add_header 缓存控制公共; }` 我会接受的。感谢您的帮助
  • 完成。谢谢。很高兴能提供帮助。
猜你喜欢
  • 2016-12-23
  • 2015-08-15
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
相关资源
最近更新 更多