Docker和Nginx中的Django和React开发,错误的MIME类型错误

时间:2019-03-19 03:29:49

标签: django reactjs docker nginx

我正在使用docker开发Django react app,但是我的管理页面出现以下错误。我正在使用nginx代理我的路由以响应客户端和Django后端以及对后端的管理。但是我的后端显示了这种错误,不适用于静态CSS文件。

Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:3050/static/admin/css/base.css". ... 有谁知道如何解决这一问题?我已经尝试过此link或将include /etc/nginx/mime.types;添加到我的nginx conf中,但是它显示404错误。以下是我的nginx conf。我什至尝试删除我的浏览器缓存,但也无法正常工作。请帮助

upstream client {
  server client:3000;
}

upstream api {
  server api:8000;
}

server {
   listen 80;
   include  /etc/nginx/mime.types; ######Try added this but it shows 404 instead of the errors
location / {
   proxy_pass http://client;
 }

location /sockjs-node {
   proxy_pass http://client;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "Upgrade";
}

location ~ ^/(static/|js|css) {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
proxy_pass http://client;
}
location ~ ^/(static/|pagedown|pagedown-extra|rest_framework|admin) {
   autoindex on;
   autoindex_exact_size off;
   autoindex_localtime on;
   proxy_pass http://api;
}

location ~ ^/api {
   # rewrite /api/(.*) /$1 break; # this is for chop off the /api/ urlpath
proxy_pass http://api;
 }

location ~ ^/admin {
# rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}
}

<!DOCTYPE html>
<html lang="ja-jp" >
<head>
<title>サイト管理 | Django サイト管理</title>
<link rel="stylesheet" type="text/css" 
 href="/static/admin/css/base.css">
<link rel="stylesheet" type="text/css" 
href="/static/admin/css/dashboard.css">
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" 
href="/static/admin/css/responsive.css">
<meta name="robots" content="NONE,NOARCHIVE">
</head>
<body class=" dashboard"
  data-admin-utc-offset="32400">

<!-- Container -->
<div id="container">


<!-- Header -->
<div id="header">
    <div id="branding">

 <h1 id="site-name"><a href="/admin/">Django 管理サイト</a></h1>

    </div>

1 个答案:

答案 0 :(得分:0)

更新 似乎我的nginx错误,它尝试为两个相同的/ static / url路径名称提供服务,因此前缀始终会评估为客户端的路径。我将Django settings.py文件的STATIC_URL更改为“ / staticfiles /”,还将nginx配置文件更改为。现在工作正常。下面是最新的配置文件,希望对您有所帮助。

/etc/nginx/conf.d/default.conf

upstream client {
  server client:3000;
}

upstream api {
  server api:8000;
}

server {
   listen 80;
   include  /etc/nginx/mime.types;
location / {
   proxy_pass http://client;
 }

location /sockjs-node {
   proxy_pass http://client;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection "Upgrade";
}

location ~ ^/(static/|js|css) {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
proxy_pass http://client;
}
location ~ ^/(staticfiles/|pagedown|pagedown-extra|rest_framework|admin) {
   autoindex on;
   autoindex_exact_size off;
   autoindex_localtime on;
   proxy_pass http://api;
}

location ~ ^/api {
   # rewrite /api/(.*) /$1 break; # this is for chop off the /api/ urlpath
proxy_pass http://api;
 }

location ~ ^/admin {
# rewrite /api/(.*) /$1 break;
proxy_pass http://api;
}

}

Django settings.py

STATIC_URL = '/staticfiles/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIR = [
   os.path.join(BASE_DIR, 'static')
]