使用Nginx+uWsgi实现Python的Django框架站点动静分离
由于: Django处理静态文件不太友好; 所以考虑结合nginx,使用nignx做它擅长的路由分发功能;同时做动静分离,即Http请求统一由Nginx进行分发,静态文件由Nginx处理,并返回给客户端;而动态的请求,则分发到uWsgi,由uWsgi再分发给Django进行处理。即客户端 <-> nginx <-> socket <-> uwsgi <-> Django 一、环境 系统:centOS 6
所以,安装前,先在控制台输入python ―version查看当前python的默认版本,如果在2.7以下,则修改默认版本。(详见附录) 二、安装nginx、uWsgi 安装 nginxsudo yum install nginx 安装 pipsudo yum install python-pip 安装 uWsgisudo pip uwsgi
# test.pydef application(env,start_response): start_response('200 OK',[('Content-Type','text/html')]) return "Hello World" 启动以Http方式访问的uWsgi,在test.py同一目录下,输入如下命令行(8001是监听的端口,可以改成你想要的端口) uwsgi --http :8001 --wsgi-file test.py 然后在浏览器中,输入ip地址:8001,看是否响应hello world,是则说明安装成功 四、连接Django和uWsgi 在项目目录下创建wsgi.py文件 wsgi.py内容如下: """ WSGI config for WHPAIWechat project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file,see https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/ """ import osfrom django.core.wsgi import get_wsgi_applicationos.environ.setdefault("DJANGO_SETTINGS_MODULE","WHPAIWechat.settings") application = get_wsgi_application() 启动以Http方式访问uWsgiuwsgi --http :8000 --chdir /home/jiayandev/WHPAIWechat/ --wsgi-file WHPAIWechat/wsgi.py 五、连接uWsgi和nginx url包含.css、.js等服务器特定目录,设置根目录 更多规则可以视业务情况而定,完整的配置如下: upstream django { server 127.0.0.1:8000; # 注意8000是上述uwsgi监听的端口 } server { listen 80 default_server; server_name _; #charset koi8-r; #access_log logs/host.access.log main; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location = /404.html { root /usr/share/nginx/html; } # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } location ~ .html$ { root /usr/share/nginx/html/front; index index.html index.htm; } location ~ .(png|jpg|jpeg|css|img|js|flv|swf|download|eot|svg|ttf|woff|woff2|otf)$ { root /usr/share/nginx/html/front; } # 以上都不匹配的的访问分发到uwsgi上 location / { include /etc/nginx/uwsgi_params; #详细看下文 uwsgi_pass django; } * # PHP分到 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; #}* } 同时,uswgi_param内容如下,照抄即可 uwsgi_param QUERY_STRING $query_string; uwsgi_param REQUEST_METHOD $request_method; uwsgi_param CONTENT_TYPE $content_type; uwsgi_param CONTENT_LENGTH $content_length; uwsgi_param REQUEST_URI $request_uri; uwsgi_param PATH_INFO $document_uri; uwsgi_param DOCUMENT_ROOT $document_root; uwsgi_param SERVER_PROTOCOL $server_protocol; uwsgi_param REMOTE_ADDR $remote_addr; uwsgi_param REMOTE_PORT $remote_port; uwsgi_param SERVER_PORT $server_port; uwsgi_param SERVER_NAME $server_name; 配置完成后,重启或者reload nginx配置即可生效 sudo service nginx restart reload: sudo service nginx reload 然后直接访问,看看有什么不一样: 六、优化uwsgi的启动 #WHPAIWechat_uwsgi.ini[uwsgi] socket = 127.0.0.1:8000 chdir = /home/jiayandev/WHPAIWechat/ wsgi-file = WHPAIWechat/wsgi.py processes = 4 threads = 2 master=True #设置此参数,有一个主进程 pidfile= pidfile/project-master.pid #主进程id写入文件里 vacuum=True #退出时,清理环境 daemonize = uwsgi.log #守护进程的方式运行,log日志存在此log文件里 启动uwsgi命令变成uwsgi WHPAIWechat_uwsgi.ini (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |