如何用nginx关联django应用
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
/etc/nginx/conf.d/myapp.conf
中的9090将为8000,然后将server 8000改为别的端口,如80端口。然后为了显示图片、js、css,需要下面的配置
如果配置成功,建议可以使用
gunicorn
替换uwsgi
,性能更好。如果排版不好请去http://www.chenxm.cc/post/275...阅读
本文采用uwsgi+nginx来部署Django。
这种方式是将nginx作为服务器前端,将接受web所有的请求,统一管理。Nginx把所有的静态请求自己处理(静态文件处理是ngInx强项),然后把所有非静态请求通过uwsgi传递给Django,由Django来处理,从而完成一次web请求。
一、uWSGI
pip install uwsgi
测试uWSGI是否安装成功
$ uwsgi --version
2.0.15
编写一个简单的wsgi应用测试uwsgi是否能正常使用
test.py
def application(env, start_response):
4.运行uwsgi:
uwsgi --http :8000 --wsgi-file test.py
参数解释:
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 \
创建uwsgi配置文件
$ 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
$ 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
$ sudo apt-get install 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标识我要配置了
$ /etc/init.d/nginx restart #重启
如果排版不好请去http://www.chenxm.cc/post/275...阅读