【问题标题】:rails - nginx + puma - static assets not being served by nginx from the tutorial link providedrails - nginx + puma - 来自提供的教程链接的 nginx 不提供静态资产
【发布时间】:2014-01-13 22:09:46
【问题描述】:

我正在使用 Ubuntu。 这是tutorial

我正在使用的 Nginx 配置:

upstream my_app {
server unix:///home/uname/railsproject/my_app.sock;
}

server {
listen 88; #(I used exact 88 when I am testing now)
server_name localhost; # I used exact localhost when I am testing this
root /home/uname/railsproject/public; # I assume your app is located at that location

location / {
 proxy_pass http://my_app; # match the name of upstream directive which is defined above
 proxy_set_header Host $host;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~* ^/assets/ {
 # Per RFC2616 - 1 year maximum expiry
 expires 1y;
 add_header Cache-Control public;

 # Some browsers still send conditional-GET requests if there's a
 # Last-Modified header or an ETag header even if they haven't
 # reached the expiry date sent in the Expires header.
 add_header Last-Modified "";
 add_header ETag "";
 break;
 }

}

彪马指令

RAILS_ENV=production puma -e production  -b unix:///home/uname/railsproject/my_app.sock -p 8000

在地址栏中,我正在输入

http://localhost/ 

然后网站打开,但静态资产不起作用。当然,我跑了

RAILS_ENV=production rake assets:precompile

并且资产在 public/assets 文件夹中可用
我还尝试将m.txt 文件放在资产目录中并访问

http://localhost/assets/m.txt    

但是没有用。我也试过这个命令:

sudo chown -R www-data:www-data public/

但这没有帮助。

【问题讨论】:

  • 端口80 上是否可能正在运行其他东西?看起来您的 nginx 正在侦听端口 88 但您正在通过执行 http://localhost/assets/m.txt 来测试端口 80
  • localhost:88 正在工作,谢谢,我想我需要使用 localhost 访问,无论如何,谢谢。
  • 您最终找到解决方案了吗?

标签: ruby-on-rails nginx puma


【解决方案1】:

我正在为未来的读者发帖。 更改托管服务提供商时遇到此错误。我的资产标准 nginx conf 停止工作,我必须更改它:

location ~ ^assets/ 
    {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
    }

在 /assets 之前删除 / 就可以了,不知道为什么。 ps:位置在服务器块中

【讨论】:

    【解决方案2】:

    我遇到了类似的问题。我从 irc 上非常有用的#nginx 频道得到了答案。他们说这是“惯用的 nginx”,而另一种形式虽然更受欢迎,但不受欢迎:

    server {
      server_name server.com;
    
      # path for static files
      root /home/production/server.com/current/public;
    
      location / {
          try_files $uri @proxy;
          # if url path access by directory name is wanted:
          #try_files $uri $uri/ @proxy;
      }
    
      location @proxy {
          proxy_pass  http://localhost:9292;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
      }
    }
    

    /hattip: 范德马尔

    【讨论】:

    • 上游指令和socket地址去哪了?他从教程中复制的配置开始像:server unix:///home/uname/railsproject/my_app.sock;
    猜你喜欢
    • 1970-01-01
    • 2012-05-11
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-20
    • 2017-08-14
    相关资源
    最近更新 更多