部署到docker,无法加载样式表和javascript

时间:2018-04-18 07:48:45

标签: docker nginx flask

这是我的docker-compose.yml

version: '2'

services:
  web:
    restart: always
    build: ./web
    expose:
      - "8000"
    volumes:
      - /usr/src/app/web/project/static
    command: /usr/local/bin/gunicorn -w 2 -b :8000 project:app
    depends_on:
      - postgres

  nginx:
    restart: always
    build: ./nginx
    ports:
      - "80:80"
    volumes:
      - /www/static
    volumes_from:
      - web
    depends_on:
      - web

  data:
    image: postgres:9.6
    volumes:
      - /var/lib/postgresql
    command: "true"

  postgres:
    restart: always
    build: ./postgresql
    volumes_from:
      - data
    expose:
      - "5432"

当我部署到docker容器时,该网站没有样式css,js正确加载,有人请帮忙吗?

已更新

# Define the parameters for a specific virtual host/server
server {
    # Define the directory where the contents being requested are stored
    # root /usr/src/app/project/;

    # Define the default page that will be served If no page was requested
    # (ie. if www.ezloan.amkcambodia.com is requested)
    # index index.html;

    # Define the server name, IP address, and/or port of the server
    listen 80;
    # server_name xxx.yyy.zzz.aaa

    # Define the specified charset to the “Content-Type” response header field
    charset utf-8;

    # Configure NGINX to deliver static content from the specified folder
    location /static {
        alias /usr/src/app/web/project/static;
    }

    # Configure NGINX to reverse proxy HTTP requests to the upstream server (Gunicorn (WSGI server))
    location / {
        # Define the location of the proxy server to send the request to
        proxy_pass http://web:8000;

        # Redefine the header fields that NGINX sends to the upstream server
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # Define the maximum file size on file uploads
        client_max_body_size 5M;
        client_body_buffer_size 5M;
    }
}

1 个答案:

答案 0 :(得分:2)

尝试将volumes上的web指令更改为

volumes:
   - /usr/src/app/web/project/static:<path of app on the container>

关于Nginx

静态文件存储在哪里?在web容器或nginx? 如果它们存储在nginx中,您需要将alias指向nginx容器上的位置,在那里挂载nginx的卷,如

volumes:
    - /usr/src/app/web/project/static:/www/static

并在nginx conf中使用

location /static {
    alias /www/static;
}

如果存储在web容器中的静态需要将nginx重定向到网络,则不需要location /static