I'm deploying RoR application to Amazon Elastic Beanstalk. Static files are served with nginx. App root is: /var/app/current
, it contains current Rails application. public
dir tree:
=> /var/app/current/public
drwxr-xr-x 6 webapp webapp 4096 Aug 31 12:53 .
drwxr-xr-x 13 webapp webapp 4096 Aug 31 12:54 ..
-rw-r--r-- 1 webapp webapp 1722 Aug 31 12:52 404.html
-rw-r--r-- 1 webapp webapp 1705 Aug 31 12:52 422.html
-rw-r--r-- 1 webapp webapp 1635 Aug 31 12:52 500.html
-rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 apple-touch-icon.png
-rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 apple-touch-icon-precomposed.png
drwxr-xr-x 3 webapp webapp 4096 Aug 31 12:54 assets
drwxr-xr-x 2 webapp webapp 4096 Aug 31 12:52 debug
-rw-r--r-- 1 webapp webapp 0 Aug 31 12:52 favicon.ico
drwxr-xr-x 4 webapp webapp 4096 Aug 31 12:52 fonts
drwxr-xr-x 3 webapp webapp 4096 Aug 31 12:52 images
-rw-r--r-- 1 webapp webapp 98 Aug 31 12:52 robots.txt
The problem is assets are not served completely. Here's my nginx config I'm pushing to EBS servers:
# 0.02
upstream my_app {
server unix:///var/run/puma/my_app.sock;
}
log_format healthd '$msec"$uri"'
'$status"$request_time"$upstream_response_time"'
'$http_x_forwarded_for';
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
location /assets {
alias /var/app/current/public/assets;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /fonts {
alias /var/app/current/public/fonts;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /images {
alias /var/app/current/public/images;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
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;
}
}
Blocks /assets
and /fonts
are served poperly, returning code 200.
But /images
return 301, redirecting to URL with trailing slash, which cannot be found:
If I request PNG image with curl
, image is served properly. Redirection /images/stubs/banner.png
-> 301 => /images/stubs/banner.png/
-> 404 happens only in browser.
Any ideas? Thank you in advance.
curl -v http://myapp.com/image.png
to get a fresh copy of the response every time.map
directive. Then,gzip_static
means that nginx will serve the pre-compressed version of a resource, if it exists. For example, if/image.png.gz
exists, nginx will send that file directly when a request for/image.png
is made. As said below, gzipping images isn't useful. For fonts and JS/CSS files it is useful, and best result can be achieved with pre-compression of files.