Django+Nginx+uWSGI

引言

记录如何在Centos7 部署 Django Web 服务,本篇文章记录如何使用nginx+uwsgi部署django 和如何使用nginx 直接反向代理django。因此就不再讲述如何安装python、nginx环境

前置

nginx + python

编写DjangoDemo

创建项目Ndemo

目录结构

初始结构
1
2
3
4
5
6
7
8
9
10
11
12
13
├─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
12
├─apps
│ └─NDemo
│ ├─asgi.py
│ ├─urls.py
│ ├─wsgi.py
│ └─ __init__.py
├─templates
├─conf
│ └─settings.py
├─manage.py
└─venv

调整结构配合调整相应文件内容

asgi.py wsgi.py manage.py 文件

  1. NDemo.settings 修改为 conf.settings

settings.py文件

  1. ROOT_URLCONF='NDemo.urls' 修改为 ROOT_URLCONF = 'apps.NDemo.urls'
  2. 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()),
]


运行项目并访问
1
2
3
4
python manage.py runserver 8000

http://127.0.0.1:8000/index/

上传至服务器

Nginx反向代理方式

运行Django

启动Django
1
2
python manage.py runserver 0.0.0.0:8000

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

反向代理

重启Django
1
python manage.py runserver 127.0.0.1:8000
修改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
4
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")

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
访问
1
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
访问
1
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")

如有错误,请 Mail 指正.

  • 本文作者: Ysc Test
  • 本文链接: https://ysctest.cn/posts/8fc1df1c.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
  • 法律声明: 本博客提供的所有包括但不限于(文章和API)等服务,仅用于学习,技术分享、交流。不得用于违法犯罪、损害国家利益。非法使用者本站不承担任何法律责任,并且本站保留追究其法律责任的权力!!!