我使用uwsgi作为服务器,使用nginx作为运行django项目的反向代理。
项目结构如下(这里我仅列出了所需的文件夹/文件):
war
├── katana
│ ├── wapps
│ │ ├── app1
│ │ └── app2
│ └── wui
│ ├── settings.py
│ └── wsgi.py
└── static
├── css
│ └── test.css
├── img
│ └── test.img
└── js
└── test.js
settings.py中的静态配置如下:
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
DATA_UPLOAD_MAX_MEMORY_SIZE = 10242880
STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), 'static')
wsgi.py如下:
import os
from django.core.wsgi import get_wsgi_application
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "katana.wui.settings")
application = get_wsgi_application()
uwsgi is server已启动,使用:uwsgi -b 65535 --socket :4000 --workers 100 --cpu-affinity 1 --module katana.wui.wsgi --py-autoreload 1
nginx conf如下:
events {
worker_connections 1024; ## Default: 1024
}
http {
include conf/mime.types;
# the upstream component nginx needs to connect to
upstream uwsgi {
server backend:4000; # for a web port socket (we'll use this first)
}
# configuration of the server
server {
# the port your site will be served on
listen 8443 ssl http2 default_server;
# the domain name it will serve for
server_name _; # substitute your machine's IP address or FQDN
charset utf-8;
ssl_certificate /secrets/server.crt;
ssl_certificate_key /secrets/server.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
add_header Strict-Transport-Security "max-age=31536000" always;
# Redirect HTTP to HTTPS
error_page 497 https://$http_host$request_uri;
# max upload size
client_max_body_size 75M; # adjust to taste
uwsgi_read_timeout 600s;
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass uwsgi;
include /config/uwsgi_params; # the uwsgi_params file you installed
}
}
}
项目部署成功,但是未加载静态内容(css,js,img)。浏览器控制台中的错误:
GET https://<ip>/static/css/test.css net::ERR_ABORTED 404
注意:我希望uwsgi服务器提供静态文件,而nginx仅充当反向代理。如果将nginx配置为提供静态文件,则可以执行此操作,但我希望使用uwsgi服务器可以实现此功能。
Django在生产环境中不提供静态文件,您应该为其添加附加的nginx位置
location /static {
alias /path/to/your/static/;
}
[我建议您不要执行以下操作,因为您已经在从nginx进行代理,没有uwsgi可以处理它们(由于需要将它们重新代理,因此您的负载会更多)
如果您仍然想走这条路uwsgistaticfiledocs