dlehman
dlehman

Reputation: 338

Rails showing images even though nginx misconfigured?

I have a Rails 5 app. I'm using the Carrierwave gem to allow image uploads to public/system/....

In reviewing production app for performance tweaks, I realized that I misconfigured nginx, and that it's only serving static files from /assets instead of /assets and /system.

What I have:

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

What I (think I) should have:

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

However, config.public_file_server.enabled = false is set in production.rb.

So now i'm confused-- how is Rails serving these images? I'm assuming I have a (grossly) incomplete understanding of how the asset pipeline actually works?

Update: nginx config

upstream puma {
  server unix:///home/deploy/apps/myapp/shared/sockets/mydomain.sock;
}

server {
  listen 80 default;
  server_name mydomain.com;

  root       /home/deploy/apps/myapp/current/public;
  access_log /home/deploy/apps/myapp/shared/log/nginx.access.log;
  error_log  /home/deploy/apps/myapp/shared/log/nginx.error.log info;

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

  location ~ ^/(robots.txt|sitemap.xml.gz)/ {
    root /home/deploy/apps/myapp/current/public;
  }

  try_files $uri/index.html $uri @puma;
  location @puma {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    proxy_pass http://puma;
  }

  location /cable {
    proxy_pass http://puma;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
  }

  error_page 500 502 503 504 /500.html;
  client_max_body_size 50M;
  keepalive_timeout 10;

  listen 443 ssl; # managed by Certbot
  # ssl certificate info...

}

Upvotes: 0

Views: 126

Answers (1)

goose3228
goose3228

Reputation: 371

It would help posting the whole nginx configuration for this application. Rails will respect public_file_server when served by passenger, puma etc. However, it can be easily overriden with nginx. The common nginx config line

root /home/rails/testapp/public;

basically tells nginx to serve /public "as it is" and makes public_file_server irrelevant. (perhaps).

Upvotes: 1

Related Questions