Django + Nginx + uWSGI Configuration
Table of Contents
Django is an open-source web application framework written in Python.
Nginx is an open-source, high-performance HTTP server and reverse proxy server.
uWSGI is an implementation of the Web Server Gateway Interface (WSGI), used for communication between a web server and Python web applications.

References:
https://cloud.tencent.com/developer/article/1594840
Nginx official documentation: https://nginx.org/en/docs/beginners_guide.html
Difference between Nginx and uWSGI:
- Both Nginx and uWSGI are web servers and can both be used to deploy services like Django.
- Nginx: Extremely efficient at serving static resources; also supports load balancing, reverse proxying, and attack mitigation.
- uWSGI: Single-node deployment, easier to set up, slightly lower performance, but supports a wider variety of web protocols.
Difference between uWSGI and uwsgi:
- uWSGI: A web server.
- uwsgi: A web protocol.
Difference between WSGI and uwsgi:
- uwsgi: Also a web protocol, faster transmission (binary storage, whereas other web protocols use string storage).
- WSGI: A Python-specific web protocol.
Install Django
pip install Django
ALLOWED_HOSTS = ['*']
Install uWSGI
Unable to find libpython3.12.a.
This is a Python version issue. Solution:
conda create -n django_env python=3.9
conda activate django_env
conda install uwsgi
Then install Django again.
Install Nginx
yum install nginx
Start the service:
systemctl start nginx
systemctl restart nginx
At this point, visiting the server’s IP address should display the Nginx welcome page.
Enable auto-start on boot:
systemctl enable nginx
Stop the service:
systemctl stop nginx
Configure Django
django-admin startproject demosite
cd demosite
python manage.py runserver 0.0.0.0:8002
For external access, modify settings.py:
ALLOWED_HOSTS = ['*']
Configure uWSGI
Create /etc/uwsgi9090.ini with the following content:
[uwsgi]
socket = 0.0.0.0:3031
chdir = /home/wjr/test/django/demosite
wsgi-file = /home/wjr/test/django/demosite/wsgi.py
processes = 5
threads = 30
master = true
daemonize = /home/wjr/test/django/demosite/uwsgi.log
module=demosite.wsgi
pidfile = /home/wjr/test/django/demosite/uwsgi.pid
chmod-socket=666
enable-threads = true
# Start Django with uWSGI: must be run from inside the project directory
uwsgi --http x.x.x.x:80 --ini /etc/uwsgi9090.ini --static-map=/static=static
# Access the project: http://x.x.x.x
# Stop uWSGI
pkill -9 -f uwsgi
# or
uwsgi --stop /home/wjr/test/django/demosite/uwsgi.pid
Configure Nginx
Create and edit nginx.conf in /etc/nginx/conf.d/:
server {
listen 8888;
server_name 192.168.56.11;
client_max_body_size 5M;
gzip on;
gzip_buffers 32 4K;#buffer 32 blocks of 4K each in memory
gzip_comp_level 6 ;#compression level, 6 is recommended
gzip_min_length 4000;#minimum length to start compression: 4 bytes
gzip_types text/plain application/json application/javascript application/x-javascript application/css application/xml application/xml+rss text/javascript application/x-httpd-php image/jpeg image/gif image/png image/x-ms-bmp;
location / {
include uwsgi_params;
uwsgi_pass 0.0.0.0:3031;
uwsgi_ignore_client_abort on;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
[root@linux-node1 demo2]# systemctl restart nginx # Start Nginx
[root@linux-node1 demo2]# uwsgi --ini /etc/uwsgi9090.ini # Start the Django project via uWSGI
# Access the project at http://192.168.56.11:8888/
[root@linux-node1 demo2]# uwsgi --stop uwsgi.pid # Stop uWSGI
If there is a port conflict, check for the conflicting process:
lsof -i :80
Flask Notes
Flask is a lightweight web application framework written in Python, based on the Werkzeug WSGI toolkit and the Jinja2 template engine.
Installation:
pip install flask
Create a Python file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello_world():
return "<p>Hello, World!</p>"
Run:
flask --app hello run --host=0.0.0.0 --port=5739
Test with curl:
curl -X POST http://x.x.x.x:5739 -H "Content-Type: application/json" -d '{"text":"️️测试文字"}'