Tornado的确很给力,知乎、Facebook一直在用。不过Tornado也有自己的局限性,比如它就没有实现完整的HTTP协议,甚至一些REST方法都不支持。不过这也难为它了,本来就是一个Web Framework顺便兼职干一点Web Server的事情而已,有就不错了。好在Tornado现在有了好伙伴Nginx,像HTTPS,负载均衡,静态文件这种破事都可以交给Nginx去处理了。
下载
从源代码编译安装Nginx需要处理一些依赖
Nginx官网
PCRE官网
ZLib官网
下载好之后解压至同一根目录下。
编译
$ ./configure
--sbin-path=/usr/local/nginx/nginx
--conf-path=/usr/local/nginx/nginx.conf
--pid-path=/usr/local/nginx/nginx.pid
--with-http_ssl_module
--with-pcre=../pcre-8.38
--with-zlib=../zlib-1.2.8
--with-openssl=../openssl-1.0.2g
#上面只是为了好看....
$ ./configure --sbin-path=/usr/local/nginx/nginx --conf-path=/usr/local/nginx/nginx.conf --pid-path=/usr/local/nginx/nginx.pid --with-http_ssl_module --with-pcre=../pcre-8.38 --with-zlib=../zlib-1.2.8 --with-openssl=../openssl-1.0.2g
$ make -j8 && sudo make install
需要注意的是,Mac下和Linux下默认生成的目录是不一样的。
nginx运行一般要求root权限。
nginx命令行参数相当简单,因为功夫全在配置文件里了……。
#启动Nginx实例
$ nginx
#使用指定的配置文件启动nginx
$ nginx -c <conf_file>
#向Nginx发出信号
$ nginx -s [stop| quit| reopen| reload]
Mac下的Nginx配置
user nobody nobody;
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use kqueue;
}
http {
# Enumerate all the Tornado servers here
upstream frontends {
server 127.0.0.1:8888;
}
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
keepalive_timeout 65;
proxy_read_timeout 200;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
server {
listen 80;
# Allow file uploads
client_max_body_size 50M;
location ^~ /static/ {
root /Volumes/Data/vserver;
if ($query_string) {
expires max;
}
}
location ^~ /upload/ {
root /Volumes/Data/vserver;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
access_log off;
rewrite (.*) /static/other/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/other/robots.txt;
}
# Ali heartbeat dectection
location = /status.taobao {
access_log off;
rewrite (.*) /static/other/status.taobao;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends;
}
}
}
Linux下Nginx的配置
# user nobody nobody;
worker_processes 1;
#error_log /var/log/nginx/error.log;
#pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# Enumerate all the Tornado servers here
upstream frontends {
server 127.0.0.1:8888;
}
include /usr/local/nginx/mime.types;
default_type application/octet-stream;
#access_log /var/log/nginx/access.log;
keepalive_timeout 65;
proxy_read_timeout 200;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
gzip on;
gzip_min_length 1000;
gzip_proxied any;
# Only retry if there was a communication error, not a timeout
# on the Tornado server (to avoid propagating "queries of death"
# to all frontends)
proxy_next_upstream error;
server {
listen 80;
# Allow file uploads
client_max_body_size 50M;
location ^~ /static/ {
root /home/vonng/vserver;
if ($query_string) {
expires max;
}
}
location ^~ /upload/ {
root /home/vonng/vserver;
if ($query_string) {
expires max;
}
}
location = /favicon.ico {
access_log off;
rewrite (.*) /static/other/favicon.ico;
}
location = /robots.txt {
rewrite (.*) /static/other/robots.txt;
}
# Ali heartbeat dectection
location = /status.taobao {
access_log off;
rewrite (.*) /static/other/status.taobao;
}
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://frontends;
}
}
}