2016-01-23 76 views
13

这是一个AWS问题,我使用Ruby 2.2(美洲豹)平台。如何使用puma/nginx为不属于资产管道的资产/公共资产提供服务?

我编辑的资产(在/公共/资产)按预期投放。其他资产/公共资源没有被提供(404)。

我在哪里配置?这是一个nginx问题吗?或美洲狮问题?

或者这只是一个AWS图像问题?

下面是一个活生生的例子(robots.txt的应该从根送达): http://staging.us-west-2.elasticbeanstalk.com/public/robots.txt

另外值得一提的是,默认的乘客平台形象工程开箱。

+2

你能不能给我们在'public'目录的其他资产的名称/位置? –

+0

你的意思是像422.html,500.html,favicon.ico? – chrisp

+1

是的,你说你有公共目录中的资产,但不会显示,但有些会显示。为了进行有效的比较,我需要知道你有什么不会显示 –

回答

4

所以,我使用的是完全相同的环境中,我发现有一点谷歌赋的解决方案:

随着轨道4+,在该文件中:

/config/environments/production.rb 

你会发现下面的附近的文件

# Disable serving static files from the `/public` folder by default since 
# Apache or NGINX already handles this. 
config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? 

这是所有罚款和花花公子,因为我们使用的是乘客(nginx的或Apache),但彪马不处理的顶线是为我们:)

要解决此问题,请执行以下操作:

在您的AWS控制台中,转至相关项目的弹性beanstalk仪表板,然后单击左侧菜单中的“配置”。

现在点击标题为小齿轮图标“软件配置”

现在你应该看到在“环境属性”表中,输入“RAILS_SERVE_STATIC_FILES”到下的“属性名称”一个新的领域,然后键入'true'(不带引号)输入值字段,点击apply。

中提琴!现在你的项目正在服务静态文件:)

+6

应用程序服务器不应在生产中提供静态资产。对于静态文件的请求撞击Rails应用程序是低效的。 http://guides.rubyonrails.org/configuring.html – thebenedict

4

如果它可以帮助任何人,或者有人知道如何改善它,这里是nginx配置,最终它为我工作。在/.ebextensions/01_files.config:

files: 
    "/etc/nginx/conf.d/webapp_healthd.conf" : 
     mode: "000755" 
     owner: root 
     group: root 
     content: | 
      upstream my_app { 
       server unix:///var/run/puma/my_app.sock; 
      } 

      log_format healthd '$msec"$uri"' 
          '$status"$request_time"$upstream_response_time"' 
          '$http_x_forwarded_for'; 

      server { 
       listen 80; 
       server_name _ localhost; # need to listen to localhost for worker tier 
       root /var/app/current/public; 

       if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") { 
       set $year $1; 
       set $month $2; 
       set $day $3; 
       set $hour $4; 
       } 

       access_log /var/log/nginx/access.log main; 
       access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd; 

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

       location @my_app { 
       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 { 
       alias /var/app/current/public/assets; 
       gzip_static on; 
       gzip on; 
       expires max; 
       add_header Cache-Control public; 
       } 
      } 
    "/opt/elasticbeanstalk/hooks/appdeploy/post/03_restart_nginx.sh": 
     mode: "000755" 
     owner: root 
     group: root 
     content: | 
      #!/usr/bin/env bash 
      rm /etc/nginx/conf.d/webapp_healthd.conf.bak 
      rm /etc/nginx/conf.d/custom.conf    
      service nginx restart 
+0

我不愿意走这条路,以防EB对这个文件做一些修改。但是我需要对EB nginx配置进行其他一些修改,所以我最终采用了这种方法。 –

+0

这仍然适合你吗?即时通讯在相同的情况下,调整nginx配置似乎没有工作。它有点不清楚哪个nginx文件实际上被用于什么。我修改了你之前做过的一个 - 通过ssh去verfiy itll工作,重新启动nginx之后,还没有成功 –

+0

@RickyBrown是的,还在工作。不幸的是,我没有任何建议。 – thebenedict

0

我需要简单地运行bundle exec rake assets:precompile

相关问题