django配置uwsgi-nginx全过程


我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文将以 Nginx 为例。

一:uwsgi介绍

说到uWSGI不得不提WSGI和uwsgi这两个知识点。

1.1:WSGI:

全称是web server gateway interface(web 服务期网关接口),它是一种描述web服务器如何与应用程序(django、flask)通信的规范。

server和application的规范在PEP3333中有具体的描述,要实现WSGI协议,必须同时实现web server和web application,当前运行在WSGI协议之上的web 框架有django、flask、bottle。

1.2:uwsgi:

与WSGI一样是uWSGI通信的一种协议,用于定义传输信息类型(type of information)。每一个uwsgi packet前4byte为传输信息类型的描述,与WSGI协议是两种东西,据说是fcgi协议的10倍快。

uWSGI:
是一个全功能的http服务器,实现了WSGI规范、uwsgi协议、http协议等。它要做的就是把http协议转化为语言支持的网络协议。比如把http协议转化成WSGI规范,让python可以直接使用。

1.3:访问过程

未配置uwsgi访问过程如图1

图一

配置uwsgi访问过程

在这里插入图片描述

2:uwsgi配置

2.1:安装uwsgi

注意:我在Mac上配置uwsgi和nginx

pip3 install uwsgi

2.2:配置ini文件

在项目的同目录下创建一个uwsgi.ini文件,文件名可以随意,一般使用项目名,我为了便于区分所以起名uwsgi
在这里插入图片描述

2.3:编辑文件 ,设置uwsgi属性

在这里插入图片描述

[uwsgi]
http=127.0.0.1:8007
chdir=/Users/wei/python_project/pyhon_testing/Mars
wsgi-file=Mars/wsgi.py
process=4
threads=2
pidfile=uwsgi.pid
daemonize=uwsgi.log
master=True

配置解析:
注意: chdir和wsgi-file.py一定不能配置错的路径,不然会出错,很多人配置错误就是因为这两个找不到项目

#开头必须是[uwsgi]
[uwsgi]

#现在还没有配置nginx所以暂时使用http
http=127.0.0.1:8007

#配置项目路径,项目的所在目录**必须是:绝对路径**
chdir=/Users/wei/python_project/pyhon_testing/Mars

#配置wsgi接口模块文件路径,**相对路径**
wsgi-file=Mars/wsgi.py

