Don Giulio
Don Giulio

Reputation: 3284

nginx not serving rails assets in ssl

I'm installing a self signed ssl certificate in my nginx (nginx version: nginx/1.4.1 (Ubuntu)), rails (Rails 3.2.16) and unicorn (unicorn v4.7.0) vps.

Everything was looking nice without ssl, but since I introduced it the assets stopped working.

here's my /etc/nginx/sites-enabled/[appname] configuration:

upstream unicorn {
  server 0.0.0.0:3000 fail_timeout=0;
}

server {
  listen [server ip]:80;
  server_name [server ip];

  location / {
    rewrite ^ https://$server_name$request_uri permanent;
  }
}

server {
  listen [server ip]:443 ssl;
  server_name [server ip];

  client_max_body_size 4G;
  keepalive_timeout 5;

  root /public;

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

  ssl_certificate      /srv/ssl/nginx.pem;
  ssl_certificate_key  /srv/ssl/nginx.key;

  ssl_session_timeout  5m;

  ssl_protocols  SSLv2 SSLv3 TLSv1;
  ssl_ciphers  HIGH:!aNULL:!MD5;
  ssl_prefer_server_ciphers   on;


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


  location @unicorn {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_intercept_errors on;

    auth_basic "Restricted";
    auth_basic_user_file /var/www/[app name]/releases/20140212165132/.htpasswd;

    proxy_pass http://unicorn;
  }

}

when I load the page, I only get the plain html, without css or images.

and when I load the assets directly I get a:

404 Not Found
nginx/1.4.1 (Ubuntu)

and in the logs:

==> nginx_error.log <==
2014/02/12 17:10:03 [error] 3307#0: *59 open() "/public/assets/logo-140-4ddb26fedaa75220d5322c85c9bd7050.png" failed (2: No such file or directory), client: [user ip], server: [server ip], request: "GET /assets/logo-140-4ddb26fedaa75220d5322c85c9bd7050.png HTTP/1.1", host: "[server ip]"

==> nginx_access.log <==
[user ip] - giulio [12/Feb/2014:17:10:03 +0000] "GET /assets/logo-140-4ddb26fedaa75220d5322c85c9bd7050.png HTTP/1.1" 404 208 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"

any clue?

thanks in advance

Upvotes: 3

Views: 1305

Answers (1)

MikeiLL
MikeiLL

Reputation: 6550

The answer, as commented above, is that

root /public;

needs to contain the full path to the public folder. Possibly something like

root /home/user/apps/app_name/current/public;

Upvotes: 1

Related Questions