本次实验为Nginx作这反向代理服务器.将客户端请求转发到后端的web服务器上(apache).nginx和DNS安装在一台服务器上.
一.实现环境
说明;
nginx+DNS服务器模拟公网地址 两个域名同时指向这个IP,通过nginx代理将实际请求转发到后端真实服务器上
www.haidongqing.com 192.168.137.106
mail.haidongqing.com 192.168.137.106
二.所需软件及版本
bind-9.3.6
nginx-1.4.5.tar.gz //wget http://nginx.org/download/nginx-1.4.5.tar.gz
三.安装配置DNS
yum -y install bind
yum -y install caching-nameserver
检查安装后的目录
ls /var/named
手动创建named置文件
vim /etc/named.conf
手动编辑正反区域配置文件
vim /var/named/haidongqing.com.zone 192.168.137.zone
测试配置文件语法和区域配置文件语法
named-checkconf
named-checkzone "haidongqing.com" haidongqing.con.zone
无报错即可以重启DNS
service named restart
netstat -ntlp //检查端口及进程名
测试DNS正解
dig -t A mail.haidongqing.com
三.安装配置Nginx
yum -y install pcre-devel
tar -zxvf nginx-1.4.5.tar.gz
cd nginx-1.4.5
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--user=nginx \
--group=nginx \
--with-pcre
make && make install
安装好之后无需改动配置文件可以直接启动nginx
这里写一个脚本方便起动nginx .
#!/bin/sh
function_start_nginx()
{
printf "starting nginx...\n"
/usr/local/nginx/sbin/nginx 2>&1
}
function_stop_nginx()
{
printf "stopping nginx...\n"
kill -9 `ps -ef |grep nginx | grep -v grep | awk '{print $2}'` 2>&1
}
function_restart_nginx()
{
printf "restart nginx...\n"
kill -9 `ps -ef |grep nginx | grep -v grep | awk '{print $2}'` 2>&1
/usr/local/nginx/sbin/nginx 2>&1
}
function_hupstart_nginx()
{
printf "huprestarting nginx...\n"
kill -HUP `cat /usr/local/nginx/logs/nginx.pid` 2>&1
}
case $1 in
start)
function_start_nginx
;;
stop)
function_stop_nginx
;;
restart)
function_restart_nginx
;;
hup)
function_hupstart_nginx
;;
*)
printf "Usage:$0{start|stop|restar|hup} \n"
esac
检查端口及服务
测试web接口
注:以上nginx 安装完成
四.配置nginx反向代理负载均衡
备份主配置文件 手动编辑新配置文件
cd /usr/local/nginx/conf
mv nginx.conf nginx.conf.bak
user nginx nginx;
worker_processes 10;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream www.haidongqing.com {
server 192.168.137.136:80;
server 192.168.137.137:80;
}
upstream mail.haidongqing.com {
server 192.168.137.100:80;
server 192.168.137.101:80;
}
server
{
listen 80;
server_name www.haidongqing.com;
location / {
proxy_pass http://www.haidongqing.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
log_format www_haidongqing_com '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/www.log www_haidongqing_com;
server
{
listen 80;
server_name mail.haidongqing.com;
location / {
proxy_pass http://mail.haidongqing.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
log_format mail_haidongqing_com '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/mail.log mail_haidongqing_com;
}
mail.haidongqing.com 的请求将转发到100 和101两台服务器上
www.haidongqing.com 的请求将转发到136和137两台服务器上
配置完成 检查语法即可重启nginx
./usr/local/nginx/sbin/nginx -t
./usr/local/nginx/sbin/sng restart
注:以上日志配置必须写在server之外.否则启动时会出来告警提示.
访问测试
将客户端DNS改为nginx+dns服务器地址,配置各realServer的dns指向及web服务器配置
以下截图为测试结果,刷新一次页面将会变化一次.如果是生产环境.所显示的内容应该是完全相同的
转载于:https://blog.51cto.com/haidongqing/1362882