I'd like to find out what is haunting my dockerized Django, Postgres, Gunicorn, Nginx project.
`/etc/nginx/html/index.html" is not found`
nginx | 2021/10/27 15:40:34 [error] 22#22: *2 "/etc/nginx/html/index.html" is not found (2: No such file or directory), client: 172.29.0.1, server: , request: "GET / HTTP/1.1", host: "0.0.0.0:8000"
nginx | 172.29.0.1 - - [27/Oct/2021:15:40:34 0000] "GET / HTTP/1.1" 404 555 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36" "-
I cannot understand why it complains about the index.html when I essentially only redirect traffic from Django on port 8000 and have replaced the config file for nginx.
In nginx.conf
I have tried to change listen 80;
to listen 8000;
and tried to change and then to delete the entry server_name localhost;
In docker-compose I have tried to change ports, such as
ports:
- 8000:80
-->
ports:
- 8000:8000
I have inspected the container and default /etc/nginx/conf.d/default.conf
is deleted and replaced by the new nginx.conf
It's probably something very simple, but I cannot see it.
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ea9da69663eb nn_nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:8000->80/tcp, :::8000->80/tcp nginx
fcc396bd0e56 nn_django_app "/usr/src/app/entryp…" 7 minutes ago Up 4 minutes 8000/tcp django_app
2653621d29ff postgres:14.0-alpine "docker-entrypoint.s…" 7 minutes ago Up 4 minutes 5432/tcp db1
or with changed ports
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e15109bacc66 nn_nginx "/docker-entrypoint.…" 28 minutes ago Up 28 minutes 80/tcp, 0.0.0.0:8000->8000/tcp, :::8000->8000/tcp nginx
7ee41d873e3d nn_django_app "/usr/src/app/entryp…" 29 minutes ago Up 28 minutes 8000/tcp django_app
e9f14bd6e6b8 postgres:14.0-alpine "docker-entrypoint.s…" 29 minutes ago Up 28 minutes 5432/tcp
docker-compose
version: "3.8"
services:
# Database containers
db1:
container_name: db1
image: postgres:14.0-alpine
volumes:
- db1_volume:/var/lib/postgresql/data/
env_file:
- ./postgres/.env
networks:
- db1_network
# Web container
django_app:
container_name: django_app
build:
context: ./backend
dockerfile: Dockerfile
volumes:
- django_static_volume:/usr/src/app/static
env_file:
- ./backend/.env
networks:
- db1_network
- nginx_network
expose:
- 8000
depends_on:
- db1
command: gunicorn server.wsgi:application -w 3 -b :8000
# Reverse proxy container (nginx)
nginx:
container_name: nginx
restart: always
build: ./nginx
volumes:
- django_static_volume:/usr/src/app/django_files/static
networks:
- nginx_network
ports:
- 8000:80
depends_on:
- django_app
networks:
db1_network:
driver: bridge
nginx_network:
driver: bridge
volumes:
db1_volume:
django_static_volume:
Nginx DOCKERFILE
FROM nginx:1.21.3-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
WORKDIR /usr/src/app
nginx.conf
upstream django_backend {
server django_app:8000;
}
server {
listen 80;
server_name localhost;
location /admin {
proxy_pass http://django_backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /api {
proxy_pass http://django_backend;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/admin/ {
alias /usr/src/app/django_files/static/admin/;
}
location /static/rest_framework/ {
alias /usr/src/app/django_files/static/rest_framework/;
}
}
selected from django settings
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',
# Third-party
'corsheaders',
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
SECRET_KEY = 'secretsecretsecret'
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', '0.0.0.0', 'localhost']
ROOT_URLCONF = 'server.urls'
WSGI_APPLICATION = 'server.wsgi.application'
CORS_ORIGIN_ALLOW_ALL = True
STATIC_URL = '/static/'
STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]
STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
STATICFILES_FINDERS = [
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder",
]
CodePudding user response:
There are some issues in your code.
For the port 8000:80
in your docker-compose
the 8000
is the port that the browser will try to connect, 80
is the port that the docker container port. That means that you need to set the same port 80
for the nginx config
.
In the Nginx config, you only set the location for /api
and /admin
and staticfiles
. I only see you try to GET /
Did you try to GET
your api
and admin
instead ?