Nginx缓存uwsgi服务的静态文件。

问题描述 投票:1回答:1

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.comblogmaximizing-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

django docker nginx docker-compose uwsgi
1个回答
2
投票

你的配置的问题是位置块中的文件名列表有换行。我试过 nginx -t -c <filename> 用你的位置块的修改版本。

        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;
    }

...这就通过了测试!

© www.soinside.com 2019 - 2024. All rights reserved.