简体   繁体   中英

nginx serves files only on port 80

I have dockerizd django app with gunicorn and nginx. The app itself works at http://127.0.0.1:8000 but without static/media files, error:

172.24.0.1 - - [08/May/2019:13:25:50 +0000] "GET /static/js/master.js HTTP/1.1" 404 77 "http://127.0.0.1:8000/"

If I try to access files on port 80, they are served just fine.

Dockerfile:

FROM python:3.6-alpine

RUN apk --update add \
    build-base \
    postgresql \
    postgresql-dev \
    libpq \
    # pillow dependencies
    jpeg-dev \
    zlib-dev

RUN mkdir /www
WORKDIR /www
COPY requirements.txt /www/
RUN pip install -r requirements.txt

ENV PYTHONUNBUFFERED 1

COPY . /www/

docker-compose.yml

version: "3"
services:
  web:
    build: .
    restart: on-failure
    volumes:
      - .:/www
    env_file:
      - ./.env
    command: >
      sh -c "python manage.py collectstatic --noinput &&
      gunicorn --bind 0.0.0.0:8000 portfolio.wsgi:application --access-logfile '-'"
    expose:
      - "8000"
    ports:
      - "8000:8000"
  nginx:
    image: "nginx"
    restart: always
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./static:/var/www/portfolio/static
      - ./media:/var/www/portfolio/media
    links:
      - web
    ports:
      - "80:80"

nginx.conf

server {
  listen 80;
  server_name 127.0.0.1;

  # serve static files
  location /static/ {
    root /var/www/portfolio;
  }

  # serve media files
  location /media/ {
    root /var/www/portfolio;
  }

  # pass requests for dynamic content to gunicorn
  location / {
    pproxy_pass http://web:8000;
    proxy_set_header Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }

What I want is static and media files to load with my web app on 127.0.0.1. It seems to me that there might be a problem with proxy_pass , but I can't figure it out.

Any ideas?

This seems to be the culprit: proxy_pass http://127.0.0.1:8000;

This line makes Nginx look for a service on port 8000 inside the Nginx container . localhost / 127.0.0.1 inside a container always means "the container itself" and not the Docker host.

You are running both services in the same Docker network, so this should work for you:

proxy_pass http://web:8000;

I see you are running two containers and nginx could not connect to python container as the ip address you gave is bound to inside the container. you might need to add extra_hosts: in docker-compose to nginx part at which it will be able to connect to other container.

If you set nice logging in nginx you will realize that it is not running on 127.0.0.1 since its a compose service. So you need to check out on which IP your compose network runs and that is where you find nginx.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM