问题
I deployed a Rails 3.2.8 application via Capistrano, with asset pipeline enabled, to my Linode server.
It is running nginx + unicorn.
When I visit my application, the minimised JS and CSS are not being served, although the assets are present in <RAILS_DIR>/public/assets
.
$ tree assets
assets
|-- application-66e477d6fd8cf088e8be44affeead089.css
|-- application-66e477d6fd8cf088e8be44affeead089.css.gz
|-- application-7d3ead38a0b5e276a97d48e52044ac31.js
|-- application-7d3ead38a0b5e276a97d48e52044ac31.js.gz
In my application, I can see those exact files not being found:
This is my nginx configuration:
server {
listen 80 default deferred;
server_name me.example.com;
root /home/kennym/apps/app/current/public;
location ^~ /assets/ {
add_header Last-Modified "";
add_header ETag "";
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @unicorn;
location @unicorn {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Can you guess what is wrong?
回答1:
location ^~ /assets/
should be location ~ ^/assets/
.
The former is does not match /assets/, the latter is matches a pattern that starts with /assets/
Update your nginx config to get caching and pre-gzipped file serving working again.
回答2:
I fixed this by commenting out the location ^~ /assets/
block in the nginx.conf
.
回答3:
For those having the same issue, for me the solution was to go into /etc/nginx/conf.d/default.conf
and set the root
field correctly.
来源:https://stackoverflow.com/questions/12657936/rails-nginx-not-serving-js-and-css