Windows系统下使用flup搭建Nginx和Python环境的方法


Posted in Python onDecember 25, 2015

首先确保你的电脑里已经安装了Python和Django,接下来我们还需要两个组件,nginx服务器和flup(Python的FastCGI组件)
nginx下载地址:http://nginx.org/en/download.html
flup下载地址:http://www.saddi.com/software/flup/dist/flup-1.0.2.tar.gz
与Linux下不同的是,nginx在windows下是以一个应用程序的方式运行,而不是以一个服务运行(难怪没人在windows服务器上用nginx)
把刚刚下载好的两个压缩包都解压到C:\nginx\, C:\flup\(目录可自己选择,这里只做个演示)然后用python setup.py install 命令

安装flup,接着就要配置nginx了,打开C:\nginx\conf\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    80; 
    server_name localhost; 
 
    #charset koi8-r; 
 
    #access_log logs/host.access.log main; 
 
    location / { 
      root  html; 
      index index.html index.htm; 
    } 
 
    #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; 
    #} 
     
    # 静态资源 
    location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|js)$ 
    { 
      root e:/gin/gin/; 
      expires 30d; 
      break; 
    } 
 
    location ~ ^/static/ { 
      root e:/gin/gin/; 
      expires 30d; 
      break; 
    }  
 
    location ~ ^/ { 
      # 指定 fastcgi 的主机和端口 
      fastcgi_pass 127.0.0.1:8051; 
      fastcgi_param PATH_INFO $fastcgi_script_name; 
      fastcgi_param REQUEST_METHOD $request_method; 
      fastcgi_param QUERY_STRING $query_string; 
      fastcgi_param CONTENT_TYPE $content_type; 
      fastcgi_param CONTENT_LENGTH $content_length; 
      fastcgi_param SERVER_PROTOCOL $server_protocol; 
      fastcgi_param SERVER_PORT $server_port; 
      fastcgi_param SERVER_NAME $server_name; 
      fastcgi_pass_header Authorization; 
      fastcgi_intercept_errors off; 
    } 
  } 
 
  # 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; 
  #  server_name localhost; 
 
  #  ssl         on; 
  #  ssl_certificate   cert.pem; 
  #  ssl_certificate_key cert.key; 
 
  #  ssl_session_timeout 5m; 
 
  #  ssl_protocols SSLv2 SSLv3 TLSv1; 
  #  ssl_ciphers HIGH:!aNULL:!MD5; 
  #  ssl_prefer_server_ciphers  on; 
 
  #  location / { 
  #    root  html; 
  #    index index.html index.htm; 
  #  } 
  #} 
 
}

需要注意的是,对于不需要url rewrite的目录,比如存放css和图片的目录,需要在配置文件里指明,否则将无法访问这些文件

location ~ ^/static/ {
      root e:/gin/gin/;
      expires 30d;
      break;
    }

最后一步就是运行nginx服务器,并且用FastCGI运行你的Django项目了
进入nginx的目录:

cd c:\nginx\
  start nginx

然后在浏览器里访问http://loaclhost/ 就应该可以看到nginx的欢迎界面了。最后进入你的Django项目的根目录,然后用一下命令来运行服务器:

python manage.py runfcgi method=threaded host=127.0.0.1 port=8051

刷新localhost页面,你就能看到你的项目主页啦~~
补充一点windwos下nginx操作的命令(来自官方文档)

nginx -s stop quick exit
nginx -s quit graceful quit
nginx -s reload changing configuration, starting a new worker, quitting an old worker gracefully
nginx -s reopen reopening log files

大功告成,开始django之旅,ohye!!!

