使用Nginx部署Django时,管理页面的css报404或正常加载但是无效

css无效或者加载失败(404)都与conf配置有关
我在配置时,发现网页报200但是css无法显示。搜索了一宿,得如下解决方案:
首先要使得css正常加载需要:

	# 使css文件正常加载
	include       mime.types;
	default_type  application/octet-stream;

如果提示缺少mime:进入Nginx的目录找到:mime.types
在这里插入图片描述
将他复制到和conf同一个目录

然后重启Nginx,发现任然无法正常显示css样式
不要慌
再加上

		# 使css文件正常加载
		location ~.*(js|css|png|gif|jpg|mp3|ogg)$ {
			root /opt/myapi/;
		}

root后面跟就是你的项目文件目录,里面包含了static文件
然后保存运行,成功:
在这里插入图片描述
附上Nginx配置文件完整代码:

# luffy_nginx.conf

user  root;

events {
	worker_connections  1024;
}

http
{
	# 使css文件正常加载
	include       mime.types;
	default_type  application/octet-stream;


	# the upstream component nginx needs to connect to
	upstream django {
		# server unix:///path/to/your/mysite/mysite.sock; 
		# for a file socket
		server 127.0.0.1:9001; 
		# for a web port socket (we'll use this first)
	}

	# configuration of the server
	server {
		# the port your site will be served on
		listen      8888;
		# the domain name it will serve for
		server_name xx.xx.xx.xx; #改成你的公网ip
		
		
		# substitute your machine's IP address or FQDN
		charset     utf-8;

		# max upload size
		client_max_body_size 75M;   # adjust to taste

		# Django media
		location /media  {
			alias /opt/myapi/media;  #改成你的静态媒体地址
			# your Django project's media files - amend as required
		}

		location /static {
			alias /opt/myapi/static; #改成你的静态文件地址
			# your Django project's static files - amend as required
		}

		# Finally, send all non-media requests to the Django server.
		location / {
			# uwsgi_pass  django;  
			uwsgi_pass  127.0.0.1:9001;#uwigs上的socket也得设置为这个
			include     /opt/myapi/uwsgi_params; 
			# the uwsgi_params file you installed
		}
		# 使css文件正常加载
		location ~.*(js|css|png|gif|jpg|mp3|ogg)$ {
			root /opt/myapi/;
		}

	}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值