Nginx not serving precompiled assets in subdirectory - ruby-on-rails-3

I have a RoR3 server deployed in Amazon EC-2.
My assets are precompiled. I ran rake:assets:precompile as a part of my capistrano deployment task.
For example:
assets/home/separator.png is precompiled in public/assets/home/separator-7abf67950e9a27f371d2b5638de4320b.png
I cant see the separator.png in the file
public/assets/home/manifest.yml, why?
This is my nginx configuration for the assets:
location ~* ^/(assets)/ {
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
The Rails app is searching for assets/home/separator-f4571b5883207774be6edc5745de4755.png, which doesnt exists.
Any ideas?

Related

how to set content-encoding metadata tags in gzip header files?

I am using webpack's compression plugin to gzip the content of files(html, js) and then I published gzip files removing the extension .gz to server(AWS/Nginx/Apache).
compression plugin of webpack's configuration:
new CompressionPlugin({
'asset': "[path]",
'algorithm': "gzip",
'test': /\.js$|\.css$|\.html$/,
'threshold': 10240,
'minRatio': 0.8
})
I have tried AWS, Nginx and Apache. on AWS i have to set content-encoding metadata explicitly after uploading the files.
On Nginx there is a much simpler solution - http_gzip_static_module.
location / {
gzip_static on;
rewrite ^/?$ /index.html break;
root /srv/app;
}
by setting gzip_static on to Nginx server it provides correct Content-Type and Content-Encoding being passed to the client.
So in both AWS and on Nginx it's working fine and I am trying to achieve the same thing on apache server but it results in unexpected token in gzip files.
After debugging, I got to understand the files which are gzipped does not have content-encoding: gzip as metadata tags and resulting browser's request to fail.
I want to understand if there is any way in webpack compression plugin or in webpack where we can explicitly add metadata of file? or anyother way I can handle this issue.

Laravel : add Cors header for static files

I have a Laravel app, which was hosted on Apache, but now has been migrated on nginx. I'm a totally newbie with nginx.
On Apache I had this in my htaccess :
<IfModule mod_headers.c>
<FilesMatch "\.(svg|ttf|otf|eot|woff|woff2)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
</IfModule>
The new hosting provider does not allow custom nginx configuration.
Is it possible to add a Cors header (Access-Control-Allow-Origin: *) for static font files (extensions : svg|ttf|otf|eot|woff|woff2) in the Laravel app PHP code ? I tried (Adding Access-Control-Allow-Origin header response in Laravel 5.3 Passport) without success, my guess is that static files are not targeted by that piece of code. Do you confirm ?
Is there a way to achieve this within my app's PHP code ?
thanks
Use this in you server block or nginx.conf to apply globally.
location ~* \.(svg|ttf|otf|eot|woff|woff2)$ {
add_header Access-Control-Allow-Origin *;
}
Make sure to restart nginx server for changes to take effect.

rails serving static asset path which doesn't match precompiled manifest

I've tried reading through many other questions on how to properly configure nginx to serve static assets for rails 3.2 but no matter what I attempt the asset path being loaded by my browser does not match the asset version specified in manifest.yml after precompile and as a result all my assets are not found.
My nginx config is as follows:
location ~ ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
expires 1y;
add_header Cache-Control public;
gzip_static on;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
I've also checked the root path in nginx is correct. I'm using nginx with unicorn via a Unix Domain Socket.
First time I set this up everything loaded fine. Then I modified an asset and re-deployed. That asset was then broken. I then bumped the asset version and now all assets are broken. I've tried clearing my local cache in case that was causing problems but it didn't help.
I'm starting to tear my hair out at this point, any help would be greatly appreciated.
After more investigation I found the unicorn process wasn't properly restarting when updating.

How to serve cached ruby apps from nginx rather than unicorn?

I have a rails 3.0 app, and it has a good amount of traffic, the app it's running through the combination of Nginx and Unicorn. The thing is that unicorn and it's workers consume a lot of resources, and because of the nature of my app, a lot of records are pulled from the database, then it's like serving almost static files generated with those database records
I was wondering if you can generate this kind of static files, cache them, serve them through nginx instead of the app through unicorn to use less resources and kind of reloading the cache after 1000 request
I'm beginning my research about that, I don't know a lot of server configuration, so I hope you guys have any advise for me, it would be great!
thanks!
I assume you mean How do I serve my static assets from nginx rather than Unicorn
I just solved this problem and here is a snippet of my nginx.conf
# Prefer to serve static files directly from nginx to avoid unnecessary
# data copies from the application server.
try_files $uri/index.html $uri.html $uri #app;
# Set Far Future Cache on Static Assets
# All requests starting with /xyz/ where xyz is
# one of the options below (~* == case insensitive)
location ~* ^/(images|javascripts|stylesheets)/ {
# Per RFC2616 - 1 year maximum expiry
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional-GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
location #app { ... }
I am using Rails 3.0.10 so you my need something like ^/assets/ instead. The ~* directive tells nginx to do a case in-sensitive reg-ex compare. Also you don't need to escape the backslashes as you would in other languages.
Here is the Nginx documentation on that: http://wiki.nginx.org/HttpCoreModule#location

Apache caching problem

I'm loading some json through apache as per:
http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/
The JSON however is outdated when I use the apache url. The node.js :8000 url serves the correct data.
How can I make sure apache doesn't cache json?
Thanks.
You can append a "cache killer" on the URL you are fetching asynchronously. That is some value that will always make the URL unique.
var url = "http://example.com/service.json?" + new Date().getTime();
A possible solution would be to setup the expire headers to the past and make sure that the browser does not cache nay json via cache-control haders for json files and
You can try to add this to your apache config file :
<FilesMatch "\.(json|json)$">
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Thu, 01 Jan 1970 00:00:00 GMT"
</FilesMatch>
The mod_headers module will need to be installed in Apache to use this method.
If you are interested you can have a read at the roots
http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9