在Elasticbeanstalk Docker环境中提供Django静态文件

使用docker环境将Django应用程序部署到EB时,静态文件将返回404.

我在docker容器上使用gunicorn,因为不推荐/不可能(?)通过gunicorn提供静态文件,我想配置EB nginx使用主机文件系统为它们服务.

Dockerfile

FROM python:3
....
....
# Expose listen ports
EXPOSE 8002

RUN chmod +x ./docker/container_start.sh

CMD ["sh", "./docker/container_start.sh"]

Dockerrun.aws.json

{
  "AWSEBDockerrunVersion": "1",
  "Ports": [
    {
      "ContainerPort": "8002"
    }
  ],
  "Volumes": [
    {
      "ContainerDirectory": "/app/assets",
      "HostDirectory": "/var/app/current/assets"
    }
  ]
}

我怎么能告诉EB nginx从/ var / app / current / assets中提供/ assets / *并将其余部分代理到docker容器?

由于docker环境,下面的东西不起作用.

option_settings:
  "aws:elasticbeanstalk:container:python:staticfiles":
    "/static/": "www/static/"

最佳答案 我有完全相同的问题!解决方案是在服务器上的/etc/sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf中自定义nginx配置.

基本上我将该文件的内容复制到.ebextension配置文件中,并添加了静态文件夹的location指令.

所以使用你的Dockerrun.aws.json

{
  "AWSEBDockerrunVersion": "1",
  "Ports": [
    {
      "ContainerPort": "8002"
    }
  ],
  "Volumes": [
    {
      "ContainerDirectory": "/app/assets",
      "HostDirectory": "/var/app/current/assets"
    }
  ]
}

您将创建.ebextension / 01_nginx.config

files:
  "/etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker-proxy.conf":
  mode: 000644
  owner: root
  group: root
  content: |
    map $http_upgrade $connection_upgrade {
        default        "upgrade";
        ""            "";
    }

    server {
        listen 80;

        gzip on;
        gzip_comp_level 4;
        gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
            set $year $1;
            set $month $2;
            set $day $3;
            set $hour $4;
        }
        access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;

        access_log    /var/log/nginx/access.log;

        location /assets/ {
          root /var/current;
        }

        location / {
            proxy_pass            http://docker;
            proxy_http_version    1.1;

            proxy_set_header    Connection          $connection_upgrade;
            proxy_set_header    Upgrade             $http_upgrade;
            proxy_set_header    Host                $host;
            proxy_set_header    X-Real-IP           $remote_addr;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        }
    }

这对我行得通!

点赞