如何使 NGINX 服务器 JSON 文件类型为 application/json 而不是 text/plain?

如何使 NGINX 服务器 JSON 文件类型为 application/json 而不是 text/plain?

我的服务器以 text/plain 形式提供“manifest.json”文件,这意味着浏览器无法读取它。

肯定有一条线/etc/nginx/config/mime.types

application/json                      json;

但是我在 chrome dev tools 中看到类型错误:

https://i.imgur.com/ClaTBS8.png

操作系统:Ubuntu 16.04.6

NGINX:1.13.1

/etc/nginx/config/nginx.conf:

#+----------------------------------------------------------------------------+
#+ NGINX Configuration v1.0.0
#+----------------------------------------------------------------------------+
pcre_jit                                                on;

timer_resolution 100ms;
user nginx nginx;

worker_priority -10;
worker_processes 1;
worker_rlimit_nofile 260000;

events {
    accept_mutex off;
    accept_mutex_delay 200ms;
    use epoll;
    worker_connections 10000;
}


http {
    #+------------------------------------------------------------------------+
    #+ Enable Brotli
    #+------------------------------------------------------------------------+
    brotli on;
    brotli_static on;
    brotli_min_length 1000;
    brotli_buffers 32 8k;
    brotli_comp_level 5;
    brotli_types *;

    #+------------------------------------------------------------------------+
    #+ client_max_body_size controls the maximum file upload size - this will
    #+ need to be modified should you need to allow file uploads over 50MB.
    #+------------------------------------------------------------------------+
    client_body_buffer_size 256k;
    client_body_in_file_only off;
    client_body_timeout 10s;
    client_header_buffer_size 64k;
    client_header_timeout 5s;
    client_max_body_size 50m;

    charset utf-8;
    connection_pool_size 512;
    default_type application/octet-stream;
    directio 4m;

    #+------------------------------------------------------------------------+
    #+ Enable GZIP
    #+------------------------------------------------------------------------+
    gzip on;
    gzip_vary on;
    gzip_disable "MSIE [1-6]\.";
    gzip_static on;
    gzip_min_length 1400;
    gzip_buffers 32 8k;
    gzip_http_version 1.0;
    gzip_comp_level 5;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml application/javascript application/x-javascript application/xml application/xml+rss application/ecmascript application/json image/svg+xml;

    ignore_invalid_headers on;
    include /etc/nginx/config/mime.types;
    index index.php index.html;

    keepalive_disable msie6;
    keepalive_requests 500;
    keepalive_timeout 5;

    large_client_header_buffers 8 64k;
    lingering_time 20s;
    lingering_timeout 5s;

    map_hash_bucket_size 128;
    map_hash_max_size 4096;

    open_file_cache max=50000 inactive=60s;
    open_file_cache_errors off;
    open_file_cache_min_uses 2;
    open_file_cache_valid 120s;
    open_log_file_cache max=10000 inactive=30s min_uses=2;

    output_buffers 8 256k;
    postpone_output 1460;

    proxy_temp_path /etc/nginx/cache/proxy;

    request_pool_size 32k;
    reset_timedout_connection on;
    sendfile on;
    sendfile_max_chunk 512k;
    send_timeout 10s;

    server_names_hash_bucket_size 128;
    server_names_hash_max_size 2048;
    server_name_in_redirect off;

    server_tokens off;

    tcp_nodelay on;
    tcp_nopush on;

    types_hash_max_size 2048;
    variables_hash_max_size 2048;

    include /etc/nginx/sites/*.conf;
}

/etc/nginx/config/sites/headers.conf

add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";

/etc/nginx/sites/_.conf

server
{
    listen 80 default_server;
    listen [::]:80;
    server_name _;

    root /home/nginx/htdocs/public;

    location /
    {
        try_files $uri $uri/ =404;
    }
}

/etc/nginx/sites/example.com.conf

upstream example {
    server 127.0.0.1:3000;
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com;

    return 301 https://$host$request_uri;
}

server
{
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;

    include /etc/nginx/config/sites/headers.conf;

    include /etc/nginx/config/ssl/resolver.conf;

    ssl on;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    include /etc/nginx/config/ssl/ssl.conf;

    location /
    {
        proxy_pass http://example;

        include /etc/nginx/config/proxy/proxy.conf;
    }

    #include /etc/nginx/config/cache/static.conf;
}

答案1

我添加了以下块到/etc/nginx/sites/example.com.conf

location ~ \.json {
    add_header  Content-Type    application/json;
    proxy_pass http://lospec;

    include /etc/nginx/config/proxy/proxy.conf;
}

看起来效果不错。但如果这样做有什么问题,请告诉我。

答案2

该文件可能mime.types未包含在 nginx 配置中,因此无法加载。

请检查一下主文件中是否有类似下面的行nginx.conf。路径nginx.conf可能是/etc/nginx/nginx.conf/etc/nginx/config/nginx.conf

include /etc/nginx/config/mime.types;

相关内容