Docker撰写,NGINX不提供静态文件

时间:2019-02-09 02:17:38

标签: docker nginx flask docker-compose

我正在尝试使用FlaskNGINX提供一个简单的Docker Compose应用。但是NGINX不会提供我的静态文件。

我正在使用Ubuntu 18.04框。这是我的文件结构:

    .
    ├── docker-compose.yml
    ├── nginx
    │   └── sites-enabled
    │       └── nginx.conf
    ├── README.md
    ├── uwsgi.ini
    └── web
        ├── app
        │   ├── __init__.py
        │   ├── static
        │   │   ├── pics
        │   │   │   ├── headshot1.jpg
        │   │   │   └── headshot2.jpg
        │   │   ├── scripts.js
        │   │   └── styles.css
        │   └── templates
        │       ├── index.html
        │       └── layout.html
        └── run.py

这是我的nginx.conf

    http {
      server {
        listen 80;
        server_name example.com www.example.com;

        location /static {
          alias /usr/src/app/app/static;
        }

        location / {
          include uwsgi_params;
          uwsgi_pass web:5000;
          proxy_set_header Host $host;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
      }
    }

还有我的docker-compose.yml

version: "2"

services:
  web:
    restart: always
    build: ./web
    expose:
      - "5000"
    volumes:
      - ./web:/usr/src/app
    command: >
      bash -c "uwsgi --socket :5000 \
                     --chdir /usr/src/app \
                     --wsgi-file run.py \
                     --callable app \
                     --master \
                     --processes 4 \
                     --threads 2"

  nginx:
    restart: always
    image: nginx:1.15
    ports:
      - "80:80"
    volumes:
      - /www/static
      - ./nginx/sites-enabled/nginx.conf:/etc/nginx/nginx.conf
    volumes_from:
      - web
    links:
      - web:web

运行容器时,我可以从Internet访问我的站点,但是没有提供任何静态文件(CSS,图像)。当我进入NGINX容器时,可以找到静态文件:

$ docker exec -it my_container_1_39dbd8b7e8dc ls -l /usr/src/app/app/static/pics
drwxr-xr-x 2 root root 4096 Feb  8 18:39 pics
-rw-r--r-- 1 root root    0 Feb  8 18:39 scripts.js
-rw-r--r-- 1 root root 3793 Feb  8 18:39 styles.css

我不确定我在做什么错。也许是文件权限问题?

编辑:将引用添加到static/目录:

HTML

<link rel="stylesheet" href="/static/styles.css"/>

CSS

#right {
  float: right;
  background:
    linear-gradient(to bottom, rgba(0,55,164, 0.75), rgba(0, 55, 164, 0.9)),
    url(pics/headshot1.jpg) no-repeat center center;
}

0 个答案:

没有答案