简体   繁体   English

为 nginx 中的所有 json 文件启用 content-type application/json

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

Currently I am facing some issue regarding enabling content-type in NGINX.目前我面临着一些关于在 NGINX 中启用内容类型的问题。 You can see below in nginx.conf that the default content-type is application/octet-stream .您可以在下面的 nginx.conf 中看到默认内容类型是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.我在 NGINX 中配置了 5 个虚拟主机,我想使用 Content-Type application/json 为所有 json 文件提供服务。

I have added "application/json json;"我添加了“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.在 mime.type 文件中并重新启动了 nginx 服务,但是当我访问任何 json 文件时,它仍然来自内容类型应用程序/八位字节流。

Is there anything else I need to do to serve the JSON file as application/json?我还需要做什么才能将 JSON 文件作为 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我的 mime.types 文件

[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我的 nginx.conf 文件

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请在这件事上给予我帮助

OK, I fix this problem .好的,我解决了这个问题。

  • Should modify ./conf/mime.types.default add应该修改./conf/mime.types.default添加
    application/json json;

  • ./sbin/nginx -s reload

If your json file has no extension, in test phrase you can modify the nginx.conf to something like this:如果您的 json 文件没有扩展名,则在测试短语中您可以将nginx.conf修改为如下内容:

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

Then the files without extension are served with Content-Type: application/json .然后没有扩展名的文件使用Content-Type: application/json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM