网上有好多的nginx+mongrel的部署文章,但是写的都不太清楚,今天自己动手实验了一下,在此记录:
引用: nginx 接受公网上的http请求,然后nginx通过反向代理用http协议再把这些请求转发给 mongrel,mongrel上启动的Rails对请求进行处理完后再把响应逆向返回,如果如果在高负载 的情况下还可以通过mongrel_cluster启动的mongrel集群,并且在nginx上启动负载均衡以成倍 的提高处理请求的能力。
1.先 点这里 下载nginx并安装:
我下载的是development版本。我的安装路径:/local/Program_Files/nginx/nginx
把/local/Program_Files/nginx/nginx/sbin设置到环境变量中。
2.安装mongrel:
gem install mongrel
gem install mongrel_cluster
3. 配置nginx:
我安装nginx的路径是:/local/Programe_Files/nginx
打开:/local/Program_Files/nginx/nginx/conf/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;
upstream mongrel { //因为我要通过别的机子来通过nginx来请求mongrel,所以
server 192.168.1.10:8000; // 不是127.0.0.1了
server 192.168.1.10:8001;
}
server {
listen 80;
server_name 192.168.1.10;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /local/Program_Files/aptana/workspace/depot/public;
index index.html index.htm;
}
location / {
proxy_pass [url]http://mongrel;[/url]
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov)$ {
root /local/Program_Files/aptana/workspace/depot/public;
}
#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;
}
} }
------------------------------------------------------------------------------------------------
4.配置mongrel:
执行下列命令,会在/local/Program_Files/aptana/workspace/depot/config/中生成mongrel_cluster.yml文件 :
# mongrel_rails cluster::configure -e development -p 8000 -N 3 \
-c /local/Program_Files/aptana/workspace/depot -a 127.0.0.1
注:-e 指定environment。-p 端口 。 -N mongrel服务进程数
文件内容为:
---
cwd: /local/Program_Files/aptana/workspace/depot
log_file: log/mongrel.log
port: "8000"
environment: development //此为开发模式,改为production即为生产模式
address: 127.0.0.1
pid_file: tmp/pids/mongrel.pid
servers: 3
5.命令 mongrel_rails cluster::start启动mongrel集群
6.到/local/Program_Files/nginx/nginx/sbin下启动nginx
7.本地机: [url]http://localhost[/url]
局域网内别的机器:[url]http://192.168.1.10[/url]
尝试 [url]http://192.168.1.10/admin[/url],depot的登录界面会出现,大功告成了!!!
由此可发现nginx+mongrel的配置是多么简便。。。
-------------------------------------附带nginx配置说明----------------------------
#运行用户
user nobody nobody;
#启动进程
worker_processes 2;
#全局错误日志及PID文件
error_log logs/error.log notice;
pid logs/nginx.pid;
#工作模式及连接数上限
events {
use epoll;
worker_connections 1024;
}
#设定http服务器,利用它的反向代理功能提供负载均衡支持
http {
#设定mime类型
include conf/mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$gzip_ratio"';
log_format download '$remote_addr - $remote_user [$time_local] '
'"$request" $status $bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_range" "$sent_http_content_range"';
#设定请求缓冲
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
#开启gzip模块
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_types text/plain;
output_buffers 1 32k;
postpone_output 1460;
#设定access log
access_log logs/access.log main;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
#设定负载均衡的服务器列表
upstream mysvr {
#weigth参数表示权值,权值越高被分配到的几率越大
#本机上的Squid开启3128端口
server 192.168.8.1:3128 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
#设定虚拟主机
server {
listen 80;
server_name 192.168.8.1 [url]www.yejr.com;[/url]
charset gb2312;
#设定本虚拟主机的访问日志
access_log logs/www.yejr.com.access.log main;
#如果访问 /img/*, /js/*, /css/* 资源,则直接取本地文件,不通过squid
#如果这些文件较多,不推荐这种方式,因为通过squid的缓存效果更好
location ~ ^/(img|js|css)/ {
root /data3/Html;
expires 24h;
}
#对 "/" 启用负载均衡
location / {
proxy_pass [url]http://mysvr;[/url]
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
}
}
}
备注:conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可,内容大致如下:
3.) 查看 Nginx 运行状态
输入地址 [url]http://192.168.8.1/NginxStatus/[/url],输入验证帐号密码,即可看到类似如下内容:
Active connections: 328
server accepts handled requests
9309 8982 28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示目前活跃的连接数
第三行的第三个数字表示Nginx运行到当前时间接受到的总请求数,如果快达到了上限,就需要加大上限值了。
第四行是Nginx的队列状态