Django+Nginx+uWSGI

  1. 简述

    记录如何在Centos7 部署 Django Web 服务,本篇文章记录如何使用nginx+uwsgi部署django 和如何使用nginx 直接反向代理django。

  2. 依赖
    • Python
    • Nginx
  3. 编写DjangoDemo

    创建项目Ndemo

    • 目录结构

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      ├─NDemo
      │ ├─asgi.py
      │ ├─settings.py
      │ ├─urls.py
      │ ├─wsgi.py
      │ └─ __init__.py
      ├─templates
      ├─manage.py
      └─venv
      ├─Include
      ├─Lib
      └─Scripts
    • Apps、Conf结构

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      ├─apps
      │ └─NDemo
      │ ├─asgi.py
      │ ├─urls.py
      │ ├─wsgi.py
      │ └─ __init__.py
      ├─templates
      ├─conf
      │ └─settings.py
      ├─manage.py
      └─venv
    • 调整结构配合调整相应文件内容

      asgi.py wsgi.py manage.py 文件

      • NDemo.settings 修改为 conf.settings

      settings.py文件

      • ROOT_URLCONF='NDemo.urls' 修改为 ROOT_URLCONF = 'apps.NDemo.urls'

      • WSGI_APPLICATION = 'NDemo.wsgi.application' 修改为 WSGI_APPLICATION = 'apps.NDemo.wsgi.application'

    • 编写View,新建view apps.NDemo.view.py

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      #!/usr/bin/env python
      # -*- coding: UTF-8 -*-
      # __author__ = Ysc
      from django.views import View
      from django.http.response import HttpResponse


      class Index(View):

      def get(self, request, *args, **kwargs):
      return HttpResponse("hello django")

    • 修改urlpatterns apps.NDemo.urls.py

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      from django.contrib import admin
      from django.urls import path

      # 新增
      from apps.NDemo.view import Index

      urlpatterns = [
      path('admin/', admin.site.urls),
         # 新增
         path('index/', Index.as_view()),
      ]


    • 运行项目并访问

    • $ python manage.py runserver 8000
      
      http://127.0.0.1:8000/index/
      
      1
      2
      3
      4
      5
      6
      7
      8
      9

      ![](https://file.ysctest.cn/blog/20250817/4ad9468b-398c-4fdb-ab75-0d6030fba33b.png)

      4. ##### Nginx反向代理方式

      * 运行Django,命令行启动Django

      ```bash
      $ python manage.py runserver 0.0.0.0:8000
    • 访问

      1
      2
      # 有防火墙的记得开放端口
      http://192.168.31.198:8000/index/

    • 反向代理,重启Django

    • 修改nginx.conf

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      server
      {
      listen 80;
      server_name 192.168.31.198;

      location / {
      proxy_pass http://127.0.0.1:8000/;
      # 获取真实IP
      proxy_set_header REMOTE_ADDR $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
      # 添加路由版本
      location /v1/ {
      proxy_pass http://127.0.0.1:8000/;
      proxy_redirect http://127.0.0.1:8000/ /v1/;
      # 获取真实IP
      proxy_set_header REMOTE_ADDR $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
      }
    • 重载Nginx配置

      1
      2
      3
      $ nginx -t

      $ nginx -s reload
    • 访问

      1
      2
      3
      http://192.168.31.198/index/

      http://192.168.31.198/v1/index/

    • 反向代理后获取真实IP,修改view文件

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      #!/usr/bin/env python
      # -*- coding: UTF-8 -*-
      # __author__ = Ysc
      from django.views import View
      from django.http.response import HttpResponse


      class Index(View):

      def get(self, request, *args, **kwargs):
      # 使用真实ip
      print(request.META.get('HTTP_X_FORWARDED_FOR'))
      return HttpResponse("hello django")

  4. Nginx+uWSGI方式
    • 安装uWSGI

      1
      2
      # 建议使用虚拟环境
      $ pip install uWSGI
    • 无路由配置

    • 创建NDemo.ini

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      # 这里就不再讲述如何使用命令行配置uWSGI
      [uwsgi]
      # django启动的端口
      socket = 127.0.0.1:8000
      # 项目目录
      chdir = /www/NDemo/
      wsgi-file = apps/NDemo/wsgi.py
      processes = 4
      threads = 2
      # 守护进程
      daemonize = /www/NDemo/NDemo.log
      # pid文件路径
      safe-pidfile = /www/NDemo/NDemo.pid
    • 启动uWSGI

      1
      $ uwsgi NDemo.ini

    • 配置Nginx

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      # nginx.conf
      server
      {
      listen 80;
      server_name 192.168.31.198;

      location / {
      include uwsgi_params;
      # 填写配置文件中地址
      uwsgi_pass 127.0.0.1:8000;
      # 获取真实IP
      uwsgi_param REMOTE_ADDR $remote_addr;
      uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;

      }

      }
    • 重载Nginx配置

      1
      2
      $ nginx -t
      $ nginx -s reload
    • 访问 http://192.168.31.198/index/

    • 路由配置,修改 NDemo.ini

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      # 这里就不再讲述如何使用命令行配置uWSGI
      [uwsgi]
      # django启动的端口
      socket = 127.0.0.1:8000
      # 项目目录
      chdir = /www/NDemo/
      # 配置访问路由  v1
      mount = /v1=apps/NDemo/wsgi.py
      manage-script-name = true
      processes = 4
      threads = 2
      daemonize = /www/NDemo/NDemo.log
      safe-pidfile = /www/NDemo/NDemo.pid
    • 修改配置Nginx

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      # nginx.conf
      server
      {
      listen 80;
      server_name 192.168.31.198;

      location /v1/ {
      include uwsgi_params;
      # 填写配置文件中地址
      uwsgi_pass 127.0.0.1:8000;
      # 获取真实IP
      uwsgi_param REMOTE_ADDR $remote_addr;
      uwsgi_param X-Forwarded-For $proxy_add_x_forwarded_for;

      }

      }
    • 重载Nginx配置

      1
      2
      $ nginx -t
      $ nginx -s reload
    • 访问 http://192.168.31.198/v1/index/

    • uwsgi获取真实IP

      修改views.py

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      # 前提前述nginx已按步骤配置

      #!/usr/bin/env python
      # -*- coding: UTF-8 -*-
      # __author__ = Ysc
      from django.views import View
      from django.http.response import HttpResponse


      class Index(View):

      def get(self, request, *args, **kwargs):
      # 使用真实ip
      # print(request.META.get('HTTP_X_FORWARDED_FOR'))
             print(request.META.get('X-Forwarded-For'))
             return HttpResponse("hello django")