Nikita Tonkoskur
Nikita Tonkoskur

Reputation: 1499

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?

Upvotes: 0

Views: 981

Answers (3)

The Fool
The Fool

Reputation: 20477

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.

Upvotes: 0

bellackn
bellackn

Reputation: 2184

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;

Upvotes: 1

Prasanth
Prasanth

Reputation: 537

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.

Upvotes: 0

Related Questions