Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling content-type application/json for all the json files in nginx

Currently I am facing some issue regarding enabling content-type in NGINX. You can see below in nginx.conf that the default content-type is application/octet-stream.

I have 5 virtual host configured in NGINX and I want to serve all the json files with Content-Type application/json.

I have added "application/json json;" in mime.type file and restarted the nginx service but still when I access any json file it is coming by content-type application/octet-stream.

Is there anything else I need to do to serve the JSON file as application/json?

### curl -v http://www.ajay.com/abc/def/ghi/jkl/mno.json

* Hostname was NOT found in DNS cache
*   Trying xx.xx.xx.xx...
* Connected to www.ajay.com (xx.xx.xx.xx) port 80 (#0)
> GET abc/def/ghi/jkl/mno.json HTTP/1.1
> User-Agent: curl/7.36.0
> Host: www.ajay.com
> Accept: */*
< HTTP/1.1 200 OK
< Accept-Ranges: bytes
< Cache-Control: max-age=86400
< **Content-Type: application/octet-stream**
< Date: Tue, 29 Jul 2014 17:03:53 GMT
< ETag: "ab9b7b2c58d3a481e172aea95b1e87a0"
< Expires: Wed, 30 Jul 2014 17:03:53 GMT
< Last-Modified: Fri, 25 Jul 2014 13:18:14 GMT
* Server nginx is not blacklisted
< Server: nginx
< Content-Length: 603
< Connection: keep-alive
.
.
.

[root@ajay nginx]# nginx -v
nginx version: nginx/1.4.3
[root@ajay nginx]# ls -lh mime.types
-rw-r--r-- 1 root root 3.5K Jul 29 10:36 mime.types

My mime.types file

[root@ajay nginx]# cat mime.types

types {
text/html                             html htm shtml;
text/css                              css;
text/xml                              xml;
image/gif                             gif;
image/jpeg                            jpeg jpg;
**application/json                      json;**
application/x-javascript              js;
application/atom+xml                  atom;
application/rss+xml                   rss;

text/mathml                           mml;
text/plain                            txt;
text/vnd.sun.j2me.app-descriptor      jad;
text/vnd.wap.wml                      wml;
text/x-component                      htc;

image/png                             png;
image/tiff                            tif tiff;
image/vnd.wap.wbmp                    wbmp;
image/x-icon                          ico;
image/x-jng                           jng;
image/x-ms-bmp                        bmp;
image/svg+xml                         svg svgz;
image/webp                            webp;

application/java-archive              jar war ear;
application/mac-binhex40              hqx;
application/msword                    doc;
application/pdf                       pdf;
application/postscript                ps eps ai;
application/rtf                       rtf;
application/vnd.ms-excel              xls;
application/vnd.ms-powerpoint         ppt;
application/vnd.wap.wmlc              wmlc;
application/vnd.google-earth.kml+xml  kml;
application/vnd.google-earth.kmz      kmz;
application/x-7z-compressed           7z;
application/x-cocoa                   cco;
application/x-java-archive-diff       jardiff;
application/x-java-jnlp-file          jnlp;
application/x-makeself                run;
application/x-perl                    pl pm;
application/x-pilot                   prc pdb;
application/x-rar-compressed          rar;
application/x-redhat-package-manager  rpm;
application/x-sea                     sea;
application/x-shockwave-flash         swf;
application/x-stuffit                 sit;
application/x-tcl                     tcl tk;
application/x-x509-ca-cert            der pem crt;
application/x-xpinstall               xpi;
application/xhtml+xml                 xhtml;
application/zip                       zip;

application/octet-stream              bin exe dll;
application/octet-stream              deb;
application/octet-stream              dmg;
application/octet-stream              eot;
application/octet-stream              iso img;
application/octet-stream              msi msp msm;

audio/midi                            mid midi kar;
audio/mpeg                            mp3;
audio/ogg                             ogg;
audio/x-m4a                           m4a;
audio/x-realaudio                     ra;

video/3gpp                            3gpp 3gp;
video/mp4                             mp4;
video/mpeg                            mpeg mpg;
video/quicktime                       mov;
video/webm                            webm;
video/x-flv                           flv;
video/x-m4v                           m4v;
video/x-mng                           mng;
video/x-ms-asf                        asx asf;
video/x-ms-wmv                        wmv;
 video/x-msvideo                       avi;
}

My nginx.conf file

user              nginx;
worker_processes  1;
error_log         /var/log/nginx/error.log  notice;
pid               /var/run/nginx.pid;
worker_rlimit_nofile 30000;
events {
    worker_connections  1024;
}
http {
    include       /etc/nginx/mime.types;
default_type  application/octet-stream;
log_format combined_time '$remote_addr - $remote_user [$time_local]'
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent" $request_time';
access_log  /var/log/nginx/access.log  combined_time;
## Size Limits
client_body_buffer_size      16k;
client_header_buffer_size     1k;
client_max_body_size         20k;
large_client_header_buffers 4 4k;
## Timeouts
client_body_timeout     5;
client_header_timeout   5;
keepalive_timeout     5 5;
send_timeout            5;
## General Options
ignore_invalid_headers   on;
keepalive_requests        2;
recursive_error_pages    on;
server_tokens           off;
server_name_in_redirect off;
sendfile                 on;
## TCP options
tcp_nopush  on;
tcp_nodelay on;
## Compression
gzip                 on;
gzip_static          on;
gzip_buffers      16 8k;
gzip_http_version   1.0;
gzip_comp_level       6;
gzip_min_length     100;
gzip_types          text/plain application/xml application/x-javascript  text/xml text/css image/x-icon image/gif image/jpeg;
gzip_vary            on;
gzip_disable        "MSIE [1-6]\.";
include /etc/nginx/conf.d/*.conf;
}

Please help me with this

like image 614
user3847894 Avatar asked Jul 29 '14 17:07

user3847894


2 Answers

OK, I fix this problem .

  • Should modify ./conf/mime.types.default add
    application/json json;

  • ./sbin/nginx -s reload

like image 151
Yans Ruan Avatar answered Oct 30 '22 15:10

Yans Ruan


If your json file has no extension, in test phrase you can modify the nginx.conf to something like this:

http {
    ...
    default_type application/json;
    ...
}

Then the files without extension are served with Content-Type: application/json.

like image 22
yuansi zhu Avatar answered Oct 30 '22 14:10

yuansi zhu