nginx下搭建fastcgi的开发环境
在上一章最简单理解CGI,FastCGI,WSGI 我们将fastcgi规范类比HTTP。下面我们通过一个案例更加明白fastcgi
我们使用的是 nginx作为前端 代理,我们包装了gevent_fastcgi FastCGIServer 作为我们的FastCGI Server。
nginx配置:
#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;
}
# 所有以.py结尾的都发送到localhost:9000进行处理
location ~\.py{
fastcgi_pass localhost:9000;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
} #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;
#}
} # 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;
# }
#} }
下面在贴出 FasctCGI Server端的代码:
from gevent_fastcgi.server import FastCGIServer
from gevent_fastcgi.wsgi import WSGIRequestHandler def wsgi_app(environ, start_response):
print 'Request from ', environ['HTTP_USER_AGENT']
start_response('200 OK', [('Content-type', 'text/html')])
yield '<html><head><title>test fastcgi</title></head><body>'
yield '<table border="2">'
for k, v in environ.items():
yield '<tr><td>%s</td><td>%s</td></tr>'%(k, v)
yield '</table>'
yield '</body></html>' request_handler = WSGIRequestHandler(wsgi_app)
server = FastCGIServer(('127.0.0.1', 9000), request_handler, num_workers=4)
server.serve_forever()
使用了生成器的方式,性能比较高。
上面的代码该一下(改成WSGIServer)马上就能切换成 WSGI Server了,我们按照WSGI的规范进行扩展一下,就是一个支持WSGI Web框架了。现在应该很清楚CGI,FastCGI,WSGI技术了吧,只要把这些技术弄明白了,我们就能随意组合HTTP Server 和 运用服务器(FastCGI Server,WSGI Server等)。
分别运行nginx和运行上面的脚本程序.
输入任何以.py结尾的请求,将会输出一张 FastCGI的环境变量的表。例如: localhost/script/test.py
下面汇总一下CGI,FastCGI,WSGI相关的技术文章
CGI 粗谈CGI
FastCGI http://www.fastcgi.com/drupal/node/6?q=node/15
WSGI http://wsgi.readthedocs.org/en/latest/index.html
以及他们之间的关系 Python Web初学解惑之 WSGI、flup、fastcgi、web.py的关系
其实看源码和这些协议的白皮书是最好的,如果还有不明白的可以留言。关于这CGI,FastCGI,WSGI以及相关知识,我研究了一个星期, 都是直接看这些 协议 的标准说明书,也研究了很多实现了这些协议 的服务器,当然基本都是Python源码。所以大家有什么困惑可以互相交流一下。
nginx下搭建fastcgi的开发环境的更多相关文章
- Windows下搭建Spark+Hadoop开发环境
Windows下搭建Spark+Hadoop开发环境需要一些工具支持. 只需要确保您的电脑已装好Java环境,那么就可以开始了. 一. 准备工作 1. 下载Hadoop2.7.1版本(写Spark和H ...
- 如何在Ubuntu下搭建Android NDK开发环境
1 搭建Android SDK开发环境 参考在在Ubuntu下搭建Android SDK开发环境(图文)首先在Ubuntu下搭建Android SDK开发环境. 2 下载NDK开发包 打开官网: ht ...
- Windows下搭建objective C开发环境
摘自:http://blog.csdn.net/zhanghefu/article/details/18320827 最近打算针对iPhone.iPod touch和iPad开发一些应用,所以,需要开 ...
- LINUX下搭建JAVA的开发环境
LINUX下搭建JAVA的开发环境 (2009-07-13 10:04:13) 下面就将Linux下JAVA开发环境的搭建详细道来: 1.Linux下JDK的安装 至于下载JDK的二进制可执行 ...
- Win7下搭建Go语言开发环境
Win7下搭建Go语言开发环境 1 下载适合window版本的Go安装包,下载地址http://code.google.com/p/go/downloads/list 2 下载适合window本本的L ...
- Windows下搭建Android NDK开发环境及命令行编译
首先说明本文内的相关安装操作参考<Pro Android C++ with the NDK>一书. 安装 Windows搭建Android NDK开发环境需要安装如下部分(同时需要配置对应 ...
- 【RN - 基础】之Windows下搭建React Native开发环境
前言 React Native由Facebook公司于2015年F8大会上开源,其主张“Learn once, write everywhere”.React Native的核心设计理念是:既拥有Na ...
- Win10系统下搭建Go lang开发环境更换国内源并且体验宇宙最快框架Iris
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_156 最近有同学开始尝试接触Go lang,拥抱新技术永远都会是一个好习惯,之前的一篇文章介绍了如何在Mac os系统下配置Go ...
- ubuntu15.10下搭建cordova+ionic开发环境
安装jdk 在命令下输入java如果没有安装会提示该命令包含于openjdk软件包 sudo apt-get install openjdk然后按下tab会列出openjdk开头的软件包 我这里就选择 ...
随机推荐
- eclipse运行项目特别慢,出现Java heap space溢出
在eclipse中可用为JVM设置参数:Window-->Preferences-->Java-->Installed JREs然后选中你安装的jre-->Edit--> ...
- 通过mybatis读取数据库数据并提供rest接口访问
1 mysql 创建数据库脚本 -- phpMyAdmin SQL Dump -- version 4.2.11 -- http://www.phpmyadmin.net -- -- Host: lo ...
- FreeMarker 自己定义指令(三)
1. 模板文件 test04.ftl foo <@customUpper> bar f <#-- 这里同意使用全部的 FTL --> <#list ["red& ...
- 深入N皇后问题的两个最高效算法的详解 分类: C/C++ 2014-11-08 17:22 117人阅读 评论(0) 收藏
N皇后问题是一个经典的问题,在一个N*N的棋盘上放置N个皇后,每行一个并使其不能互相攻击(同一行.同一列.同一斜线上的皇后都会自动攻击). 一. 求解N皇后问题是算法中回溯法应用的一个经典案例 回溯算 ...
- Android 自定义Spinner和其下拉窗口
: 自定义Spinner其实包括两个部分: 第一部分是用来打开下拉列表的按钮,如图,这个绿色背景直接设置Spinner的背景就行,素材文件如下: 里面的文字需要注意下,Spinner控件没有直接修改文 ...
- Java实现平衡二叉树(AVLTree)的构建
近期在学习数据结构上关于平衡二叉树的知识,看了严老师的思路,感觉用java写出递归的构建方式有点困难,由于当中的递归须要把引用传进去,所以感觉是要实现起来比較麻烦,所以就首先想到使用非递归的方式来实现 ...
- java获取计算机硬件参数
public class HardWareUtils { /** * * 获取主板序列号 * * * * @return */ public static String g ...
- 【转】IOS AutoLayout详解(三)用代码实现(附Demo下载)
转载自:blog.csdn.net/hello_hwc IOS SDK详解 前言: 在开发的过程中,有时候创建View没办法通过Storyboard来进行,又需要AutoLayout,这时候用代码创建 ...
- Objective-C总Runtime的那点事儿(一)消息机制【转】
RunTime简称运行时.就是系统在运行的时候的一些机制,其中最主要的是消息机制.对于C语言,函数的调用在编译的时候会决定调用哪个函数( C语言的函数调用请看这里 ).编译完成之后直接顺序执行,无任何 ...
- basicAnimation移动图形
目的:采用CABasicAnimation 点击屏幕上的点来是实现图像的位置移动 并且位置能够不反弹 难点:1 通过动画的KeyPath找到layer的属性 2 通过NSValue将点包装成对象 ...