[Vuejs]-Deploying Django with Nginx, Gunicorn and Supervisor

0👍

But doesn’t exposing port 8000 defeat the purpose of using Nginx as a reverse proxy and Gunicorn’s unix socket?

In gunicorn, you have to expose 8000 port on localhost like this gunicorn --bind 127.0.0.1:8000 <djangoapp>.wsgi --daemon. Exposing it on 0.0.0.0 will obviously be a security vulnerability considering your nginx in on the same server.

Doesn’t exposing 8000 also increase the surface area for attack vectors? Or is it best practice to expose port 8000? I’m a bit confused why I would use both expose that port and use both Nginx and Gunicorn.

You don’t need to expose port 8000 you can expose any port but you need to tell gunicon to listen on at least a single port so that nginx can pass requests to it.

And regarding using both nginx and gunicorn, they both are really different and handle very different use case/functions of an application.

Nginx uses "event‑driven" approach to handle requests so a single worker of nginx can handle 1000s of req simultaneously. But Gunicorn on the other hand mostly(by default) uses sync worker which means a request will remain with a worker till it is processed. (posted this twice today :p)

So you need both if you remove nginx all your requests will return 50X except which are currently handled by gunicorn until the worker is free. And also gunicorn is not made to handle user traffic or in bigger application things like load balancing can only be done by nginx. So nginx has it’s own purpose in an application.

0👍

After neeraj9194 pointed out the 400, I did more searching for issues relating to Nginx, Gunicorn 400 and Django and I came across a ton of similar issues. Looks like it’s mainly an Nginx issue. The answer in this blog fixed my issue.

I replaced the location block in my API Nginx config with:

location / {
      proxy_set_header Host $host;
      proxy_pass http://backend_server;
      proxy_set_header X-Forwarded-Host $server_name;
      proxy_set_header X-Real-IP $remote_addr;
  }

Leave a comment