Search code examples
nginxflaskuwsgi

Nginx not routing browser request to wsgi(python server running)


I am running my flask project from uwsgi on nginx. But my nginx is not routing the request to uwsgi when i hit localhost:80/

My nginx.conf looks like this

server {
    listen 80;
    server_name <your machine ip/domain>;(if on local it would be localhost but I was running on WSL so I put it IP)

    location / {
        include         uwsgi_params;
        uwsgi_pass      web_app:5000; (you might see suggestion of .sock files or suffixing http:// or unix: but none work for me plain simple your python server's service name which you would provide in docker-compose)
    }
}

docker-compose looks like this

version: '3.7'

services:
  web_app:
    build: .
    container_name: kpi-dashboard
    ports:
      - 5000:5000
    depends_on:
      - db
  nginx:
    build: ./nginx
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    depends_on:
      - web_app
  db:
    image: postgres:13-alpine
    container_name: postgresql
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=postgres
    ports:
      - 5432:5432
volumes:
  postgres_data:

nginx dockerfile

FROM nginx

RUN rm /etc/nginx/conf.d/default.conf (it is important to remove the default conf as it would not take your custom conf no matter where you copy it)
COPY nginx.conf /etc/nginx/conf.d/
(there are answers online to copy it no other places but this only works)

EXPOSE 80


web app dockerfile

FROM python:3.8.16-slim-buster

RUN apt-get update
RUN apt-get install gcc -y && apt-get install python3-dev -y && apt-get install libpq-dev -y

ENV PYTHONPATH=${PYTHONPATH}:${PWD}
RUN pip install poetry

WORKDIR /app
COPY pyproject.toml /app/
COPY . /app/

RUN poetry config virtualenvs.create false
RUN poetry install --no-dev

EXPOSE 5000

CMD ["uwsgi", "--ini", "wsgi.ini"]

wsgi.ini file

[uwsgi]
module = app (this is when you are writing you project entrypoint in app.py. if you are writing in wsgi.py then this would become wsgi:app)
socket = 0.0.0.0:5000
callable = app (this is important as wsgi by default considers your app instance as application either handle it in your main file or just add this configuration)
processes = 1
threads = 1
master = true
vacuum = true
die-on-term = true

This is what the nginx container output looks like

enter image description here

Editing question as the 404 issue was solved. But nginx is still not routing to wsgi.

The solution changed the location of copying the nginx.conf file in nginx dockerfile

COPY nginx.conf /etc/nginx/nginx.config

Editing question again as nginx routing to wsgi issue also resolved. The solution updated files as mentioned above


Solution

  • Yes so this worked for me. There are n number of configurations available online and almost all are same yet a slight difference causes the issue. I am updating my question to change files with the content that worked. Hope it helps someone.