1

I'm serving a Rails application under a sub-uri like https://projects.mydomain.com/app1/

But my assets are not loading.

here is my nginx config :

location ~ ^/app1(/.*|$) {
    root /srv/www/app1/current/public;

    passenger_base_uri /app1;
    passenger_app_root /srv/www/app1/current;
    passenger_document_root /srv/www/app1/current/public;
    passenger_enabled on;
}

  location ~ ^/(assets)/ {
    root /srv/www/app1/current/public;
    gzip_static on;
    expires max;
    add_header Cache-Control public;
}

The application is loaded, with all URL/PATHes prefixed for /app1/ but the assets are not loaded.

https://projects.mydomain.com/app1/assets/application-123456.css Does NOT work

When I try the assets path without prefix, i can see the assets exists:

https://projects.mydomain.com/assets/application-123456.css DO work

Any idea ?

UPDATE:

Added error_log /var/log/nginx/error.log debug; Of course this is an edited extract:

2015/12/18 11:15:44 [debug] 27187#0: *1 http process request line
2015/12/18 11:15:44 [debug] 27187#0: *1 http request line: "GET /app1/assets/application-123456.css HTTP/1.1"
2015/12/18 11:15:44 [debug] 27187#0: *1 http uri: "/app1/assets/application-123456789precompiledhash123456789.css"
2015/12/18 11:15:44 [debug] 27187#0: *1 http args: ""
2015/12/18 11:15:44 [debug] 27187#0: *1 http exten: "css"
...
2015/12/18 11:15:44 [debug] 27187#0: *1 test location: ~ "^/app1/assets"
2015/12/18 11:15:44 [debug] 27187#0: *1 using configuration "^/app1/assets"
...
2015/12/18 11:15:44 [debug] 27187#0: *1 http finalize request: -4, "/app1/assets/application-123456.css?" a:1, c:2
2015/12/18 11:15:44 [debug] 27187#0: *1 http request count:2 blk:0
2015/12/18 11:15:44 [debug] 27187#0: *1 http run request: "/app1/assets/application-123456.css?"
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream check client, write event:1, "/app1/assets/application-123456.css"
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream recv(): -1 (11: Resource temporarily unavailable)
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream request: "/app1/assets/application-123456.css?"
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream dummy handler
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream request: "/app1/assets/application-123456.css?"
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream dummy handler
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream request: "/app1/assets/application-123456.css?"
2015/12/18 11:15:44 [debug] 27187#0: *1 http upstream process header
#...
2015/12/18 11:15:44 [debug] 27187#0: *1 recv: fd:26 1844 of 16384
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi status 404 "404 Not Found"
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "Status: 404 Not Found"
...
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "Content-Type: text/html; charset=utf-8"
...
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "Date: Fri, 18 Dec 2015 10:15:44 GMT"
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "Content-Length: 1564"
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "Connection: close"
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header: "X-Powered-By: Phusion Passenger"
2015/12/18 11:15:44 [debug] 27187#0: *1 http scgi header done
2015/12/18 11:15:44 [debug] 27187#0: *1 uploadprogress error-tracker error: 0
2015/12/18 11:15:44 [debug] 27187#0: *1 xslt filter header
2015/12/18 11:15:44 [debug] 27187#0: *1 HTTP/1.1 404 Not Found
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
Status: 404 Not Found
...
Date: Fri, 18 Dec 2015 10:15:44 GMT
X-Powered-By: Phusion Passenger 
Server: nginx + Phusion Passenger
Content-Encoding: gzip

As a additionnal information, when in this nginx location directive I try to replace the root directive by an alias one I get a 403.

  location ~ ^/app1/assets {
    alias /srv/www/app1/current/public/assets;
    #root /srv/www/app1/current/public;
    gzip_static on; # to serve pre-gzipped version
    expires max;
    add_header Cache-Control public;
    passenger_enabled on;
  }

I tried regexp like ^/app1/assets/.* but still not working. (403 forbidden for the alias)

UPDATE2:

2015/12/18 15:38:49 [debug] 878#0: *1 test location: ~ "^/app1/assets/(.+)$"
2015/12/18 15:38:49 [debug] 878#0: *1 using configuration "^/app1/assets/(.+)$"
2015/12/18 15:38:49 [debug] 878#0: *1 http cl:-1 max:262144000
2015/12/18 15:38:49 [debug] 878#0: *1 rewrite phase: 3
...
2015/12/18 15:38:49 [debug] 878#0: *1 content phase: 16
2015/12/18 15:38:49 [debug] 878#0: *1 http script copy: "/srv/www/app1/current/public/assets/"
2015/12/18 15:38:49 [debug] 878#0: *1 http script capture: "application-123456.css/"
2015/12/18 15:38:49 [debug] 878#0: *1 open index "/srv/www/app1/current/public/assets/application-123456.css/index.html"

Tried to add an index directive, not working (404).
Tried to add a try_files, still have a /index.html added at the end, therefore having same 404.
gdurelle
  • 111
  • 5
  • 1
    Did you try location `/app1/assets/`? If so, please enable the debug error log and show it for an asset request. – gxx Dec 17 '15 at 21:52
  • Hi, yes I tried that, and also directives like alias and intern, different regexp using /app1/assets, etc. Nothing do. I'll add a UPDATE section with the debug log. – gdurelle Dec 18 '15 at 10:19

1 Answers1

0

Got a friend's help. Here is how to works:

server {
  listen *:80;
  listen [::]:80;
  server_name projects.mydomain.com;
  server_tokens off;
  return 301 https://$server_name$request_uri;
}

server{
  listen 0.0.0.0:443 ssl;
  listen [::]:443;
  server_name projects.gdurelle.com;
  server_tokens off;

  location ~ ^/app1(/.*|$) {
    alias /srv/www/app1current/public$1;

    passenger_base_uri /app1;
    passenger_app_root /srv/www/app1/current;
    passenger_document_root /srv/www/app1/current/public;
    passenger_enabled on;
  }

  location ~ ^/app2(/.*|$) {
    alias /srv/www/app2/current/public$1;

    passenger_base_uri /app2;
    passenger_app_root /srv/www/app2/current;
    passenger_document_root /srv/www/app2/current/public;
    passenger_enabled on;
  }
}
gdurelle
  • 111
  • 5
  • The first location seems to have a typo `alias /srv/www/app1current/public$1;` should be `alias /srv/www/app1/current/public$1;` – pauljohn32 Jan 28 '20 at 19:04