Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 468
user2139745 Avatar asked Dec 26 '13 08:12

user2139745


2 Answers

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

like image 153
coding addicted Avatar answered Nov 09 '22 23:11

coding addicted


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

like image 21
MustModify Avatar answered Nov 10 '22 01:11

MustModify