Nginx高可用反向代理搭建
Nginx簡介
Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 爲俄羅斯訪問量第二的 Rambler.ru 站點開發的,第一個公開版本0.1.0發佈於2004年10月4日。其將源代碼以類BSD許可證的形式發佈,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。
Nginx 可以在大多數 Unix like OS 上編譯運行,並有 Windows 移植版。 Nginx 的1.4.0穩定版已經於2013年4月24日發佈,一般情況下,對於新建站點,建議使用最新穩定版作爲生產版本,已有站點的升級急迫性不高。Nginx 的源代碼使用 2-clauseBSD-like license。
Nginx 是一個很強大的高性能Web和反向代理服務器,它具有很多非常優越的特性:
在高連接併發的情況下,Nginx是Apache服務器不錯的替代品:Nginx在美國是做虛擬主機生意的老闆們經常選擇的軟件平臺之一。能夠支持高達 50,000 個併發連接數的響應,感謝Nginx爲我們選擇了 epoll andkqueue作爲開發模型。
Nginx環境搭建準備
主機名 Os IP 用途 Vip
NginxA Rhel6.2 192.168.2.41 Nginx主節點A 192.168.2.40
NginxB Rhel6.2 192.168.2.41 Nginx節點B 192.168.2.40
ClusterSer WinSer2008 192.168.2.21 模擬後臺集羣server 無
Nginx安裝部署
# yum install gcc gcc-c++ autoconf automakezlib zlib-devel openssl openssl-devel pcre-devel
到官網下載Nginx
上傳到/opt中
# tar zxvf nginx-1.6.0.tar.gz
# cd nginx-1.6.0
#./configure –-prefix=/u01/nginx1.6.0 --with-http_ssl_module--with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module
# make
# make install
Nginx基本配置
Nginx大致可以理解分爲四個板塊:全局模塊,事件模塊,http模塊,以及server模塊
切換到/u01/nginx1.6.0/conf下面,編輯nginx配置文件nginx.conf。
#全局塊
worker_processes 1; #配置允許生產的工作進程數
error_log logs/error.log notice; #配置錯誤日誌存放路徑及記錄級別
pid logs/nginx.pid; #配置nginx進程pid存放路徑
#事件塊
events {
worker_connections 65535; #配置最大連接數
use epoll; #使用epoll事件驅動模型
}
HTTP模塊
http {
include mime.types; #引用MIME-Type
default_type application/octet-stream; #配置用於處理前端請求的MIME類型
#日誌存放路徑及日誌格式
access_log logs/access.log combined;
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;
send_timeout 120s #配置客戶連接等待響應時間
sendfile on; #支持sendfile方式傳輸文件
keepalive_timeout 65; #配置連接超時時間
clinet_hearder_buffer_size4k; #配置請求頭部數據大小
#配置壓縮
gzip on;
gzip_min_length1024
gzip_buffers 324k
gzip_comp_level 2
gzip_typestext/plain application/x-javascript text/css application/xml;
gzip_vary on;
gunzip_staticon;
SERVER塊
server {
listen 80;
server_name cluster;
charset koi8-r;
access_log logs/cluster.access.log main;
location / {
root html;
index index.html index.htm;
}
error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
}
Nginx反向代理配置
在配置文件中如下修改,在server模塊前增加upstream轉發,並且修改location配置
upstream cluster {
ip hash;
server 192.168.2.21:80;
}
SERVER塊
server {
listen 80;
server_name cluster;
charset koi8-r;
access_log logs/cluster.access.log main;
location / {
proxy_pass http://cluster;
proxy_connect_timeout 120s;
proxy_read_timeout 120s;
proxy_set_header host $host;
proxy_set_hearder x-Real-IP $remote_addr;
proxy_set_header x-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
#redirect server error pages to the static page /50x.html
}
Keepalive配合使用
確保keepalive需要環境包都有安裝:
yum install gcc gcc-c++ openssl-developenssl kernel-devel kernel popt-devel,libnl-devel
下載安裝keepalive
# tar keepalived-1.1.20.tar.gz
# ./configure –prefix=/u01/keepalive
# make
# make install
# cp /u01/keepalived-1.2.13/sbin/keepalived/usr/sbin
# cp/u01/keepalived-1.2.13/etc/sysconfig/keepalived /etc/sysconfig
# cp/u01/keepalived-1.2.13/etc/rc.d/init.d/keepalived /etc/init.d
# mkdir /etc/keepalived
# cp/u01/keepalived-1.2.13/etc/keepalived/keepalived.conf /etc/keepalived
A節點(192.168.2.41)配置文件如下:
global_defs {
notification_email { }
notification_email_from
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance nginxA{
state MASTER
interface eth0
track_interface {
eth1
}
virtual_router_id 1
priority 200
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.2.40/24
192.168.2.41/24
192.168.2.42/24
}
}
B節點(192.168.2.42)配置文件如下:
global_defs {
notification_email { }
notification_email_from
smtp_server smtp.qq.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance nginxB{
state BACKUP
interface eth0
track_interface {
eth1
}
virtual_router_id 1
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123
}
virtual_ipaddress {
192.168.2.40/24
192.168.2.41/24
192.168.2.42/24
}
}
#根據自己的網絡環境,修改下配置文件
service keepalived start
排查看日誌,/var/log/message
注意:priority一般master大於backup的值!