Gunicorn helps your Django app talk to the web by running it safely and efficiently. It acts like a waiter taking web requests and giving back responses.
Gunicorn as WSGI server in Django
gunicorn myproject.wsgi:application
myproject with your Django project name.gunicorn myproject.wsgi:application
gunicorn --workers 3 myproject.wsgi:applicationgunicorn --bind 0.0.0.0:8000 myproject.wsgi:application
This command starts Gunicorn with 2 workers, listening only on your computer's local address at port 8000. You can open http://127.0.0.1:8000 in your browser to see your Django app.
# Run this command in your terminal inside your Django project folder gunicorn --workers 2 --bind 127.0.0.1:8000 myproject.wsgi:application
Gunicorn works only with WSGI-compatible apps like Django.
Use multiple workers to improve handling many users but don't use too many to avoid slowing your server.
For public access, combine Gunicorn with a web server like Nginx for security and speed.
Gunicorn runs your Django app by acting as a bridge between the web and your code.
You start Gunicorn with your Django project's WSGI application to serve requests.
Adjust workers and binding options to control performance and accessibility.
Practice
Solution
Step 1: Understand Gunicorn's purpose
Gunicorn is a WSGI server that connects web requests to the Django app code.Step 2: Identify what Gunicorn does not do
Gunicorn does not handle database queries, static files, or template compilation directly.Final Answer:
It acts as a bridge between the web server and the Django application. -> Option BQuick Check:
Gunicorn = bridge server [OK]
- Thinking Gunicorn manages static files
- Confusing Gunicorn with Django ORM
- Assuming Gunicorn compiles templates
myproject with the default WSGI application?Solution
Step 1: Identify the WSGI application path
In Django, the WSGI app is located atprojectname.wsgi:application.Step 2: Match the correct command format
The correct Gunicorn command usesgunicorn myproject.wsgi:application.Final Answer:
gunicorn myproject.wsgi:application -> Option DQuick Check:
WSGI app path = myproject.wsgi:application [OK]
- Using manage or settings instead of wsgi
- Confusing URLs module with WSGI
- Omitting the ':application' part
gunicorn --workers 3 --bind 0.0.0.0:8000 myproject.wsgi:application, what will happen when you run it?Solution
Step 1: Analyze the --workers option
The command specifies 3 workers, so Gunicorn will start 3 worker processes.Step 2: Analyze the --bind option
Binding to 0.0.0.0:8000 means listening on all network interfaces at port 8000.Final Answer:
Gunicorn will start 3 worker processes and listen on all network interfaces at port 8000. -> Option CQuick Check:
--workers 3 + --bind 0.0.0.0:8000 = Gunicorn will start 3 worker processes and listen on all network interfaces at port 8000. [OK]
- Assuming 0.0.0.0 is invalid
- Thinking workers default to 1 always
- Ignoring the bind port
gunicorn myproject.wsgi:application --workers two and get an error. What is the likely cause?Solution
Step 1: Check the --workers option value
The value 'two' is a word, but --workers expects an integer number.Step 2: Confirm Gunicorn option requirements
Gunicorn requires a numeric value for workers; using a word causes an error.Final Answer:
The workers option must be a number, not a word. -> Option AQuick Check:
--workers needs number, not text [OK]
- Assuming WSGI path error causes this
- Thinking --workers is unsupported
- Believing --bind is mandatory for this error
Solution
Step 1: Set the correct number of workers
The requirement is 4 workers, so use--workers 4.Step 2: Bind to port 8080 on all interfaces
Binding to0.0.0.0:8080makes the app accessible on port 8080 from any network interface.Step 3: Verify the WSGI application path
The pathmyproject.wsgi:applicationis the correct WSGI app for Django.Final Answer:
gunicorn --workers 4 --bind 0.0.0.0:8080 myproject.wsgi:application -> Option AQuick Check:
4 workers + bind 0.0.0.0:8080 = gunicorn --workers 4 --bind 0.0.0.0:8080 myproject.wsgi:application [OK]
- Binding only to localhost (127.0.0.1) limits access
- Using wrong port number
- Setting wrong number of workers
Ready to prove it?
Pass the test and this pattern moves unknown
