Nginx working outside Docker but not inside - django

I'm making a personal website using Django, Gunicorn, Nginx and Docker. When I execute:
gunicorn --chdir personal-website --bind :8000 personal_website.wsgi:application
The output is:
[arturocuya#localhost personalwebsite]$ gunicorn --chdir personal-website --bind :8000 personal_website.wsgi:application
[2018-09-09 11:49:02 -0500] [5161] [INFO] Starting gunicorn 19.6.0
[2018-09-09 11:49:02 -0500] [5161] [INFO] Listening at: http://0.0.0.0:8000 (5161)
[2018-09-09 11:49:02 -0500] [5161] [INFO] Using worker: sync
[2018-09-09 11:49:02 -0500] [5165] [INFO] Booting worker with pid: 5165
And it works (kinda, configuration for static files is yet to be done)
The problem is that when I run the Docker container with sudo docker-compose up, I get 502 Bad Gateway
I'm suspecting that the problem is how I use the ports but I don't really understand how it should be done.
This is my folder structure
.
├── config
│   └── nginx
│   └── conf.d
│   └── local.conf
├── docker-compose.yml
├── Dockerfile
└── personal-website
└── manage.py
Dockerfile
# Start from an official image
FROM python:3.6
# The following is an arbitrary location choice
RUN mkdir -p /opt/services/personalwebsite/src
WORKDIR /opt/services/personalwebsite/src
# Copy the project code
COPY . /opt/services/personalwebsite/src
# Install dependencies
RUN pip install django gunicorn Pillow
# Expose Port 8000
EXPOSE 8000
# Define the default command to run when starting the container
CMD ["gunicorn", "--chdir", "personal-website", "--bind", ":8000", "personal_website.wsgi:application"]
docker-compose.yml
version: '3'
services:
personalwebsite:
build: .
volumes:
- .:/opt/services/personalwebsite/src
networks:
- nginx_network
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
depends_on:
- personalwebsite
networks:
- nginx_network
networks:
nginx_network:
driver: bridge
config / nginx / conf.d / local.conf
# first we declare our upstream server, which is our Gunicorn application
upstream personalwebsite_server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "personalwebsite"
server personalwebsite:8000;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://personalwebsite;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
I also made sure to modify ALLOWED_HOSTS inside the Django project's settings.py
ALLOWED_HOSTS = ['127.0.0.1', '149.248.5.164', '0.0.0.0']
Edit 1:
As someone suggested in the comments, I accessed the Nginx container with sudo docker-compose exec nginx bash and then did curl personalwebsite:8000. I got a DISALLOWED HOST error so I added personalwebsite to the allowed hosts in settings.py then I tried to curl again and the output was the HTML of my page, which is fine.
That seemed to do the trick inside the container because the output was the HTML of my page. But then I did sudo docker-compose up and i got 502 Bad Gateway again. The exact output was:
[arturocuya#localhost personalwebsite]$ sudo docker-compose up
[sudo] password for arturocuya:
Starting personalwebsite_personalwebsite_1 ... done
Starting personalwebsite_nginx_1 ... done
Attaching to personalwebsite_personalwebsite_1, personalwebsite_nginx_1
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Starting gunicorn 19.9.0
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [1] [INFO] Using worker: sync
personalwebsite_1 | [2018-09-10 02:07:12 +0000] [10] [INFO] Booting worker with pid: 10
nginx_1 | 172.26.0.1 - - [10/Sep/2018:02:07:17 +0000] "GET / HTTP/1.1" 502 576 "-" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" "-"
nginx_1 | 2018/09/10 02:07:17 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://172.26.0.2:80/", host: "0.0.0.0:8000"
nginx_1 | 2018/09/10 02:07:18 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.26.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.26.0.2:80/favicon.ico", host: "0.0.0.0:8000", referrer: "http://0.0.0.0:8000/"
nginx_1 | 172.26.0.1 - - [10/Sep/2018:02:07:18 +0000] "GET /favicon.ico HTTP/1.1" 502 576 "http://0.0.0.0:8000/" "Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36" "-"

Your Nginx configuration should be
upstream personalwebsite {
server personalwebsite:8000;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://personalwebsite;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
Is your Nginx service is running inside the container?
Please hit the below command inside the Nginx container and let me know the output
curl -I localhost:80

It's very easy to understand, your proxy_pass URL and upstream keyword should be the same
For deep diving into Nginx upstream configuration go through the Link

Related

When I set DEBUG=False Django gives me 400 bad request am using [docker, nginx, django, gunicorn]

I am trying to define a production env for Django using docker and Nginx and Gunicorn and It works fine when debug=True If I make debug=False the Issue start here gives me Bad Request (400)
my Nginx file like this:
upstream API {
server backend-stack:8000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_set_header Host $host;
proxy_pass http://api;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
}
location /static/ {
alias /backend/static/;
}
location /media/ {
alias /backend/media/;
}
}
my allowed_hosts In settings.py
if env.get("ALLOWED_HOSTS"):
ALLOWED_HOSTS = env.get("ALLOWED_HOSTS")
my gunicorn execute command from entrypoint.sh file:
gunicorn core.wsgi:application --bind 0.0.0.0:8000
also here Is my nginx container In docker-compose:
nginx:
container_name: nginx-stack
build: ./nginx
restart: always
volumes:
- ./nginx/config:/etc/nginx/conf.d
- ./nginx/log:/var/log/nginx
- static_volume:/app_backend/static
- media_volume:/app_backend/media
ports:
- 80:80
- 443:443
depends_on:
- backend
networks:
- app-network
my allowed_hosts value:
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '0.0.0.0', '*']
finaly access log file for nginx :
192.168.144.1 - - [12/Jun/2021:15:18:02 +0000] "GET / HTTP/1.1" 400 154 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36" "-"
ok, I did It the problem was happening because I don't create any view yet or any page with a URL after I create a new page and set it as the home page It's working now.

Gunicorn/Nginx configuration for Django 2.2 gives 502 Bad gateway

I am getting bad gateway 502 error when I access the site. This is how my configuration looks like
/etc/nginx/sites-enabled/django
upstream app_server {
server unix:/home/django/gunicorn.socket fail_timeout=0;
}
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
client_max_body_size 4G;
server_name _;
keepalive_timeout 5;
# Your Django project's media files - amend as required
location /media {
alias /home/django/my_project/current/my_project/media;
}
# your Django project's static files - amend as required
location /static {
alias /home/django/my_project/current/my_project/static;
}
# Proxy the static assests for the Django Admin panel
location /static/admin {
alias /usr/local/lib/python3.6/dist-packages/django/contrib/admin/static/admin/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_buffering off;
proxy_pass http://app_server;
}
}
/etc/systemd/system/gunicorn.service
[Unit]
Description=Gunicorn daemon for Django
Before=nginx.service
After=network.target
[Service]
WorkingDirectory=/home/django/my_project/current
ExecStart=/usr/bin/gunicorn3 --name=my_project --pythonpath=/home/django/my_project/current --bind unix:/home/my_project/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py my_project.wsgi:application --log-file /var/log/gunicorn.log --log-level debug --workers=2
Restart=always
SyslogIdentifier=gunicorn
User=django
Group=django
[Install]
WantedBy=multi-user.target
When I run service gunicorn status, I can see this:
● gunicorn.service - Gunicorn daemon for Django
Loaded: loaded (/etc/systemd/system/gunicorn.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2020-07-19 19:28:37 UTC; 7min ago
Process: 9373 ExecStart=/usr/bin/gunicorn3 --name=my_project --pythonpath=/home/django/my_project/current --bind unix:/home/django/gunicorn.socket --config /etc/gunicorn.d/gunicorn.py my_project.wsgi:application --log-file /var/log/gunicorn.l
Main PID: 9373 (code=exited, status=1/FAILURE)
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Service hold-off time over, scheduling restart.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Scheduled restart job, restart counter is at 5.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: Stopped Gunicorn daemon for Django.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Start request repeated too quickly.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: gunicorn.service: Failed with result 'exit-code'.
Jul 19 19:28:37 admin-panel-s-2vcpu-4gb-fra1-01 systemd[1]: Failed to start Gunicorn daemon for Django.
And I can see in the nginx error log:
2020/07/19 19:26:00 [crit] 9039#9039: *15 connect() to unix:/home/django/gunicorn.socket failed (2: No such file or directory) while connecting to upstream, client: 84.48.191.11, server: _, request: "GET / HTTP/1.1", upstream: "http://unix:/home/django/gunicorn.socket:/", host: "161.35.197.193"
Any advice? I looked into similar questions, but I was not able to resolve my problem.
It seems like I had an error 502 because it couldn't open database. I have found more information about it in Gunicorn log.

django + nginx on docker bad request (400)

I'm trying to get django (on gunicorn) and nginx running with docker. Unfortunately, I keep getting a Bad Request (400) error after I run docker-compose up -d --build. Help.
I have tried changing directories, directory names, volumes, networks and exposed ports to no avail. I also tried adding and removing server_name in my nginx.conf file.
In settings.py I have:
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
This is my docker-compose.yml file:
version: '3.3'
services:
web:
build: ./app
command: gunicorn my_server.wsgi:application --bind 0.0.0.0:8000
volumes:
- ./app/:/usr/src/app/
expose:
- 8000
environment:
- DATABASE=${DATABASE}
- SECRET_KEY=${SECRET}
- SQL_DATABASE=${POSTGRES_USER}
- SQL_ENGINE=django.db.backends.postgresql
- SQL_HOST=${POSTGRES_HOST}
- SQL_PASSWORD=${POSTGRES_PASSWORD}
- SQL_PORT=${POSTGRES_PORT}
- SQL_USER=${POSTGRES_USER}
- SU_NAME=${SU_NAME}
- SU_EMAIL=${SU_EMAIL}
- SU_PASSWORD=${SU_PASSWORD}
depends_on:
- db
logging:
driver: "json-file"
options:
max-size: "100k"
max-file: "20"
db:
image: postgres:11.2-alpine
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_USER=${POSTGRES_USER}
logging:
driver: "json-file"
options:
max-size: "100k"
max-file: "20"
nginx:
build: ./nginx
restart: unless-stopped
volumes:
- static_volume:/usr/src/app/assets
ports:
- 80:80
depends_on:
- web
logging:
driver: "json-file"
options:
max-size: "100k"
max-file: "20"
volumes:
static_volume:
postgres_data:
external: true
and this is my nginx.conf file:
upstream my_server {
server web:8000;
}
server {
listen 80;
location / {
proxy_pass http://my_server;
proxy_set_header Host $http_host;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_redirect off;
}
location /assets/ {
alias /usr/src/app/assets;
}
location /robots.txt {
alias /var/www/html/robots.txt;
}
}
In my Dockerfile for django, I run makemigrations and migrate and I have confirmed that the database tables are created.
I expect to go to localhost or 127.0.0.1 and see my website served there. Instead, I get this error: Bad Request (400).
When I run docker ps I get:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2dbc6ff7be78 my_server_nginx "nginx -g 'daemon of…" 4 minutes ago Up 4 minutes 0.0.0.0:80->80/tcp my_server_nginx_1
a6173e017c93 my_server_web "/usr/src/app/entryp…" 4 minutes ago Up 4 minutes 8000/tcp my_server_web_1
918e7bdae298 postgres:11.2-alpine "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 5432/tcp my_server_db_1
When I run docker logs my_server_nginx_1 I get lines like this:
172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"
172.18.0.1 - - [08/May/2019:22:25:07 +0000] "GET /favicon.ico HTTP/1.1" 400 37 "http://localhost/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36" "-"
Probaly you need to add my_server to your allowed hosts(same as your upstream in NGINX config):
ALLOWED_HOSTS = ['my_server']
While trying to implement upinder kumar's solution I stumbled upon the solution. I will add here for anyone else facing this problem in the future:
The nginx.conf cannot have both
proxy_set_header Host $http_host;
and
proxy_set_header Host $host;
Deleting either one solved the problem.
Create socket file using gunicorn and configure in our virtual host.
#############Gunicorn configuration###################
[unit]
Description=gunicorn daemon
After=network.target
[Service]
User=root
Group=nginx
WorkingDirectory=/var/www/html/source
ExecStart=/var/www/html/env_source/bin/gunicorn --workers 3 --bind
unix:/var/www/html/source/source.sock application_name.wsgi:application
[Install]
WantedBy=multi-user.target
It will create file name source.sock just configure this sock file in nginx virtual host
###########nginx Configuratio
server {
server_name example.com;
listen 80;
access_log /var/www/html//source/access.log;
error_log /var/www/html/source/error.log;
include /etc/nginx/default.d/*.conf;
location /static/ {
root /var/www/html/source/run;
}
location / {
proxy_pass http://unix:/var/www/html/source/source.sock;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $host;
}
}
if you define allowed hosts in settings.py:
ALLOWED_HOSTS = ['localhost','127.0.0.1','my_domain.com']
if you define allowed hosts in .env as an environment
variable, you should use the following format (without brackets):
in .env file:
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 my_domain.com
and in settings.py
ALLOWED_HOSTS = os.getenv("DJANGO_ALLOWED_HOSTS", "127.0.0.1").split()

NGINGX gives 502 Bad Gateway when trying to use NGINX + Gunicorn on Docker

I'm developing a Django app with a PostgreSQL database and I'm using NGINX + Gunicorn with Docker.
PostgreSQL, NGINX and Gunicorn are on different containers communicating with networks. I can build my app with docker-compose build but when I execute it with docker-compose up and view my app in the browser all I get is a 502 Bad Gateway error and in the terminal all I see is this:
nginx_1 | 127.0.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.0" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "172.23.0.1"
nginx_1 | 172.23.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.1" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
My docker-compose looks like this:
version: '3'
services:
# Database container
db:
image: postgres:10
volumes:
- db_volume:/var/lib/postgresql/data
env_file:
- ./.env
networks:
- db_network
# Web app container with gunicorn
webapp:
build: .
env_file: ./.env
volumes:
- .:/opt/services/webapp/src
- static:/opt/services/webapp/static
- media:/opt/services/webapp/media
networks:
- db_network
- nginx_network
depends_on:
- db
# NGINX (Reverse proxy) container
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static:/opt/services/webapp/static
- media:/opt/services/webapp/media
networks:
- nginx_network
depends_on:
- webapp
networks:
db_network:
driver: bridge
nginx_network:
driver: bridge
volumes:
db_volume:
static:
media:
And this is my Dockerfile:
# Start with an official Python image
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/webapp/src
WORKDIR /opt/services/webapp/src
# Install dependencies
ADD requirements.txt /opt/services/webapp/src
RUN pip install -r requirements.txt
COPY . /opt/services/webapp/src
# Expose port 8000
EXPOSE 8000
# Default command to run when starting the container
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "myapp", "myapp.wsgi:application"]
This is my requirements.txt:
bcrypt==3.1.4
cffi==1.11.5
Django==2.0.4
Pillow==5.1.0
psycopg2==2.7.4
psycopg2-binary==2.7.4
pycparser==2.18
pytz==2018.4
six==1.11.0
django-phonenumber-field==2.0.0
gunicorn==19.8.1
gevent==1.3.1
And my NGINX configuration:
# Upstream server
upstream myapp_server {
server webapp:8000;
}
# Main server
server {
listen 80;
server_name localhost;
location /static/ {
alias /opt/services/webapp/static/;
}
location /media/ {
alias /opt/services/webapp/media/;
}
location / {
proxy_pass http://myapp_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1;
break;
}
}
}
I'm not sure about what could be causing this problem but it looks like gunicorn is not properly detecting my app, NGINX is working and PostgreSQL seems to be working too!
You need use proxy_pass to your upstream.
if (!-f $request_filename) {
proxy_pass http://myapp_server;
break;
}

docker nginx failed to make connection with web container

My docker nginx container failed to connect with gunicorn container. docker compose logs looks like,
dj | [2017-09-16 12:37:14 +0000] [22] [INFO] Starting gunicorn 19.7.1
dj | [2017-09-16 12:37:14 +0000] [22] [DEBUG] Arbiter booted
dj | [2017-09-16 12:37:14 +0000] [22] [INFO] Listening at: http://127.0.0.1:8000 (22)
dj | [2017-09-16 12:37:14 +0000] [22] [INFO] Using worker: sync
dj | [2017-09-16 12:37:14 +0000] [25] [INFO] Booting worker with pid: 25
dj | [2017-09-16 12:37:14 +0000] [22] [DEBUG] 1 workers
ng | 2017/09/16 12:37:22 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:8000/api/v1", host: "localhost"
ng | 172.20.0.1 - - [16/Sep/2017:12:37:22 +0000] "GET /api HTTP/1.1" 502 537 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36" "-"
ng | 2017/09/16 12:37:31 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /admin HTTP/1.1", upstream: "http://127.0.0.1:8000/api/admin", host: "localhost"
ng | 172.20.0.1 - - [16/Sep/2017:12:37:31 +0000] "GET /admin HTTP/1.1" 502 537 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/52.0.2743.116 Chrome/52.0.2743.116 Safari/537.36" "-"
If I make a curl request inside django container, it shows the relevant html text.
$ docker-compose exec api bash
root#e29e3756916c:/code# curl http://127.0.0.1:8000
root#e29e3756916c:/code# curl http://127.0.0.1:8000/api/v1
root#e29e3756916c:/code# curl -L http://127.0.0.1:8000/api/v1
{"_type":"document","_meta":{"url":"http://127.0.0.1:8000/api/v1/","title":"Backend APIs"},"auth":{"convert-token":{"create":{"_type":"link","url":"/api/v1/auth/convert-token/","action":"post","description":"Implements an endpoint to convert a provider token to an access token\n\nThe endpoint is used in the following flows:\n\n* Authorization code\n* Client credentials"}},"revoke-token":{"create":{"_type":"link","url":"/api/v1/auth/revoke-token/","action":"post","description":"Implements an endpoint to revoke access or refresh tokens"}},"sign_in":{"create":{"_type":"link","url":"/api/v1/auth/sign_in/","action":"post","encoding":"application/json","fields":[{"name":"username","required":true,"location":"form","schema":{"_type":"string","title":"Username","description":""}},{"name":"password","required":true,"location":"form","schema":{"_type":"string","title":"Password","description":""}}]}},"sign_up":{"create":{"_type":"link","url":"/api/v1/auth/sign_up/","action":"post","encoding":"application/json","fields":[{"name":"first_name","location":"form","schema":{"_type":"string","title":"First name","description":""}},{"name":"last_name","location":"form","schema":{"_type":"string","title":"Last name","description":""}},{"name":"email","location":"form","schema":{"_type":"string","title":"Email address","description":""}},{"name":"username","required":true,"location":"form","schema":{"_type":"string","title":"Username","description":"Required. 150 characters or fewer. Letters, digits and #/./+/-/_ only."}},{"name":"password","required":true,"location":"form","schema":{"_type":"string","title":"Password","description":""}},{"name":"is_active","location":"form","schema":{"_type":"boolean","title":"Active","description":"Designates whether this user should be treated as active. Unselect this instead of deleting accounts."}}]}},"token":{"create":{"_type":"link","url":"/api/v1/auth/token/","action":"post","description":"Implements an endpoint to provide access tokens\n\nThe endpoint is used in the following flows:\n\n* Authorization code\n* Password\n* Client credentials"}}}}root#e29e3756916c:/code#
root#e29e3756916c:/code#
nginx.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
proxy_force_ranges on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
location /admin {
proxy_pass http://127.0.0.1:8000/api/admin;
}
location /api {
proxy_pass http://127.0.0.1:8000/api/v1;
}
location /oauth {
proxy_pass http://127.0.0.1:8000/api/oauth;
}
location /static {
proxy_pass http://127.0.0.1:8000/static;
}
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
}
Dockerfile for nginx container is,
FROM nginx:latest
#RUN apt-get update
ADD ./static /usr/share/nginx/html
ADD nginx.conf /etc/nginx/conf.d/default.conf
ENV NGINX_PORT 80
EXPOSE 80
CMD /bin/bash -c "nginx -g 'daemon off;'"
docker-compose.yaml
version: '2'
services:
nginx:
build: ./nginx
container_name: ng
ports:
- "80:80"
#volumes:
# - ./src:/src
# - ./config/nginx:/etc/nginx/conf.d
depends_on:
- api
links:
- api
volumes_from:
- api
api:
build: ./s2s_api
container_name: dj
command: bash ./wait_for_db.sh
restart: always
depends_on:
- db
ports:
- "8000:8000"
tty: true
links:
- db:mysql
db:
image: mysql
container_name: db
command: mysqld --user=root --verbose
volumes:
- ./dbcreation.sql:/tmp/dbcreation.sql
- ./import.sh:/tmp/import.sh
ports:
- "3306:3306"
restart: always
environment:
MYSQL_DATABASE: "S2S"
MYSQL_USER: "root"
MYSQL_PASSWORD: "avi"
MYSQL_ROOT_PASSWORD: "avi"
MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
DB_PORT: 3306
DB_HOST: db
wait_for_db.sh
#!/bin/sh
# Wait for database to get available
M_LOOPS="10"
#wait for mysql
i=0
# http://stackoverflow.com/a/19956266/4848859
while ! curl db:3306 >/dev/null 2>&1 < /dev/null; do
i=`expr $i + 1`
if [ $i -ge $M_LOOPS ]; then
echo "$(date) - db:3306 still not reachable, giving up"
exit 1
fi
echo "$(date) - waiting for db:3306..."
sleep 3
done
echo "$(date) - db:3306 Reachable ! - Starting Daemon"
#start the daemon
#exec $START_CMD
python manage.py makemigrations
python manage.py migrate
gunicorn s2s_api.wsgi:application -b 127.0.0.1:8000 --log-level debug
You issue is obvious from the logs.
dj | [2017-09-16 12:37:14 +0000] [22] [INFO] Listening at: http://127.0.0.1:8000 (22)
ng | 2017/09/16 12:37:22 [error] 8#8: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: localhost, request: "GET /api HTTP/1.1", upstream: "http://127.0.0.1:8000/api/v1", host: "localhost"
So there are two issues and both of them at 127.0.0.1. In your dj container, you should be running gunicorn on 0.0.0.0:8000 because the request will be forwarded by the nginx container and it is external to django container
127.0.0.1 in each container points to the container loop back itself. Now when you proxy_pass in nginx to 127.0.0.1:8000, nginx is expecting something to be running in the same container at port 8000. So it doesn't work. You need to change it to name of the service you used in docker-compose. Assuming it was api, you should use
proxy_pass http://api:8000/api/oauth;
Also on a side notem you shouldn't proxy pass static files to Gunicorn, they should be handled in nginx container itself