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标准库os.path包、glob包使用实例
Nov 25 Python
Python正则简单实例分析
Mar 21 Python
Python爬取附近餐馆信息代码示例
Dec 09 Python
Python实现将doc转化pdf格式文档的方法
Jan 19 Python
详解如何在python中读写和存储matlab的数据文件(*.mat)
Feb 24 Python
Python实现的NN神经网络算法完整示例
Jun 19 Python
python 制作自定义包并安装到系统目录的方法
Oct 27 Python
pycharm 在windows上编辑代码用linux执行配置的方法
Oct 27 Python
Python爬虫文件下载图文教程
Dec 23 Python
python和node.js生成当前时间戳的示例
Sep 29 Python
如何编写python的daemon程序
Jan 07 Python
教你怎么用python selenium实现自动化测试
May 27 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
PHP中copy on write写时复制机制介绍
2014/05/13 PHP
php自定义加密与解密程序实例
2014/12/31 PHP
PHP的命令行命令使用指南
2015/08/18 PHP
详细解读PHP的Yii框架中登陆功能的实现
2015/08/21 PHP
Zend Framework框架Smarty扩展实现方法
2016/03/22 PHP
PHP简单获取及判断提交来源的方法
2016/04/22 PHP
PHP使用函数用法详解
2018/09/30 PHP
ThinkPHP5&5.1框架关联模型分页操作示例
2019/08/03 PHP
Laravel定时任务的每秒执行代码
2019/10/22 PHP
prototype 的说明 js类
2006/09/07 Javascript
浅谈JSON中stringify 函数、toJosn函数和parse函数
2015/01/26 Javascript
JavaScript中判断两个字符串是否相等的方法
2015/07/07 Javascript
微信企业号开发之微信考勤百度地图定位
2015/09/11 Javascript
举例说明如何为JavaScript的方法参数设置默认值
2015/11/17 Javascript
JavaScript中如何使用cookie实现记住密码功能及cookie相关函数介绍
2016/11/10 Javascript
jQuery实现ToolTip元素定位显示功能示例
2016/11/23 Javascript
pace.js和NProgress.js两个加载进度插件的一点小总结
2018/01/31 Javascript
Element input树型下拉框的实现代码
2018/12/21 Javascript
[05:39]2014DOTA2西雅图国际邀请赛 淘汰赛7月14日TOPPLAY
2014/07/14 DOTA
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
2018/02/03 Python
Python编写一个优美的下载器
2018/04/15 Python
python的mysql数据库建立表与插入数据操作示例
2019/09/30 Python
css3.0 图形构成实例练习一
2013/03/19 HTML / CSS
CSS3圆角边框和边界图片效果实例
2016/07/01 HTML / CSS
HTML5通用接口详解
2016/06/12 HTML / CSS
意大利折扣和优惠券网站:Groupalia
2019/10/09 全球购物
描述RIP和OSPF区别以及特点
2015/01/17 面试题
仓库门卫岗位职责
2013/12/22 职场文书
相亲活动方案
2014/08/26 职场文书
服务行业演讲稿
2014/09/02 职场文书
私营公司诉讼代理委托书范本
2014/09/13 职场文书
小学语文教师年度考核个人总结
2015/02/05 职场文书
综合素质自我评价评语
2015/03/06 职场文书
南京大屠杀观后感
2015/06/02 职场文书
退伍军人感言
2015/08/01 职场文书
ant design charts 获取后端接口数据展示
2022/05/25 Javascript