环境:CentOS-6.4-x86_64

解压:

[root@centos ~]#tar -zxvf nginx-1.0.11.tar.gz -C /usr/local/src

创建组:

[root@centos~]# groupadd -r nginx

[root@centos ~]#useradd -r -g nginx -s /bin/nologin -M nginx

编译以及安装

[root@centosnginx-1.0.11]#./configure \

               --conf-path=/etc/nginx/nginx.conf \

               --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-http_ssl_module \

               --with-http_flv_module \

               --with-http_stub_status_module \

               --with-http_gzip_static_module \

               --http-client-body-temp-path=/var/tmp/nginx/client/ \

               --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

               --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\

               --with-pcre

                           

[root@centosnginx-1.0.11]# make && make install

编辑脚本

[root@centos init.d]# vim nginx

    #!/bin/bash

    prog=/usr/local/nginx/sbin/nginx

    lockfile=/var/lock/nginx.lock

    . /etc/init.d/functions

    start(){

    if [ -e $lockfile ];then

       echo "the nginx  is started" && exit

       else

       echo -n "nginx server is starting... "

       sleep 1

       $prog && echo "ok" && touch $lockfile ||echo "fail"

    fi

  }

   stop(){

    if [ ! -e $lockfile ];then

        echo "the nginx  is stoped" && exit

        else

        echo -n "the nginx  is stoping... "

        sleep 1

        killproc nginx && echo "ok" && rm -rf $lockfile ||echo "[ fail ]"

     fi

  }

   configtest() {

       $prog -t

   }

  case $1 in

   start)

   start

        ;;

   stop)

   stop

        ;;

   configtest)

   configtest

        ;;

   *)

   echo "USAGE:start|stop|configtest"

        ;;

   esac

测试

[root@centosnginx-1.0.11]# service nginx configtest

nginx: theconfigtest file /etc/nginx/nginx.conf syntax is ok

nginx:configtest file /etc/nginx/nginx.conf test is successful

[root@centosnginx-1.0.11]# service nginx start      

nginx serverisstarting... [ ok]

[root@centosnginx-1.0.11]# netstat -tupln |grep 80

tcp        0     0 0.0.0.0:80                 0.0.0.0:*                   LISTEN      4363/nginx          

nginx反向代理. url重定向. 负载均衡_定向

重定向

[root@centos ~]#vim /etc/nginx/nginx.conf

nginx反向代理. url重定向. 负载均衡_定向_02

重新启动nginx,然后打开浏览器,输入一个关于bmp的地址,系统里面本身是没有的nginx反向代理. url重定向. 负载均衡_定向_03


测试成功!



反向代理

首先把另外开一台虚拟机,地址是192.168.11.3把需要的都配置好,然后在/var/www/html下建一个文件,看在本台机上能否访问到这个文件

nginx反向代理. url重定向. 负载均衡_定向_04

配置本机中nginx

nginx反向代理. url重定向. 负载均衡_定向_05

nginx反向代理. url重定向. 负载均衡_定向_06

重新启动,然后通过浏览器输入:

nginx反向代理. url重定向. 负载均衡_定向_07

测试成功!




负载均衡

后台有两台服务器,分别有两个小的测试页面,一个是192.168.11.2 一个是192.168.11.3  然后用代理服务器(地址是192.168.11.2)用轮询的方式看是否能访问到

为了不影响测试,先把原来的nginx去掉,用一个低版本的nginx,因为高版本的nginx在做的时候会出现很多问题

nginx反向代理. url重定向. 负载均衡_定向_08

解压补丁文件

nginx反向代理. url重定向. 负载均衡_定向_09

nginx反向代理. url重定向. 负载均衡_定向_10

开始打补丁

nginx反向代理. url重定向. 负载均衡_定向_11

编译以及安装

[root@centos nginx-1.0.11]# ./configure\

               --conf-path=/etc/nginx/nginx.conf \

               --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-http_ssl_module \

               --with-http_flv_module \

               --with-http_stub_status_module \

               --with-http_gzip_static_module \

               --http-client-body-temp-path=/var/tmp/nginx/client/ \

               --http-proxy-temp-path=/var/tmp/nginx/proxy/ \

               --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/\

               --with-pcre\

                   --add-module=/tmp/healthcheck_nginx_upstreams-master/

nginx反向代理. url重定向. 负载均衡_定向_12

配置文件

nginx反向代理. url重定向. 负载均衡_定向_13

nginx反向代理. url重定向. 负载均衡_定向_14

nginx反向代理. url重定向. 负载均衡_定向_15

重新启动

nginx反向代理. url重定向. 负载均衡_定向_16

nginx反向代理. url重定向. 负载均衡_定向_17

测试成功!

除了轮询外,还可以修改权重等方法来实现服务器的负载均衡,这里就不再详细说明了,可以到nginx的官网上,上面有许多介绍nginx的使用方法!