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/;
}
}
}