【发布时间】:2018-12-27 21:56:55
【问题描述】:
我在 angular src 文件夹中有一个 assets/image 文件夹,我将所有图像都留在那里,我有许多组件和子组件正在使用这些图像,例如 <img src"../../../assets/image/test.png">,
我构建了我的 Angular 应用程序并将所有文件放到静态文件夹中,在 nginx 中我指向它从静态文件夹中加载,在模板中的 django 中我使用 index.html 来加载静态文件,如下所示: 现在我的应用程序将运行,但在角度加载中没有地址像“../../../file”的文件都得到 404:
http://hello.com/bird.65c8b9bce67b6a965a9c.png (error 404)
如果我在地址前面放一个静态关键字,例如http://hello.com/static/bird.65c8b9bce67b6a965a9c.png
图像将加载..
我该如何处理这个问题?
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title> site </title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="{% static 'favicon.png' %}">
<link rel="stylesheet" href="{% static 'styles.921a613cd46f44e0d5a0.css' %}"></head>
<body>
<app-root>Loading . . .</app-root>
<script type="text/javascript" src="{% static 'runtime.6afe30102d8fe7337431.js' %}"></script>
<script type="text/javascript" src="{% static 'polyfills.b0205464c9bd4e7fe3b3.js' %}"></script>
<script type="text/javascript" src="{% static 'scripts.59ed76cc23ba77b0ec72.js' %}"></script>
<script type="text/javascript" src="{% static 'main.08947dd689f13d4027ea.js' %}"></script>
</body>
</html>
我也添加了
urlpatterns += static(base.STATIC_URL, document_root=base.STATIC_ROOT) + \
static(base.MEDIA_URL, document_root=base.MEDIA_ROOT)
到我的 urls.py 结尾,我运行 python manage.py collectstatic
nginx 服务器:
server {
listen 80;
server_name hello.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/vertical/academy;
}
location / {
include proxy_params;
#proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://unix:/home/vertical/academy/academy.sock;
}
}
【问题讨论】:
标签: django angular nginx django-staticfiles