Nginx与uWSGI架构概述
Django应用的部署方案多样,其中结合Nginx与uWSGI是业界较为通用的实践方式。
该架构通常将Nginx置于最前端以接管所有WEB请求并统一管理流量。Nginx利用其优势自行处理所有静态资源请求,随后将动态或非静态请求经由uWSGI协议转发给Django框架,由后者执行具体逻辑以响应客户端。
在此架构中,uWSGI充当关键的角色,如同桥梁一般连接Nginx与Django应用。
鉴于Linux系统在服务器领域的卓越表现,本次部署全过程将在Ubuntu操作系统环境下进行。
Ubuntu安装配置Nginx
一、安装Nginx
Nginx是一款轻量级的Web服务器、反向代理服务器以及电子邮件(IMAP/POP3)代理服务器,基于BSD-like协议发布。其显著特点包括内存占用低且并发处理能力极强,在同类型网页服务器中表现优异。
作为当前流行的Web服务器软件,利用Nginx部署Django应用具有广泛参考价值,此处作简要介绍。
Nginx官方网站:http://nginx.org/
打开Ubuntu控制台(快捷键ctrl+alt+t),通过Ubuntu官方仓库进行安装。
fnngj@ubuntu:~$ sudo apt-get install nginx #安装
启动Nginx服务:
fnngj@ubuntu:~$ /etc/init.d/nginx start #启动
fnngj@ubuntu:~$ /etc/init.d/nginx stop #关闭
fnngj@ubuntu:~$ /etc/init.d/nginx restart #重启
若需修改Nginx默认端口,请编辑/etc/nginx/nginx.conf配置文件。
server {
listen 8088; # 修改端口号
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
通常在文件第36行左右,将默认的80端口更改为其他端口,例如8088。这是因为默认80端口常被其他应用程序占用。
修改完成后,执行上述命令重启Nginx服务。访问地址:http://127.0.0.1:8088/
若页面显示如上图所示内容,则表明Nginx已成功启动。
uWSGI安装与基础测试
二、安装uWSGI
使用pip工具安装uWSGI组件。
root@ubuntu:/etc# python3 -m pip install uwsgi
为测试uWSGI功能,创建test.py文件:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
通过uWSGI命令运行该测试文件。
fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --wsgi-file test.py

接下来配置Django与uWSGI的连接。假设Django项目路径为:/home/fnngj/pydj/myweb
复制代码 代码如下: fnngj@ubuntu:~/pydj$ uwsgi --http :8001 --chdir /home/fnngj/pydj/myweb/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191
常用选项说明:
http :指定协议类型及端口号
processes :设定开启的进程数量
workers :设定开启的进程数量,等同于processes(官网释义为spawn the specified number ofworkers / processes)
chdir :指定应用加载前的工作目录(chdir to specified directory before apps loading)
wsgi-file :载入指定的wsgi文件(load .wsgi file)
stats :在指定地址上启用状态监控服务(enable the stats server on the specified address)
threads :设置运行线程数。受限于GIL机制,作者认为此参数实际作用有限。(run each worker in prethreaded mode with the specified number of threads)
master :允许主进程存在(enable master process)
daemonize :使进程后台运行,并将日志输出至指定文件或UDP服务器(daemonize uWSGI)。实践中最常用的是将运行记录写入本地文件。
pidfile :指定PID文件位置,用于记录主进程的PID号。
vacuum :服务器退出时自动清理环境,删除生成的unix socket文件和pid文件(try to remove all of the generated file/sockets)
Nginx与uWSGI整合部署
三、Nginx+uWSGI+Django整合
接下来将三者结合部署。首先列出项目所需的关键文件结构:
myweb/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
在使用Django创建myweb项目时,子目录myweb下已自动生成wsgi.py文件。因此,只需额外创建myweb_uwsgi.ini配置文件即可。uWSGI支持多种配置格式(如xml、ini等),此处选用ini格式。
# myweb_uwsgi.ini file
[uwsgi]
# Django-related settings
socket = :8000
# the base directory (full path)
chdir = /home/fnngj/pydj/myweb
# Django s wsgi file
module = myweb.wsgi
# process-related settings
# master
master = true
# maximum number of worker processes
processes = 4
# ... with appropriate permissions - may be needed
# chmod-socket = 664
# clear environment on exit
vacuum = true
该配置本质上是将上一小节中命令行跟随大量参数的形式转化为文件化配置。
socket 指定项目执行的端口号。
chdir 指定项目的根目录。
module myweb.wsgi 可理解为:相对于myweb_uwsgi.ini文件所在的同级目录下,存在myweb目录,其中包含wsgi.py文件。
其余参数含义可参考前文参数介绍部分。
随后切换到myweb项目目录,通过uWSGI命令读取myweb_uwsgi.ini文件来启动项目。
fnngj@ubuntu:~$ cd /home/fnngj/pydj/myweb/
fnngj@ubuntu:~/pydj/myweb$ uwsgi --ini myweb_uwsgi.ini
[uWSGI] getting INI configuration from myweb_uwsgi.ini
*** Starting uWSGI 2.0.12 (32bit) on [Sat Mar 12 13:05:06 2026] ***
compiled with version: 4.8.4 on 26 January 2026 06:14:41
os: Linux-3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:18:00 UTC 2026
nodename: ubuntu
machine: i686
clock source: unix
detected number of CPU cores: 2
current working directory: /home/fnngj/pydj/myweb
detected binary path: /usr/local/bin/uwsgi
!!! no internal routing support, rebuild with pcre support !!!
chdir() to /home/fnngj/pydj/myweb
your processes number limit is 15962
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
thunder lock: disabled (you can enable it with --thunder-lock)
uwsgi socket 0 bound to TCP address :8000 fd 3
Python version: 3.4.3 (default, Oct 14 2026, 20:37:06) [GCC 4.8.4]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x8b52dc0
your server socket listen backlog is limited to 100 connections
your mercy for graceful operations on workers is 60 seconds
mapped 319920 bytes (312 KB) for 4 cores
*** Operational MODE: preforking ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x8b52dc0 pid: 7158 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI master process (pid: 7158)
spawned uWSGI worker 1 (pid: 7160, cores: 1)
spawned uWSGI worker 2 (pid: 7161, cores: 1)
spawned uWSGI worker 3 (pid: 7162, cores: 1)
spawned uWSGI worker 4 (pid: 7163, cores: 1)
注意观察uWSGI的启动日志,若出现错误,需检查配置文件中的参数设置是否正确。
接着需要修改nginx.conf配置文件。打开/etc/nginx/nginx.conf文件,添加以下内容。
……
server {
listen 8099;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/myweb_access.log;
error_log /var/log/nginx/myweb_error.log;
client_max_body_size 75M;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
}
location /static {
expires 30d;
autoindex on;
add_header Cache-Control private;
alias /home/fnngj/pydj/myweb/static/;
}
}
……
listen 指定Nginx代理uWSGI对外暴露的端口号。
server_name 多数网络资料建议设置为网址(如www.example.com),但此处若设为网址会导致无法访问,故指定为本机默认IP。
在配置过程中,曾对Nginx如何与uWSGI建立关联感到困惑。目前看来,核心在于以下两行配置。
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
include指令必须指定为uwsgi_params;而uwsgi_pass指向的本机IP端口必须与myweb_uwsgi.ini配置文件中的设置保持一致。
现在重新启动Nginx,参考前述重启Nginx的命令。然后访问:http://127.0.0.1:8099/
通过该IP和端口的指向,请求首先进入Nginx。若在页面上执行某些操作,可见这些请求最终被转发至uWSGI进行处理。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持本站。