configuring nginx to serve static json files - ruby-on-rails

Switching to nginx for a site, one issue I'm having is serving up static json files.
I added to the mime types:
application/zip zip;
...
application/json json;
...
and restarted but it tried serving it up as a download (ie http://domain.com/json-tmp/locations.json). What else would I need to configure?
thx

I had to add
application/json json;
in /etc/nginx/mime.types and restart nginx to make it work.

I know this post is quite old but adding application/json mime type to nginx configuration file plus restarting the server should work.
When you request the json file try to debug the response header and check if the Content-Type header was successfully changed to application/json.

Try serving it as javascript ...
application/javascript json;
Or as plain text ...
text/plain txt json;
I'll try the javascript first.

Related

Rails sending gzip or uncompressed file

As per rails guides, compression is on atuomatically for assets unless specifically defined differently config.assets.gzip. These, and the uncompressed files are being generated on the application's shared/assets folder.
Context: nginx server version 1.14 + Phusion Passegner 6.0 running
How can I tell whether the call to the server is invoking the .gz vs uncompressed asset (assuming the client browser is not using the cached version)?
In the client request, there will be the Accept-Encoding header
It may contain smth like gzip, deflate, br
In this case, gzipped resource will be sent if there's one available. In the response headers there will be Content-Encoding: gzip then

Nginx not rendering .json URLs - Rails

I have a Rails 4 application with nginx 1.4.4 and I'm having issues trying to access JSON routes.
I'm trying to access the following route:
http://www.example.com/products/106.json
Which produces a 404 / Not found in my nginx server. However, in my development server it works correctly (a Thin server), it displays the product as JSON.
I have already added the application/json json; line to the mime.types config file.
What else should I look in order to fix this? I can provide more information if needed. Thanks.

net::ERR_CONTENT_DECODING_FAILED when using Rack::Deflater on jruby

net::ERR_CONTENT_DECODING_FAILED
I am receiving this in chrome when Rack::Deflater is enabled, when using on jruby.
After having some reading it looks like this is some jruby issue with gzipping.
Is there any alternative for serving gzipped assets? And also dynamically gzipping responses?
I am hosting the application on heroku. Rails version is 3.2.X
I think this is related with this issue:
https://github.com/jruby/jruby/issues/1371#ref-issue-15218894
This error comes by browser
The response should contain the header telling the client which compression scheme is being used: Content-Encoding: gzip and
The request should contain the correct header: "Accept-Encoding: gzip, deflate"

Change headers for a specific file

I have a manifest.webapp file in my app's public folder.
I can access it in normally, but the request came as text/plain, and I need it to be application/x-web-app-manifest+json.
I tried to put the following in my environment:
Mime::Type.register "application/x-web-app-manifest+json", :webapp
But it didn't work as expected. Probably I'm missing something.
How can I fix this?
Thanks in advance.
[EDIT]
Only for local tests, in remote I just changed the nginx mime.types file.
You need to update your mime files of your web server (Nginx, Lighttpd or Apache).
I use this link for helping me : http://9elements.com/io/index.php/make-ogg-video-work-with-rails/

Compressing a JSON response from a Rails app

We have an app that queries for locations for a customer. We're getting to the point where some customers could have upwards of 10,000 locations. The JSON response for this can get quite large, over 1mb sometimes.
I'm wondering first off the best way to compress this. We have apache in front of a Rails app running in trinidad with JRuby. Can I just set mod_deflate to always compress any responses that are application/json? How might I go about doing this?
Next, what is the browser support for gzip'd json? When I gzip a sample response of 200k it goes down to 30k. That's a significant savings. We're really like to be able to minimize the size of that response without having to minimize the number of locations returned.
In general, for newer versions of Rails, you can do it by adding
use Rack::Deflater
before the "run" line in the config.ru file. This will work perfectly with browsers/clients that support gzip. We use it in production on major websites.
Note for JRuby users: This assumes that your Rails app is launched through Rack, which it often isn't for JRuby. You need a recent version of JRuby-Rack and configure it in Warbler to run in 'rack' mode instead of 'rails' mode.
If the browser supports gzip'd/deflated data, then JSON will go through it just fine. AJAX data is just a regular HTTP request that was done on behalf of a script, rather than a human. At the HTTP level, there's absolutely zero difference between transferring some HTML or a JSON string - it's just data.
For Googlers... [I'm running Apache 2.2.16 and don't care about IE6]
JSON Responses with Content-Encoding = gzip didn't happen until I edit mod_deflate.conf to include this:
AddOutputFilterByType DEFLATE application/json
You can check server response headers with Firefox / Firebug / Net tab
First, make sure you have apache's mod_deflate installed by running this command.
a2enmod deflate
If this command installed it, restart apache. If not, you're good for now.
service apache2 restart
In apache2.conf usually located at /etc/apache2 append this line to the end of the file. This will include a file that we'll create in a little.
Include mod_deflate.conf
Next, we'll edit the mod_deflate.conf with our options:
SetOutputFilter DEFLATE
SetEnvIfNoCase Request_URI \
\.(?:exe|t?gz|zip|bz2|sit|rar)$ \
no-gzip dont-vary
SetEnvIfNoCase Request_URI \.pdf$ no-gzip dont-vary
#Skip browsers with known problems
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
The first block of code disables gzipping exes, gzs, pdfs.. etc
The second block of code skips deflation from browsers that don't support it.
Finally, restart apache again
service apache2 restart
The settings were copied from the link below:
http://www.howtoforge.com/apache2_mod_deflate

Resources