一.环境准备html
操做系统:CentOS 6.4 x86_64
nginx
软件版本:Nginx 1.4.5web
二.安装NGINX
算法
解压vim
tar xvf nginx-1.4.5.tar.gz 后端
2.新建nginx用户和组浏览器
[root@xiao59 softs]# groupadd -g 108 -r nginx缓存
[root@xiao59 softs]# useradd -u 108 -r -g 108 nginx安全
[root@xiao59 softs]# id nginxbash
uid=108(nginx) gid=108(nginx) groups=108(nginx)
3.准备编译配置文件
[root@xiao59 softs]# yum install -y pcre-devel openssl-devel
4.编译并安装
[root@xiao59 nginx-1.4.5]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_gzip_static_module --with-http_ssl_module
[root@xiao59 nginx-1.4.5]# make && make install
5.为nginx提供启动脚本
[root@xiao59 nginx-1.4.5]# vim /etc/init.d/nginx
===============================
#!/bin/bash
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /usr/local/nginx/conf/nginx.conf
# config: /usr/local/nginx/sbin/nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
#variables
opt=$1
NGINX_CONF=/usr/local/nginx/conf/nginx.conf
NGINX=/usr/local/nginx/sbin/nginx
NGINX_PID=/usr/local/nginx/logs/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
#function
function if_opt {
if [ $2 == 0 ]; then
echo -n "nginx is $1 ok!" && success
else
echo -n "nginx is $1 error!" && failure
fi
}
##start nginx
case $opt in
start)
if [ -f ${NGINX_PID} ]; then
echo "nginx is already start!"
else
${NGINX} -c ${NGINX_CONF}
if_opt start $?
fi
;;
##stop nginx
stop)
if [ ! -f ${NGINX_PID} ] || [ `netstat -tnpl | grep nginx | wc -l` -lt 1 ]; then
echo "nginx is already stopped!"
else
cat ${NGINX_PID} | xargs kill -QUIT
if_opt stop $?
fi
;;
##restart nginx
restart)
if [ `netstat -tnpl | grep nginx | wc -l` -ge 1 ]; then
cat ${NGINX_PID} | xargs kill -HUP
if_opt restart $?
else
echo "nginx is not running..." && echo "start nginx"
${NGINX} -c ${NGINX_CONF}
if_opt restart $?
fi
;;
##test nginx server
configtest)
${NGINX} -t ${NGINX_CONF}
if_opt configtest $?
;;
*)
echo "usage:$1 {start|stop|restart|configtest}"
esac
=============================
//注意:
1)$2表明$?是否执行成功
2)NGINX_PID不稳定,建议用`netstat -tnpl | grep nginx | wc -l`
6.为此脚本赋予执行权限
[root@xiao59 nginx-1.4.5]# chmod +x /etc/init.d/nginx
7.添加至服务管理列表,使其开机自启动
[root@xiao59 nginx-1.4.5]# chkconfig --add nginx
[root@xiao59 nginx-1.4.5]# chkconfig nginx on
三.NGINX反向代理(准备一台测试服务器10.10.54.58)
安装httpd
[root@xiao58 ~]# yum install -y httpd
2.提供测试页面
[root@xiao58 ~]# echo "<h1> web.test.com</h1>" >/var/www/html/index.html
3.启动httpd的服务
[root@xiao58 ~]# /etc/init.d/httpd start
4.测试
浏览器输入:10.10.54.58
5.正反向代理
结论就是,正向代理 是一个位于客户端和原始服务器(origin server)之间的服务器,为了从原始服务器取得内容,客户端向代理发送一个请求并指定目标(原始服务器),而后代理向原始服务器转交请求并将得到的内容返回给客户端。客户端必需要进行一些特别的设置才能使用正向代理。
继续举例:
例用户访问 http://www.test.com/readme,但www.test.com上并不存在readme页面,他是偷偷从另一台服务器上取回来,而后做为本身的内容返回用户,但用户并不知情。这里所提到的 www.test.com 这个域名对应的服务器就设置了反向代理功能。
结论就是,反向代理正好相反,对于客户端而言它就像是原始服务器,而且客户端不须要进行任何特别的设置。客户端向反向代理的命名空间(name-space)中的内容发送普通请求,接着反向代理将判断向何处(原始服务器)转交请求,并将得到的内容返回给客户端,就像这些内容本来就是它本身的同样。
从用途上来说:
正向代理的典型用途是为在防火墙内的局域网客户端提供访问Internet的途径。正向代理还可使用缓冲特性减小网络使用率。反向代理的典型用途是将防火墙后面的服务器提供给Internet用户访问。反向代理还能够为后端的多台服务器提供负载平衡,或为后端较慢的服务器提供缓冲服务。另外,反向代理还能够启用高级URL策略和管理技术,从而使处于不一样web服务器系统的web页面同时存在于同一个URL空间下。
从安全性来说:
正向代理容许客户端经过它访问任意网站而且隐藏客户端自身,所以你必须采起安全措施以确保仅为通过受权的客户端提供服务。反向代理对外都是透明的,访问者并不知道本身访问的是一个代理。
6.nginx代理模块
说明:代理模块的指令有不少我这里只讲解重要的proxy_pass,想了解更多代理指令请参考官方中文文档。
这个模块能够转发请求到其余的服务器。HTTP/1.0没法使用keepalive(后端服务器将为每一个请求建立而且删除链接)。nginx为浏览器发送HTTP/1.1并为后端服务器发送HTTP/1.0,这样浏览器就能够为浏览器处理keepalive。
location / {
proxy_pass http:
//localhost
:8000;
proxy_set_header X-Real-IP $remote_addr;
}
注意,当使用http proxy模块(甚至FastCGI),全部的链接请求在发送到后端服务器以前nginx将缓存它们,所以,在测量从后端传送的数据时,它的进度显示可能不正确。
[root@xiao59 ~]# cd /usr/local/nginx/conf/
[root@xiao59 conf]# cp nginx.conf nginx.conf.bak
[root@xiao59 conf]# vim nginx.conf
====================
location / {
proxy_pass http://10.10.54.58;
}
=======================
指令说明:proxy_pass
语法:proxy_pass URL
默认值:no
使用字段:location, location中的if字段
这个指令设置被代理服务器的地址和被映射的URI,地址可使用主机名或IP加端口号的形式,例如:proxy_pass http://localhost:8000/uri/;
8.重启服务
[root@xiao59 conf]# /etc/init.d/nginx restart
9.测试
浏览器输入:10.10.54.59(当咱们访问10.10.54.59时,被代理从新定向到10.10.54.58上)
10.查看WEB服务器日志
[root@xiao58 ~]# tail -f /var/log/httpd/access_log
注:能够看到咱们这里的客户的IP全是,nginx代理服务器的IP,并非真实客户端的IP。下面咱们修改一下,让日志的IP显示真实的客户端的IP。
11.修改nginx配置文件
[root@xiao59 ~]# vim /usr/local/nginx/conf/nginx.conf
==============================================
location / {
proxy_pass http:
//192
.168.18.201;
proxy_set_header X-Real-IP $remote_addr;
#加上这一行
}
===============================================
指令说明:proxy_set_header
语法:proxy_set_header header value
默认值: Host and Connection
使用字段:http, server, location
这个指令容许将发送到被代理服务器的请求头从新定义或者增长一些字段。这个值能够是一个文本,变量或者它们的组合。proxy_set_header在指定的字段中没有定义时会从它的上级字段继承。
12.重启服务
[root@xiao59 conf]# /etc/init.d/nginx restart
13.测试并查看日志
[root@xiao58 ~]# tail -f /var/log/httpd/access_log
注,你们能够看到日志记录的仍是代理的IP,没有显示真实客户端的IP.
14.查看并修改httpd配置文件
[root@xiao58 ~]# vim /etc/httpd/conf/httpd.conf
============================================================
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined#修改成以下
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\""
combined
=============================================
15.重启并测试
[root@xiao58 ~]# /etc/init.d/httpd restart
[root@xiao58 ~]# tail -f /var/log/httpd/access_log
注,你们能够看到如今的日志里记录的IP地址就是真实的客户端地址了
四.Nginx之负载均衡
1.upstream 负载均衡模块说明
案例:下面设定负载均衡的服务器列表。
upstream
www.ssr.com
{
server 10.10.54.54:80;
server 10.10.54.59:8090 max_fails=3 fail_timeout=20s;
server 10.10.54.56:8060;
}
server {
location / {
proxy_pass http:
//www.ssr
.com;
}
}
upstream是Nginx的HTTP Upstream模块,这个模块经过一个简单的调度算法来实现客户端IP到后端服务器的负载均衡。在上面的设定中,经过upstream指令指定了一个负载均衡器的名称www.ssr.com。这个名称能够任意指定,在后面须要用到的地方直接调用便可。
2.upstream 支持的负载均衡算法
Nginx的负载均衡模块目前支持4种调度算法,下面进行分别介绍,其中后两项属于第三方调度算法。
轮询(默认)。每一个请求按时间顺序逐一分配到不一样的后端服务器,若是后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响。Weight 指定轮询权值,Weight值越大,分配到的访问机率越高,主要用于后端每一个服务器性能不均的状况下。
ip_hash。每一个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题。
fair。这是比上面两个更加智能的负载均衡算法。此种算法能够依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配。Nginx自己是不支持fair的,若是须要使用这种调度算法,必须下载Nginx的upstream_fair模块。
url_hash。此方法按访问url的hash结果来分配请求,使每一个url定向到同一个后端服务器,能够进一步提升后端缓存服务器的效率。Nginx自己是不支持url_hash的,若是须要使用这种调度算法,必须安装Nginx 的hash软件包。
3.upstream 支持的状态参数
在HTTP Upstream模块中,能够经过server指令指定后端服务器的IP地址和端口,同时还能够设定每一个后端服务器在负载均衡调度中的状态。经常使用的状态有:
down,表示当前的server暂时不参与负载均衡。
backup,预留的备份机器。当其余全部的非backup机器出现故障或者忙的时候,才会请求backup机器,所以这台机器的压力最轻。
max_fails,容许请求失败的次数,默认为1。当超过最大次数时,返回proxy_next_upstream 模块定义的错误。
fail_timeout,在经历了max_fails次失败后,暂停服务的时间。max_fails能够和fail_timeout一块儿使用。
注,当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是weight和backup。
4.配置nginx负载均衡
[root@CentOS001 ~]# mkdir -p /usr/local/nginx/virtual/www.ssr.com.conf
[root@CentOS001 ~]# vim /usr/local/nginx/virtual/www.ssr.com.conf
======================================
upstream www.ssr.com {
server 10.10.54.54:80 max_fails=3 weight=1 fail_timeout=60s;
server 10.10.54.59:8090 max_fails=3 weight=2 fail_timeout=60s;
server 10.10.54.56:8060;
}
server {
listen 80;
server_name www.ssr.com;
charset utf-8;
access_log logs/www.access.log main;
index index.html;
location /upload {
autoindex on;
}
location /download {
rewrite ^/download$ /upload last;
}
location / {
proxy_pass http://www.ssr.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;
}
}
=======================================================================
[root@CentOS001 ~]# vim /usr/local/nginx/conf/nginx.conf
================================
##加到HTTP中
include /usr/local/nginx/virtual/www.ssr.com.conf;
================================
注,upstream是定义在server{ }以外的,不能定义在server{ }内部。定义好upstream以后,用proxy_pass引用一下便可。
5.从新加载一下配置文件
[root@CentOS001 ~]# /etc/init.d/nginx restart
6.修改web服务器的端口
[root@xiao56 ~]# vim /etc/httpd/conf/httpd.conf
============================
Listen 8060
ServerName www.ssr.com:8060
NameVirtualHost *:8060
<VirtualHost *:8060>
ServerAdmin wangxq@ssr.com
DocumentRoot /var/www/html
ServerName www.ssr.com
ErrorLog logs/www-error_log
CustomLog logs/www-access_log common
</VirtualHost>
=============================
重启服务:[root@xiao56 ~]# /etc/init.d/httpd restart
//10.10.54.59也作相应操做,端口号改成8090.
7.测试一下
浏览器输入:10.10.54.54
注,你们能够不断的刷新浏览的内容,能够发现56与59是交替出现的,达到了负载均衡的效果。