【问题标题】:Enabling content-type application/json for all the json files in nginx为 nginx 中的所有 json 文件启用 content-type application/json
【发布时间】:2014-09-21 04:20:37
【问题描述】:

目前我面临一些关于在 NGINX 中启用内容类型的问题。 你可以在下面的 nginx.conf 中看到默认的内容类型是application/octet-stream

我在 NGINX 中配置了 5 个虚拟主机,我想使用 Content-Type application/json 提供所有 json 文件。

我添加了“application/json json;”在 mime.type 文件中并重新启动了 nginx 服务,但是当我访问任何 json 文件时,它仍然来自 content-type application/octet-stream。

我还需要做什么才能将 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

我的 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;
}

我的 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;
}

请帮我解决这个问题

【问题讨论】:

  • 如果您希望 所有 文件为 JSON,您可以尝试将 default_type 设置为 application/json。它没有解释为什么你所做的事情不起作用。您是重新启动 Nginx,还是在这些更改后重新加载?此外,是否有一个名为 /abc/def/ghi/jkl/mno.json 的实际文件,或者它是一个写出 JSON 的应用程序?如果是应用程序,那么应用程序可能需要设置mime-type header,而不是依赖Nginx来做。

标签: json nginx content-type


【解决方案1】:

好的,我解决了这个问题。

  • 应修改./conf/mime.types.default添加
    application/json json;

  • ./sbin/nginx -s reload

【讨论】:

  • 默认情况下就在那里
【解决方案2】:

如果您的 json 文件没有扩展名,在测试短语中您可以将 nginx.conf 修改为如下内容:

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

然后没有扩展名的文件使用Content-Type: application/json

【讨论】:

    猜你喜欢
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2015-08-30
    • 1970-01-01
    • 2020-04-23
    相关资源
    最近更新 更多