nginx配置 Nginx和Rails配置 nginx和Mongrel做负载均衡

[quote="鸣哥独白"]
最近在看nginx,本来是要配置PAM MODULE,就是把自己系统的用户权限系统和Linux本身的用户权限系统结合起来。可以从Linux系统添加和管理用户。登录如同windows的域帐号。然后,有了下面的整理。我就简单的陪了一下就通了。
注意的有点,
[color=olive] 要用mongrel的负载均衡,可以不用陪public的目录[/color]
流程概况:
1. 下载 [url="http://dl.iteye.com/topics/download/7b5a54a9-49e6-3689-9796-d026ba95c6d5"]nginx-0.7.64.tar.gz[/url]
2. 配置编译环境
3.
./configure
make
sudo make install

4. vim /usr/local/nginx/conf/nginx.conf
5. /usr/local/nginx/sbin/nginx
如果:

[img]http://dl.iteye.com/upload/attachment/186586/0329c326-223c-3b19-946e-99f1d208712e.jpg[/img]
则参考 [url=http://hlee.iteye.com/admin/blogs/558904]Nginx 重启 nginx 停止 修改配置后生效[/url]killall -HUP nginx
[/quote]

主要参考下文
[url=http://blackanger.blog.51cto.com/140924/40089]配置nginx+mongrel的rails部署环境 [/url]

或许,你对这个也兴趣
[url=http://minstrel.iteye.com/blog/114467]freeBSD下配置nginx+mongrel的rails部署环境[/url]

[color=olive]nginx是什么?[/color]

Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,它已经在该站点运行超过两年半了。Igor 将源代码以类BSD许可证的形式发布。尽管还是测试版,但是,Nginx 已经因为它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名了。

[quote][color=orange]启动与停止nginx的命令[/color]

假设nginx安装在/usr/local/nginx目录里。将如下命令加到~/.bashrc(linux)、 ~/.bash_profile(Mac)

alias sn='sudo /usr/local/nginx/sbin/nginx' 
alias kn='sudo kill `cat /usr/local/nginx/logs/nginx.pid `'
alias rn='kn; sn'


[color=orange]增加应用的host到/etc/hosts,[/color]如:

127.0.0.1 app1
127.0.0.1 app2
[/quote]

这样,在开发时,可以在浏览器中,直接用app1, app2去访问各个应用,再也不用去script/server之类的了。


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;
[color=red]location / {
root /local/Program_Files/aptana/workspace/depot/public;
index index.html index.htm;
}[/color]
location / {
[color=red]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;[/color]
}
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的队列状态

本文出自 “{ :Alex Space => " Ruby Notes " }” 博客,请务必保留此出处http://blackanger.blog.51cto.com/140924/40089
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值