使用uwsgi作为应用服务器部署Django项目,同时从指定目录提供静态文件服务(如下命令所示),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配置文件中添加此内容后,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
/static/
的请求,并直接从磁盘上提供collectstatic
收集的文件。文档中有更多信息https://docs.djangoproject.com/en/3.0/howto/static-files/deployment/ - Iain Shelvingtoncollectstatic
是使用uwsgi
提供的,nginx将请求传递给uwsgi
。因此,静态文件存在于Python容器中。 - rakesh kotian