Django项目部署Nginx报错:未知指令/etc/nginx/nginx.conf
Django部署Nginx配置问题导致CSS无法加载
问题概述
我有一个Django项目已上传至服务器,但Nginx配置出错,导致CSS无法正常显示。
错误信息
nginx: [emerg] unknown directive "/etc/nginx/nginx.conf" in /etc/nginx/nginx.conf:7 nginx: configuration file /etc/nginx/nginx.conf test failed
当前Nginx配置文件内容(/etc/nginx/nginx.conf)
-# For more information on configuration, see: -# * Official English Documentation: http://nginx.org/en/docs/ -# * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; -# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; -# Load modular configuration files from the /etc/nginx/conf.d directory. -# See http://nginx.org/en/docs/ngx_core_module.html#include -# for more information. include /etc/nginx/conf.d/*.conf; server { listen 81; listen [::]:81; server_name __; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; error_page 404 /404.html; location = /404.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } location /static/ { root /home/username/public_html/; expires 1d; } } -# Settings for a TLS enabled server. -# -# server { -# listen 443 ssl http2; -# listen [::]:443 ssl http2; -# server_name _; -# root /usr/share/nginx/html; -# -# ssl_certificate "/etc/pki/nginx/server.crt"; -# ssl_certificate_key "/etc/pki/nginx/private/server.key"; -# ssl_session_cache shared:SSL:1m; -# ssl_session_timeout 10m; -# ssl_ciphers HIGH:!aNULL:!MD5; -# ssl_prefer_server_ciphers on; -# -# # Load configuration files for the default server block. -# include /etc/nginx/default.d/*.conf; -# -# error_page 404 /404.html; -# location = /40x.html { -# } -# -# error_page 500 502 503 504 /50x.html; -# location = /50x.html { -# } -# } }
Nginx服务日志
启动日志(sudo journalctl -u nginx -n 30)
Oct 15 16:20:32 test.diyetzone.com systemd[1]: Starting The nginx HTTP and reverse proxy server... Oct 15 16:20:32 test.diyetzone.com nginx[1569]: nginx: [emerg] unknown directive "/etc/nginx/nginx.conf" in /etc/nginx/nginx.conf:7 Oct 15 16:20:32 test.diyetzone.com nginx[1569]: nginx: configuration file /etc/nginx/nginx.conf test failed Oct 15 16:20:32 test.diyetzone.com systemd[1]: nginx.service: control process exited, code=exited status=1 Oct 15 16:20:32 test.diyetzone.com systemd[1]: Failed to start The nginx HTTP and reverse proxy server. Oct 15 16:20:32 test.diyetzone.com systemd[1]: Unit nginx.service entered failed state. Oct 15 16:20:32 test.diyetzone.com systemd[1]: nginx.service failed.
停止后重启日志
Oct 15 16:38:27 test.diyetzone.com systemd[1]: Starting The nginx HTTP and reverse proxy server... Oct 15 16:38:27 test.diyetzone.com nginx[3033]: nginx: [emerg] unknown directive "/etc/nginx/nginx.conf" in /etc/nginx/nginx.conf:7 Oct 15 16:38:27 test.diyetzone.com nginx[3033]: nginx: configuration file /etc/nginx/nginx.conf test failed Oct 15 16:38:27 test.diyetzone.com systemd[1]: nginx.service: control process exited, code=exited status=1 Oct 15 16:38:27 test.diyetzone.com systemd[1]: Failed to start The nginx HTTP and reverse proxy server. Oct 15 16:38:27 test.diyetzone.com systemd[1]: Unit nginx.service entered failed state. Oct 15 16:38:27 test.diyetzone.com systemd[1]: nginx.service failed.
解决方案
修复配置语法错误
报错指向第7行,原因是所有注释行开头多了-符号(如-# Load dynamic modules...),Nginx无法识别该格式。将所有注释行的-去掉,确保注释以#开头。修正静态文件配置及权限
- 检查路径:确认
/home/username/public_html/static/是Django收集后的静态文件目录,路径不符则修改root值。 - 权限设置:执行以下命令让Nginx能读取静态文件:
sudo chmod -R 755 /home/username/public_html/static/ sudo chown -R nginx:nginx /home/username/public_html/static/
- 检查路径:确认
验证配置并重启服务
- 测试配置有效性:
sudo nginx -t - 测试通过后重启Nginx:
sudo systemctl restart nginx
- 测试配置有效性:
内容的提问来源于stack exchange,提问作者Burak Demirer
相关产品推荐
相关产品推荐

