使用nginx和nextjs获取mime类型错误

xn1cxnb4  于 2023-03-29  发布在  Nginx
关注(0)|答案(1)|浏览(219)

下面是nginx的配置文件:

# Cache zone
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=7d use_temp_path=off;

upstream nextjs_sales {
  server nextjs_sales:3000;
}

upstream nextjs_rents {
  server nextjs_rents:3000;
}

server {
  listen 80 default_server;

  server_name _;

  types {
    application/javascript js;
    text/css css;
    application/json json;
    image/svg+xml svg;
  }

  server_tokens off;

  gzip on;
  gzip_proxied any;
  gzip_comp_level 4;
  gzip_types text/css application/javascript image/svg+xml;

  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;

  # SALES APP
  location /comprar {
    proxy_pass http://nextjs_sales;
  }

  # SALES APP BUILT ASSETS (E.G. JS BUNDLES)
  location /comprar/_next/static {
    proxy_cache STATIC;
    proxy_pass http://nextjs_sales;
  }

  # SALES APP STATIC ASSETS (E.G. IMAGES)
  location /comprar/static {
    proxy_cache STATIC;
    proxy_ignore_headers Cache-Control;
    proxy_cache_valid 60m;
    proxy_pass http://nextjs_sales;
  }

  # RENTS APP
  location /alquilar {
    proxy_pass http://nextjs_rents;
  }

  # RENTS APP BUILT ASSETS (E.G. JS BUNDLES)
  location /alquilar/_next/static {
    proxy_cache STATIC;
    proxy_pass http://nextjs_rents;
  }

  # RENTS APP STATIC ASSETS (E.G. IMAGES)
  location /alquilar/static {
    proxy_cache STATIC;
    proxy_ignore_headers Cache-Control;
    proxy_cache_valid 60m;
    proxy_pass http://nextjs_rents;
  }

  location / {
    stub_status on;
    access_log   off;
  }
}

但是即使我有types属性,我也得到了:

有什么建议或帮助来解决这个问题吗?

mec1mxoz

mec1mxoz1#

上游服务器(http://nextjs_*)可能在其响应中设置了不正确的内容类型标头,这可以通过proxy_hide_header来缓解。可以根据代理或上游服务器上的URL使用curl -I来识别多个内容类型响应标头:

$ curl -I localhost/.../file.js          # Proxy
$ curl -I nextjs_sales:3000/.../file.js  # Upstream

相关问题