【发布时间】:2020-08-06 04:25:24
【问题描述】:
django 项目使用 uwsgi 作为应用服务器部署,它还提供来自指定目录的静态文件(如下命令所示),并且使用 nginx 作为反向代理服务器。这是使用 docker 部署的。
运行服务器的uwsgi命令如下:
uwsgi -b 65535 --socket :4000 --workers 100 --cpu-affinity 1 --module wui.wsgi --py-autoreload 1 --static-map /static=/project/static;
此时应用程序运行良好。我想将静态文件缓存到 nginx 服务器中。所以我参考了博客https://www.nginx.com/blog/maximizing-python-performance-with-nginx-parti-web-serving-and-caching,并在我的 nginx.conf 中包含了以下配置:
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
将此添加到我的 Nginx conf 后,Nginx 服务器容器退出并出现以下错误:
[emerg] 1#1: invalid number of arguments in "location" directive in /etc/nginx/nginx.conf:43
这就是uwsgi服务的静态文件可以缓存到nginx的方式吗?如果是,请告诉我这里出了什么问题。
我完整的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
}
location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg
|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid
|midi|wav|bmp|rtf)$ {
expires max;
log_not_found off;
access_log off;
}
}
}
Nginx 版本:1.16
【问题讨论】:
-
您通常会从 nginx 配置中的路径提供静态文件,而不是基于扩展名。对于标准的 Django 设置,您可以向
/static/提出任何请求,并直接从collectstatic收集文件的磁盘提供文件。文档有更多信息docs.djangoproject.com/en/3.0/howto/static-files/deployment -
是的,可以做到,但由于我使用 docker 容器,即 nginx 和 python(django) )在 python 容器中执行的
collectstatic使用uwsgi提供服务,nginx 会将请求传递给uwsgi。所以静态文件存在于 python 容器中。 -
即使你在 docker 容器中,最好将这些静态数据通过一个卷暴露给 nginx,以便整个静态目录直接由 nginx 提供服务
标签: django docker nginx docker-compose uwsgi