Python 相关文章推荐
python数据结构之二叉树的建立实例
Apr 29 Python
Python读写配置文件的方法
Jun 03 Python
Scrapy抓取京东商品、豆瓣电影及代码分享
Nov 23 Python
在Python中定义一个常量的方法
Nov 10 Python
Python设计模式之抽象工厂模式原理与用法详解
Jan 15 Python
pytorch实现用Resnet提取特征并保存为txt文件的方法
Aug 20 Python
python协程gevent案例 爬取斗鱼图片过程解析
Aug 27 Python
使用 Python 合并多个格式一致的 Excel 文件(推荐)
Dec 09 Python
Python + Requests + Unittest接口自动化测试实例分析
Dec 12 Python
Pytorch 保存模型生成图片方式
Jan 10 Python
给 TensorFlow 变量进行赋值的方式
Feb 10 Python
python实现同一局域网下传输图片
Mar 20 Python
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
Dec 25 #Python
Linux系统上Nginx+Python的web.py与Django框架环境
Dec 25 #Python
Linux下将Python的Django项目部署到Apache服务器
Dec 24 #Python
在Linux系统上部署Apache+Python+Django+MySQL环境
Dec 24 #Python
在Mac OS上使用mod_wsgi连接Python与Apache服务器
Dec 24 #Python
在Mac OS上搭建Python的开发环境
Dec 24 #Python
详解Python字符串对象的实现
Dec 24 #Python
You might like
手把手教你使用DedeCms的采集的图文教程
2007/03/11 PHP
PHP中用header图片地址 简单隐藏图片源地址
2008/04/09 PHP
phpmailer发送邮件之后,返回收件人是否阅读了邮件的方法
2014/07/19 PHP
ThinkPHP实现转换数据库查询结果数据到对应类型的方法
2017/11/16 PHP
php+redis消息队列实现抢购功能
2018/02/08 PHP
PHP实现将上传图片自动缩放到指定分辨率,并保持清晰度封装类示例
2019/06/17 PHP
利用NodeJS的子进程(child_process)调用系统命令的方法分享
2013/06/05 NodeJs
js控制分页打印、打印分页示例
2014/02/08 Javascript
jQuery插件kinMaxShow扩展效果用法实例
2015/05/04 Javascript
jQuery+JSON实现AJAX二级联动实例分析
2015/12/18 Javascript
javascript中select下拉框的用法总结
2016/01/07 Javascript
总结JavaScript三种数据存储方式之间的区别
2016/05/03 Javascript
js 获取图像缩放后的实际宽高,位置等信息
2017/03/07 Javascript
无限循环轮播图之运动框架(原生JS实现)
2017/10/01 Javascript
vue.js打包之后可能会遇到的坑!
2018/06/03 Javascript
Node.js Koa2使用JWT进行鉴权的方法示例
2018/08/17 Javascript
微信{"errcode":48001,"errmsg":"api unauthorized, hints: [ req_id: 1QoCla0699ns81 ]"}
2018/10/12 Javascript
Layui 解决表格异步调用后台分页的问题
2019/10/26 Javascript
nodejs使用socket5进行代理请求的实现
2020/02/21 NodeJs
[53:23]Secret vs Liquid 2018国际邀请赛淘汰赛BO3 第二场 8.25
2018/08/29 DOTA
[01:23:24]DOTA2-DPC中国联赛 正赛 PSG.LGD vs Elephant BO3 第三场 2月7日
2021/03/11 DOTA
深入解析Python编程中super关键字的用法
2016/06/24 Python
Python制作钉钉加密/解密工具
2016/12/07 Python
python数据预处理之将类别数据转换为数值的方法
2017/07/05 Python
解决python中 f.write写入中文出错的问题
2018/10/31 Python
伦敦剧院及景点门票:Encore Tickets
2018/07/01 全球购物
GWT (Google Web Toolkit)有哪些主要的原件组成?
2015/06/08 面试题
DIY手工制作经营店创业计划书
2014/02/01 职场文书
文明倡议书范文
2014/04/15 职场文书
2014院党委领导班子对照检查材料思想汇报
2014/09/24 职场文书
2014年文艺部工作总结
2014/11/17 职场文书
让人瞬间清醒的句子,句句经典,字字如金
2019/07/08 职场文书
2020年个人安全保证书参考模板
2020/01/08 职场文书
python 用递归实现通用爬虫解析器
2021/04/16 Python
MySQL 视图(View)原理解析
2021/05/19 MySQL
Java实现注册登录跳转
2022/06/16 Java/Android