两台linux服务器负载均衡代码实现,nginx实现负载均衡,nginx负载均衡确保两台服务器数据保...
nginx實現負載均衡,nginx負載均衡確保兩臺服務器數據保
一、準備篇:
Nginx 負載服務器:
Centos 6.2
IP:192.168.1.93
WEB服務器:
Web1:192.168.1.155
Web2:192.168.1.205
實現目的:用戶訪問192.168.1.93時,通過Nginx負載到WEB1和WEB2
=============================================
二、配置Nginx負載均衡服務器
配置好WEB1和WEB2的環境以及上傳好程序。確保兩臺服務器數據保持一致!
1.1關閉Selinux
#vi /etc/selinux/config
#SELINUX=enforcing 修改為disabled
#:wq
#shutdown -r now
2.2 文件存放:
軟件源代碼存放位置:/usr/local/src
遠嗎編譯安裝位置:/usr/local/軟件名字
2.3 下載軟件包
下載 nginx(目前穩定版)
http://nginx.org/download/nginx-1.0.15.tar.gz
下載 pcre
ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.30.tar.gz
下載 ngx_cache_purge(清除指定 URL 緩存,方便以后擴展配置 nginx 緩存服務器)
http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz
2.4 安裝編譯工具庫
#yum install -y make apr* autoconf automake curl curl-devel gcc gcc-c++
zlib-devel openssl openssl-devel pcre-devel gd kernel keyutils patch perl
kernel-headers compat* cpp glibc libgomp libstdc++-devel
keyutils-libs-devel libsepol-devel libselinux-develkrb5-devel
zlib-devel libXpm* freetype libjpeg* libpng* php-common php-gd ncurses*
libtool* libxml2 libxml2-devel patch
======================================
三、安裝篇
#groupadd www #添加 www 組
#useradd -g www www -s /bin/false #創建 nginx 運行賬戶 www 并加入到 www,不允許 www 用戶直接登錄系統 cd /usr/local/src #進入安裝目錄
#tar zxvf ngx_cache_purge-1.5.tar.gz #解壓
#tar zxvf nginx-1.0.15.tar.gz #解壓
#tar zxvf pcre-8.30.tar.gz #解壓
#cd nginx-1.0.15
#./configure –prefix=/usr/local/nginx –without-http_memcached_module
–user=www –user=www –group=www –with-http_stub_status_module
–with-openssl=/usr/
–with-pcre=/usr/local/src/pcre-8.30 –add-module=../ngx_cache_purge-1.5 #配置
注意:–with-pcre=/usr/local/src/pcre-8.30 指向的是源碼包解壓的路徑,而
不是安裝的路徑,否則會報錯
#make #編譯
#make install #安裝
#/usr/local/nginx/sbin/nginx #啟動 Nginx
設置 nginx 開啟啟動
#echo “/usr/local/nginx/sbin/nginx” >> /etc/rc.local
=======================================================
四、配置篇
配置Nginx
#備份 nginx 配置文件
#cp/usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak
4.1 設置 nginx 運行賬號
#vi /usr/local/nginx/conf/nginx.conf #編輯
找到 user nobody;修改為user www www; #在第一行
4.2 禁止 nginx 空主機頭
#vi /usr/local/nginx/conf/nginx.conf #編輯找到 server,在上面一行添加如下內容:
##############################
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~/.ht {
deny all;
}
}
##############################
#/usr/local/nginx/sbin/nginx -s reload #平滑重啟nginx服務
這樣設置好以后,空頭主機訪問會直接跳轉到nginx404界面
4.3 添加nginx虛擬主機包含文件
#cd /usr/local/nginx/conf/ #進入 nginx 安裝目錄
#mkdir vhost #建立虛擬目錄
#vi /usr/local/nginx/conf/nginx.conf #編輯
找到上一步添加的代碼,在最后添加如下內容:include vhost/*.conf;例如:
##############################
server {
listen 80 default;
server_name _;
location / {
root html;
return 404;
}
location ~/.ht {
deny all;
}
}
include vhost/*.conf;
##############################
4.4 添加 Web 服務器列表文件
#cd /usr/local/nginx/conf/ #進入目錄
#touch vhost.conf #建立文件
#vi /usr/local/nginx/conf/nginx.conf #編輯
找到上一步添加的代碼,在下面添加一行
include vhost.conf;
4.5 設置 nginx 全局參數
vi /usr/local/nginx/conf/nginx.conf #編輯
worker_processes 2; # 工作進程數,為 CPU 的核心數或者兩倍
events
{
use epoll; #增加
worker_connections 65535; #修改為 65535,最大連接數。
}
#############以下代碼在 http { 部分增加與修改##############
server_names_hash_bucket_size 128; #增加
client_header_buffer_size 32k; #增加
large_client_header_buffers 4 32k; #增加
client_max_body_size 300m; #增加
tcp_nopush on; #修改為 on
keepalive_timeout 60; #修改為 60
tcp_nodelay on; #增加
server_tokens off; #增加,不顯示 nginx 版本信息
gzip on; #修改為 on
gzip_min_length 1k; #增加
gzip_buffers 4 16k; #增加
gzip_http_version 1.1; #增加
gzip_comp_level 2; #增加
gzip_types text/plain application/x-javascript text/css
application/xml; #增加
gzip_vary on; #增加
====================================================
4.6 設置 Web 服務器列表
#cd /usr/local/nginx/conf/ #進入目錄
#vi vhost.conf #編輯,添加以下代碼
upstream osyunweihost {
server 192.168.1.155:80 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.1.205:80 weight=1 max_fails=2 fail_timeout=30s;
ip_hash;
}
4.7 新建虛擬主機配置文件
#cd /usr/local/nginx/conf/vhost #進入虛擬主機目錄
#touch vhost.conf #建立虛擬主機配置文件
#vi vhost.conf #編輯
server
{
listen 80;
server_name www.a.com bbs.a.com sns.a.com;
location /
{
proxy_next_upstream http_502 http_504 error timeout
invalid_header;
proxy_pass http://ahost;
#proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
log_format access ‘$remote_addr – $remote_user [$time_local]
$request ‘
‘”$status” $body_bytes_sent
“$http_referer” ‘
‘”$http_user_agent”
“$http_x_forwarded_for”‘;
access_log /usr/local/nginx/logs/access.log access;
location /NginxStatus {
stub_status on;
access_log on;
auth_basic “NginxStatus”;
#auth_basic_user_file pwd;
}
}
:wq! #保存配置
#/usr/local/nginx/sbin/nginx -s relooad #平滑重啟 nginx
==================================================
五、測試篇
域名:
www.a.com
bbs.a.com
sns.a.com
分別解析到 192.168.1.93
客戶訪問這三個站點的時候,Nginx 根據客戶訪問的ip_hash 值,負載均衡到Web1和 Web2 服務器上。
至此Nginx負載均衡配置完成。
總結
以上是生活随笔為你收集整理的两台linux服务器负载均衡代码实现,nginx实现负载均衡,nginx负载均衡确保两台服务器数据保...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为手机怎么设置指纹解锁
- 下一篇: linux 定时器和sleep,linu