django 高性能部署 nginx uwsgi

本文详细介绍如何使用uwsgi和nginx进行高并发项目部署。包括uwsgi安装与基本配置、结合Django项目运行实例,以及nginx配置步骤。通过具体实例演示了如何实现本地访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 由于项目需求,需要接入高并发和域名,根据市场调研,最终决定使用uwsgi+nginx
  • uwsgi官网介绍:
    uWSGI 是一个(巨大的) C 应用,所以你需要一个 C 编译器(比如 gcc 或者 clang)和 Python 开发版头文件。
  • 首先安装uwsgi
pip install uwsgi
  • 测试uwsgi功能是否正常
# 创建test.py 文件
# test.py
def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return [b"Hello World"] # python3
    #return ["Hello World"] # python2

#运行
uwsgi --http :8000 --wsgi-file test.py

#浏览器展示
hello world 
  • 配置uwsgi文件,看字义就知道,不过多解释
# uwsgi.ini
[uwsgi]
socket = 127.0.0.1:3031
static-map=/static=/usr/local/py_work/RS/pools/static
vacuum=true
chdir = /usr/local/py_work/RS
wsgi-file = /usr/local/py_work/RS/RS/wsgi.py
processes = 4
threads = 2
;stats = 127.0.0.1:9191
daemonize=/usr/local/uwsgi/RS.log
  • 启动带django项目的uwsgi
uwgsi uwsgi.ini
# 出现以下log变为成功
[uWSGI] getting INI configuration from RS/uwsgi.ini
[uwsgi-static] added mapping for /static => /usr/local/py_work/RS/pools/static
  • 安装nginx,mac系统直接brew就好了,没别的,中间有个小坑,这种自己装的和公司装的目录结构有点小不一样,迷惑了一会,公司装的nginx conf文件是在v.host 文件夹下面,自己安装的是在servers文件夹下
  • 配置nginx with uwsgi,官网说明是这样的,客户端到服务端的通信
the web client <-> the web server
# the upstream component nginx needs to connect to
upstream django {
    # server unix:///path/to/your/mysite/mysite.sock; # for a file socket
    server 127.0.0.1:8001; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      8000;
    # the domain name it will serve for
    server_name .example.com; # substitute your machine's IP address or FQDN
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    # Django media
    location /media  {
        alias /path/to/your/mysite/media;  # your Django project's media files - amend as required
    }

    location /static {
        alias /path/to/your/mysite/static; # your Django project's static files - amend as required
    }

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed
    }
}
  • 访问本地8080端口出现页面即为成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值