前几天申请了koding.com的VM,用了下感觉 不错啊,第一次用上这么爽的而且是免费的VM, 大家也可以去注册试用哈,我这里给免费打下广告,顺利利用我提供的网址注册(https://koding.com/?r=armingli),还可以给我赠送250M硬盘空间哦。嘻嘻。。
再简单介绍下这个VM吧,先让大家多了解下,系统是 ubuntu13.10, 默认装了apache, 不喜欢用可以装nginx,昨天刚装了,折腾了下午,把一个简单的反向代理搞定了。还可以装MySQL, pip(对搞python的可是福利啊),没钱买空间的可以来自己折腾啦,我装的是 tornado, 挺好用,速度还可以,对了此VM还可以ssh的,只不过速度有点小慢,不是很稳定,不过,网页上的工具已经很好用了。亲自去验证下吧,https://koding.com/?r=armingli.
下面进入正题,因为tornado跑的应用端口是8000的,访问的时候不方便,就想着把端口给省去,在某群里问下,果断都推荐上nginx, 就去装了,第一次不会用,上网搜现在的配置,拿过来改,折腾了半小时没有搞定,就去打球了。
晚上回来,找师傅指点了几处位置,搞定,记下主要位置的意思,想必大家看看文档都会知道,我这懒人。。。。
<!-- lang: shell -->
#this points a system user
user root; #1 这里的user指系统的 用户,用哪个用户来执行
worker_processes 1;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
use epoll;
}
http {
# Enumerate all the Tornado servers here
#2 这里要设置要代理的应用的端口
upstream frontends {
server 127.0.0.1:8888;
}
include /etc/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_static on;
gzip_min_length 1000;
gzip_proxied any;
#3 设置文件类型
gzip_types text/plain text/css text/xml
application/x-javascript application/xml
application/atom+xml text/javascript;
# 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; #4 监听端口号,当请求过来时,如果不填默认是80,如果是其他端口,没这个必要了就
server_name armingli.kd.io;#5 此处是服务器的域名,通过该域名访问你的应用
# Allow file uploads
client_max_body_size 50M;
location static/ {
#6 此处写你的应用位置,绝对路径
root /home/app/path/;
if ($query_string) {
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;
# point to the upstream
proxy_pass http://frontends; #7 此处就是将上面配置的upstream#2, frontends 只是个名字,可以随便起
}
}
}
完成了上面的配置,要把它放在nginx的目录中,命名就是nginx.conf, 此文件应该放在/etc/nginx
目录下的,该目录下有个默认的文件,我的做法是把 /etc/nginx
下的nginx.conf 备份了下,然后把上面的配置文件放在了自己的一个目录下,然后设置了硬链接,这样做不是很好,最好还是放/etc/nginx
下吧。
配置放好了,现在还不 能急着启动,koding上的VM装着apache, 80端口被占用着,要先关闭apache, apache2ctl -k stop
关之, 然后启动nginx就可以了,sudo nginx
。关闭命令sudo nginx -s stop
。
小记完成,nginx有必要好好研究下的。