nginx負載均衡配置
1物理環境
192.168.221.129 |
Redhat5.5 |
nginx-0.7.69穩定版 |
192.168.221.130 |
Redhat5.5 |
nginx-0.7.69穩定版 |
2.安裝環境
兩臺機器均選裝nginx
先安裝nginx 依賴包
yum install -y gcc openssl-devel pcre-develzlib-devel
有的說光盤沒有自帶pcre-devel包,但是我的redhat5.5版本帶了這個包
下載nginx包
wget http://nginx.org/download/nginx-0.7.69.tar.gz
tar -zxvf nginx-0.7.69.tar.gz
cd nginx-0.7.69
./configure --with-http_stub_status_module--prefix=/opt/nginx
安裝了http_stub模塊,並講包安裝在了/opt/nginx目錄下,默認好像轉載/usr下面的
make
make install
cd /opt/nginx/conf
剩下關鍵就是配置nginx.conf 這個配置文件了
3.修改配置文件
分別在每臺機器的/var/www/html目錄下放個index.html文件以便安裝好了之後測試測試負載均衡
192.168.221.129 機器要做web服務器,還要做nginx負載均衡的代理服務器,所以,80端口用來做web,81端口用來做代理服務器。
192.168.221.129 /opt/nginx/conf/nginx.conf 文件配置
[root@localhost ~]# cat /opt/nginx/conf/nginx.conf
user nobody 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;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65536;
}
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"';
log_format download '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$http_range""$sent_http_content_range"';
client_max_body_size 20m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
#gzip on;
server {
listen 80;
server_name 192.168.221.129;
index index.html index.htm;
root /var/www/html;
charset gb2312;
access_log logs/host.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name 192.168.221.130;
index index.html index.htm;
root /var/www/html;
charset gb2312;
access_log logs/host.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
upstream 192.168.221.129 {
server 192.168.221.129:80;
server 192.168.221.130:80;
}
server {
listen 81;
server_name 192.168.221.129;
access_log logs/host.access.log main;
location / {
proxy_pass http://192.168.221.129;
# include /opt/nginx/conf/proxy.conf;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 500 502 503 504 /50x.html;
}
}
192.168.221.130 /opt/nginx/conf/nginx.conf 文件配置
[root@localhost ~]# cat/opt/nginx/conf/nginx.conf
user nobody 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;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65536;
}
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"';
log_format download '$remote_addr - $remote_user [$time_local]"$request" '
'$status $body_bytes_sent"$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$http_range""$sent_http_content_range"';
client_max_body_size 20m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 0;
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
#gzip on;
server {
listen 80;
server_name 192.168.221.130;
index index.html index.htm;
root /var/www/html;
charset gb2312;
access_log logs/host.access.log main;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
可以用 /opt/nginx/sbin/nginx -t 來檢查語法錯誤
[root@localhost ~]# /opt/nginx/sbin/nginx-t
the configuration file/opt/nginx/conf/nginx.conf syntax is ok
[warn]: 65536 worker_connections are morethan open file resource limit: 65535
configuration file/opt/nginx/conf/nginx.conf test is successful
/opt/nginx/sbin/nginx 啓動nginx服務
kill 找到進程殺死,就是關閉了,
[root@localhost ~]# ps -ef |grep nginx
root 13325 1 017:20 ? 00:00:00 nginx: masterprocess ../sbin/nginx
nobody 13326 13325 0 17:20 ? 00:00:00 nginx: worker process
root 13649 13607 0 18:30 pts/2 00:00:00 grep nginx
試用了下簡單的功能,還有許多功能沒配
圖示可以看出通過訪問代理192.168.221.129的81好端口,負載均衡到了服務器192.168.221.129:80 和192.168.221.130:80的web端口。