rails - nginx + puma - static assets not being served by nginx from the tutorial link provided

前提是你 提交于 2019-12-12 08:06:37

问题


I am using Ubuntu. Here is the tutorial

Nginx config I am using:

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

}

Puma command

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

In the address bar, I am typing

http://localhost/ 

and then website opening but static assets not working. Of course, I ran

RAILS_ENV=production rake assets:precompile

and assets are available in public/assets folder
I also tried placing m.txt file in assets directory and accessing

http://localhost/assets/m.txt    

but didn't work. I also tried this command:

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

but this didn't help.


回答1:


I'm posting for future readers. I encounter this error when I change my hosting provider. My standard nginx conf for assets stop working, I have to change it for:

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

Removing the / before /assets did the trick, don't know why. ps: location is in the server block




回答2:


I had a similar problem. I got an answer from the very helpful #nginx channel on irc. They said it was "idiomatic nginx" and that the other form, though more popular, was discouraged:

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: vandemar



来源:https://stackoverflow.com/questions/20781682/rails-nginx-puma-static-assets-not-being-served-by-nginx-from-the-tutorial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!