上篇已经安装了Tornado 运行环境了,现在要装上Nginx 作反向代理和均衡负载。
安装Nginx
下载当前Ngina 稳定版 http://nginx.org/download/nginx-1.4.3.tar.gz
1
2
3
cd $OPENSHIFT_DATA_DIR
wget http://nginx.org/download/nginx-1.4.3.tar.gz
tar zxf nginx-1.4.3.tar.gz
在安装前要下载一个PCRE lib
1
2
3
cd $OPENSHIFT_DATA_DIR
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.31.tar.bz2
tar jxf pcre-8.31.tar.bz2
1
2
cd nginx-1.4.3
./configure --prefix=$OPENSHIFT_DATA_DIR --with-pcre=$OPENSHIFT_DATA_DIR/pcre-8.31
会看到类似下面的内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Configuration summary
+ using PCRE library: /tmp//pcre-8.31
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library
nginx path prefix: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime/"
nginx binary file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//sbin/nginx"
nginx configuration prefix: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//conf"
nginx configuration file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//conf/nginx.conf"
nginx pid file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//logs/nginx.pid"
nginx error log file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//logs/error.log"
nginx http access log file: "/var/lib/stickshift/c45cdc9a27944dc5b1cd7cb9e5c9f8c7/mysite/runtime//logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
安装
1
make && make install
配置Nginx
这个比较麻烦,看了高手使用模板的方法,自己也试了一下,因为我要替换的不仅仅是端口和ip,还有静态文件路径,在有/时sed 会抛出一个错误,在官方论坛看了半天解决不了,只好放弃了,老老实实的填上自己的数据。下面是我的一份标准的开多个实例的配置。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#user nobody
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
charset utf-8;
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 265;
#tcp_nodelay on;
client_max_body_size 50M;
client_body_buffer_size 512k;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
upstream frontends {
server $OPENSHIFT_DIY_IP:15001;
server $OPENSHIFT_DIY_IP:15002;
}
server {
listen 8080;
server_name localhost;
location ^~ /static/ {
root $OPENSHIFT_REPO_DIR;
expires max;
}
location ^~ /upload/ {
root $OPENSHIFT_DATA_DIR;
expires max;
}
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;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
这里我在后端启动了两个实例,对应的两个端口 15001 和 15002,用Nginx 来平衡负载。
看看top 图:openshift-tornado-nginx-1.gif
在Openshift 上的demo Openshift tornado
参考文档 https://www.openshift.com/blogs/lightweight-http-serving-using-nginx-on-openshift