#配置启动的进程数,这个多配无意,按照自己电脑核去配置
processes=4
#配置每个进程的线程数
threads=2
#配置启动管理主进程
master=True
#配置存放主进程的进程号文件,启动uwsgi后**uwsgi.pid文件会自动生成**
pidfile=uwsgi.pid
#配置dump日志记录
daemonize=uwsgi.log`

2.4:启动运行uwsgi

在这里插入图片描述

cd 到uwsgi.ini配置文件所在目录
命令:uwsgi --ini uwsgi.ini
命令解析:uwsgi和–ini是关键命令不可以错,uwsgi.ini是ini配置文件名

在这里插入图片描述

这就代表着启动好了,但是我们还要看一下是否真的成功

ps aux|grep 'uwsgi'

在这里插入图片描述
这就是说明启动成功

2.5:停止uwsgi

cd 到uwsgi.pid配置文件所在目录
注意: uwsgi.pid是自动生成的,所以按照我们上面的配置文件路径,uwsgi.pid和uwsgi.ini在同一个目录下

uwsgi --stop uwsgi.pid

2.6:重新启动

uwsgi --reload uwsgi.pid 

2.7:查看端口是否被占用

sudo lsof -i :8007

在这里插入图片描述
看到有,我们将端口kill掉

sudo kill -9 43057

43057是端口pid

然后访问你的路由测试一下,访问成功说明配置没有问题

2.8:uwsgi运行说明

在这里插入图片描述

2.9:uwsgi常见问题

在这里插入图片描述

二:Nginx

Nginx是一款轻量级的Web服务器、反向代理服务器,由于它的内存占用少,启动极快,高并发能力强,在互联网项目中广泛应用。

在这里插入图片描述

上图基本上说明了当下流行的技术架构,其中Nginx有点入口网关的味道。

2.1:反向代理服务器?

经常听人说到一些术语,如反向代理,那么什么是反向代理,什么又是正向代理呢?

2.2:正向代理:

在这里插入图片描述

2.3:反向代理:

在这里插入图片描述

上面就是一个简单的介绍,如果想深入了解可以查看中文官网

django和nginx的流程
在这里插入图片描述

2.4:Mac OS Nginx安装

  1. 安装(可以用 brew 安装)
sudo brew install nginx
  1. 查看 nginx 版本
nginx -v

在这里插入图片描述

  1. 配置Nginx
    首先查找目录,我的目录在/usr/local/etc/nginx
    在这里插入图片描述
  2. 修改nginx.conf文件
    命令:vim /usr/local/etc/nginx/nginx.conf
    下面这是默认的配置项
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
	
    server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;

            root   /Users/XXX/Downloads/Autotestplat-master;# 项目目录
            index  index.html index.htm;

	    uwsgi_pass 0.0.0.0:8001;
            include /usr/local/etc/nginx/uwsgi_params; # uwsgi_params文件的地址
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}
    include servers/*;
}

  1. 新增内容
server {
        listen       8080;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;

            # root   /Users/liuwei/Downloads/Autotestplat-master;# 项目目录
            # index  index.html index.htm;
            
            # 新添加的内容
            uwsgi_pass 127.0.0.1:8007;
            include /usr/local/etc/nginx/uwsgi_params; # uwsgi_params文件的地址  
        }

只添加了两行,include要改成自己的uwsgi_params文件路径
新增内容

uwsgi_pass 127.0.0.1:8007;
include /usr/local/etc/nginx/uwsgi_params;
  1. 检查nginx配置文件语法是否有问题,命令

    如果配置有问题,则会提示某一行的错误

nginx -t

在这里插入图片描述
图上:我这个是没有问题的

  1. 启动 nginx
    查找nginx路径which nginx
sudo nginx

也可以使用下面的命令启动,但是配置文件nginx.conf修改后用这个命令执行不生效,故不建议使用:

sudo brew services start nginx

2.5:修改uwsgi配置

前面我们访问是通过http方式请求,uwsgi进行转发,现在我们配置了nginx,那么就要通过nginx负责请求并转发给uwsgi

在这里插入图片描述

说明: Nginx负责接收请求,并把请求转发给后面的uWSGI,此模式下,uWSGI需要以socket模式启动

[uwsgi]
# 去掉
# http=127.0.0.1:8007
# 改为
socket=127.0.0.1:8007

2.5.1:uwsgi配置文件修改如下

[uwsgi]
# http=127.0.0.1:8007
socket=127.0.0.1:8007
chdir=/Users/xxx/python_project/pyhon_testing/Mars
wsgi-file=Mars/wsgi.py
process=4
threads=2
pidfile=uwsgi.pid
daemonize=uwsgi.log
master=True

2.5.2:重启uwsgi

在uwsgi.ini所在文件下执行

uwsgi --ini uwsgi.ini

在这里插入图片描述
大家有没有发现uwsgi配置了socket后,启动数量不一样了和之前,不一样说明配置成功了,因为它http模式启动,它会自动启动一个http进程来进行解读,所以它比socket多一个进程

2.5.3:访问项目

首先我们看一下这个图
在这里插入图片描述
我们应该先访问nginx也就是80,然后又nginx进行分配到uwsgi8007
一定一定要记住,先访问80也就是nginx,nginx默认是80

2.5.4:Nginx命令

# nginx停止命令
nginx -s stop

# 启动
nginx

# 重启
nginx -s reload

小结

1:uwsgi修改后一定要重启,重启
2:nginx的路径配置一定要正确,一定要正确
3:uwsgi的配置路径一定要正确,一定要正确
4:启动项目,我的话先启动nginx然后在启动uwsgi,这个没有特殊要求
5:如果配置好,然后启动没有任何问题,但是还是无法访问,是这个原因
在这里插入图片描述

listen  8000; # 这个端口是nginx用来监听uwsgi的,默认的是80,总之项目是通过下面的server_name:8080来访问的

三:Nginx+uWSGI排错思路

排查宗旨:——>看日志!看日志!!看日志!!!

注意:如果找不到nginx的日志位置,可以使用nginx -t来查看

  • nginx日志位置:
    异常信息:/usr/local/var/log/nginx/error.log
    error.log里面包含了所有请求出错的错误信息
    在这里插入图片描述

    正常访问信息:/usr/local/var/log/nginx/access.log
    access.log里面包含了所有请求访问日志,来一个请求一条记录
    在这里插入图片描述

3.1:访问127.0.0.1:80地址,502响应

  • 502代表nginx反向代理配置成功,但是对应的uWSGI未启动

以上就是我的配置全过程,如果对你有帮助,请给我点个赞,内容可能有点乱,但是我是按照自己的配置流程所记录的,感谢大家,参考官方

3.2:访问127.0.0.1:80地址,404响应

  1. 路由的确不在django配置中
  2. nginx配置错误,未禁止掉,try_files

3.3:配置静态文件步骤

在这里插入图片描述

在这里插入图片描述

附加
pip freeze > pkg.txt
将当前生产环境下 Python 的模块收集起来存放到 pkg.txt 文件里
pip install -r pkg.txt
在部署环境下降生产环境下的需要模块全部安装

  • 7
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值