如何用nginx关联django应用

发布于 2022-09-01 07:37:58 字数 556 浏览 17 评论 0

Django:1.6.5,python:2.6,centOS: 6.5 ,nginx:1.8.0-1

django应用下有wsgi.py和prod.ini文件

prod.ini文件:
图片描述

wsgi.py:
图片描述

可是怎么将其与nginx关联起来,我是通过yum install nginx安装的nginx,修改了/etc/nginx/conf.d/myapp.conf 文件,内容如下:
图片描述

请问该怎么配置呢?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏有森光若流苏 2022-09-08 07:37:58

/etc/nginx/conf.d/myapp.conf中的9090将为8000,然后将server 8000改为别的端口,如80端口。

然后为了显示图片、js、css,需要下面的配置

location /static/ {
    alias /home/www/MK_dream/MK_dream/static;
    if ($query_string) {
        expires max;
    }
    expires 30d;
}
location /static/media/ {
    alias /home/www/MK_dream/MK_dream/static/media;
    if ($query_string) {
        expires max;
    }
    expires 30d;
}

location ~* ^.+\.(cur|ico|json|gif|jpeg|cur|jpg|png|ttf|otf|js|css|mpg|avi|mp3|swf|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|txt|tar|mid|midi|wav|rtf|mpeg|woff)$ {
    root  /home/www/MK_dream/MK_dream/;
    expires 30d;
    access_log off;
}   

如果配置成功,建议可以使用gunicorn替换uwsgi,性能更好。

野生奥特曼 2022-09-08 07:37:58

如果排版不好请去http://www.chenxm.cc/post/275...阅读

本文采用uwsgi+nginx来部署Django。

这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。

一、uWSGI

  1. 安装

pip install uwsgi

  1. 测试uWSGI是否安装成功

        在终端中输入以下命令查看uwsgi的版本号,如果输出正常,说明uswgi已安装成功
    

$ uwsgi --version
2.0.15

  1. 编写一个简单的wsgi应用测试uwsgi是否能正常使用

        首先创建一个test.py文件(命令:vim 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

4.运行uwsgi:

uwsgi --http :8000 --wsgi-file test.py
参数解释:

http :8000表示使用http协议,端口号为8000,

wigi-file则表示要运行的wsgi应用程序文件。

uwsgi运行后打开浏览器,访问http://127.0.0.1:8000/ ,或者是相应服务器地址的8000端口,就可以看到hello world 页面了。

运行截图:

1.png

如果想要运行项目来测试

uwsgi --http :8000 --chdir 项目路径 -w 项目.wsg --static-map=/static=static

uwsgi --http :8000 --chdir /home/teacher/ -w teacher.wsgi --static-map=/static=static
uWSGI常用命令:

uwsgi --chdir=/path/to/your/project \

--module=mysite.wsgi:application \
--env DJANGO_SETTINGS_MODULE=mysite.settings \
--master --pidfile=/tmp/project-master.pid \
--socket=127.0.0.1:49152 \      # 可以ip地址,也可以是文件 
--processes=5 \                 # 进程数量
--uid=1000 --gid=2000 \         # 如果是root用户,uwsgi可以有删除权限
--harakiri=20 \                 # 一个请求超时时间
--max-requests=5000 \           # 一个工作进程最大请求数
--vacuum \                      # 退出时清楚环境
--home=/path/to/virtual/env \   # virtualenv的路径
-- static                       # 做一个映射,指定静态文件
--http                          # 这个就和runserver一样指定IP 端口
--daemonize=/var/log/uwsgi/yourproject.log      # 日志 
  1. 创建uwsgi配置文件

    在项目统计目录下创建文件夹script,(比如项目目录路径/home/project_teacher/teacher,那么scrip文件存放在/home/project/teacher/script)
    

$ pwd
/home/project_teacher

$ mkdir script
$ vim uwsgi.ini

配置信息如下:

[uwsgi]

项目目录

chdir=/opt/project_teacher/teacher/

指定项目的application

module=teacher.wsgi:application

进程个数

workers=5
pidfile=/opt/project_teacher/script/uwsgi.pid

指定IP端口

http=192.168.31.123:8080

指定静态文件

static-map=/static=/opt/test_project/teacher/static

启动uwsgi的用户名和用户组

uid=root
gid=root

启用主进程

master=true

自动移除unix Socket和pid文件当服务停止的时候

vacuum=true

序列化接受的内容,如果可能的话

thunder-lock=true

启用线程

enable-threads=true

设置自中断时间

harakiri=30

设置缓冲

post-buffering=4096

设置日志目录

daemonize=/opt/project_teacher/script/uwsgi.log

指定sock的文件路径

socket=/opt/project_teacher/script/uwsgi.sock

  1. 启动配置

$ uwsgi --ini uwsgi.ini # 启动uwsgi配置
[uwsgi-static] added mapping for /static => /home/trunk/static # 启动成功

$ uwsgi --stop uwsgi.pid # 关闭uwsgi
signal_pidfile()/kill(): Operation not permitted [core/uwsgi.c line 1659]

二、Nginx

  1. 安装

$ sudo apt-get install nginx #安装

  1. 检查nginx是否安装成功

$ /etc/init.d/nginx start

[ ok ] Starting nginx (via systemctl): nginx.service.
检查nginx是否启动成功

$ ps -ef |grep -i nginx
root 6961 1 0 03:56 ? 00:00:00 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
www-data 6962 6961 0 03:56 ? 00:00:00 nginx: worker process
pala 6985 2090 0 03:57 pts/0 00:00:00 grep --color=auto -i nginx
然后打开浏览器,访问ip地址,出现如下页面即代表nginx安装完成且可以正常启动。

2.png

Nginx常用命令

$ /etc/init.d/nginx start #启动
$ /etc/init.d/nginx stop #关闭
$ /etc/init.d/nginx restart #重启
三、Django + uWSGI +Nginx

创建一个xxx.conf配置文件(nginx的默认配置目录为/etc/nginx/conf.d)

$ vim /etc/nginx/conf.d/xxx.conf
配置文件信息如下:

server { # 这个server标识我要配置了

    listen 80;  # 我要监听那个端口
    server_name 10.129.205.183 ;  # 你访问的路径前面的url名称
    access_log  /var/log/nginx/access.log  main;  # Nginx日志配置
    charset  utf-8; # Nginx编码
    gzip on;  # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
    gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream;  # 支持压缩的类型

    error_page  404           /404.html;  # 错误页面
    error_page   500 502 503 504  /50x.html;  # 错误页面

    # 指定项目路径uwsgi
    location / {        # 这个location就和咱们Django的url(r'^admin/', admin.site.urls),
        include uwsgi_params;  # 导入一个Nginx模块他是用来和uWSGI进行通讯的
        uwsgi_connect_timeout 30;  # 设置连接uWSGI超时时间
        uwsgi_pass unix:/opt/project_teacher/script/uwsgi.sock;  # 指定uwsgi的sock文件所有动态请求就会直接丢给他
    }

    # 指定静态文件路径
    location /static/ {
        alias  /opt/project_teacher/teacher/static/;
        index  index.html index.htm;
    }
3. 重启nginx

$ /etc/init.d/nginx restart #重启

如果排版不好请去http://www.chenxm.cc/post/275...阅读

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文