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端口。