配置nginx+mongrel的rails部署環境

 

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
   局域網內別的機器:[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的隊列狀態

本文出自 “{ :

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章