【问题标题】:Have nginx serve up all files in a certain directory让 nginx 提供某个目录中的所有文件
【发布时间】:2015-09-28 23:45:44
【问题描述】:

我想让 nginx 在我的 Rails 应用程序中提供 /public/system/articles/images 中的所有图像。我试过了 位置/系统{ 允许所有; }

当我放入 my_site.com/public/system/picture.png 时,它返回 Rails 404 页面(与 nginx 404 页面相反,或者我真正想要的图像)。

这是我当前的 conf 文件:

upstream varnish {
    server localhost:6081;
}

server {
    listen 80;
    server_name my_site.com;

    location ~* \.(ico|css|js|gif|jpe?g|png)\?[0-9]+?$ {
      expires max;
      break;
    }

  root /home/my_site/my_site/current/public;

  location /system {
    allow all;
  }

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_set_header X-AI-App "my_site";
    proxy_pass http://varnish;
  }

  #root /home/my_site/my_site/current/public;

  location / {
    # all IP addresses are listed here:
      allow all;

    try_files $uri @app;
  }

}

【问题讨论】:

  • 可能是你的正则表达式有问题。我刚试过这个。 file = "/system/articles/images/bar.png"; file =~ /\.(ico|css|js|gif|jpe?g|png)\?[0-9]+?$/ => nil
  • 如果我禁用该位置指令,我会得到同样的错误。
  • 但是,这是您想要使用它来提供静态图像的指令吗?如果是这样,您将需要修复正则表达式而不是禁用它。
  • 啊,对不起,我说得像泥巴一样!在 /system 中提供图像的指令是location /system { },稍微往下一点。根据nginx命令指令的方式,应该在正则表达式之前使用AFAIK,不是吗? nginx.org/en/docs/http/request_processing.html
  • 另外,如果我有一些未被该正则表达式捕获的东西,例如 /public/system/test_file.txt,我仍然会遇到同样的问题。

标签: ruby-on-rails nginx


【解决方案1】:

似乎我不需要对 nginx 配置文件做任何事情。

当您尝试访问 /public 目录下的内容时,不要将 /public 放在您调用的 URL 中。

所以我应该输入my_site.com/system/picture.png而不是my_site.com/public/system/picture.png

【讨论】:

    猜你喜欢
    • 2014-11-05
    • 2011-12-04
    • 2017-09-13
    • 2015-08-13
    • 2019-06-12
    • 2015-12-03
    • 2016-03-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多