DAY04

一、搭建Nginx服务器

1nginx简介

   nginx是一个轻量级的HTTP服务器,同时也是一个反向代理服务器(web\mail

Linux/Unix平台常用web服务器:Apache /Nginx /Lighttpd /Tomcat

2、安装Nginx

   Nginx搭建网站服务器

   1)安装前的准备工作

[root@server1 ~]# service httpd stop //释放80端口

  2)添加用户

[root@server1 ~]# useradd nginx //进程运行时的所有者和所属组

3)解压

[root@server1 /]# tar -zxf nginx-0.8.55.tar.gz

4)安装PCRE库(nginx默认支持rewrite地址重写-修改用户请求地址,需要调用PCRE库)

[root@server1 ~]# yum -y install pcre pcre-devel

5)配置

--prefix=  指定安装目录    --user= 指定进程运行时的所有者

--group= 指定进程运行时的所属组   --with-http_stub_status_module   支持

--with-http_ssl_module   支持https访问

[root@server1 nginx-0.8.55]# vim nginx_1.sh

./configure --prefix=/usr/local/nginx --user=nginx   --group=nginx  --with-http_stub_status_module--with-http_ssl_module

[root@server1 nginx-0.8.55]# sh nginx_1.sh

6)编译、安装

[root@server1 nginx-0.8.55]# make && make install

[root@server1 nginx]# pwd

/usr/local/nginx

[root@server1 nginx]# ls

conf  html  logs sbin

html 网页文件存放目录 conf配置文件存放目录,nginx.conf主配置文件

logs  日志目录   sbin  服务启动脚本

7)开启服务

-v   查看nginx的版本信息   -V  查看安装时的配置参数

-t   测试默认配置文件      -c 指定配置文件(可选项)

[root@server1 nginx]# sbin/nginx –t //测试主配置文件

[root@server1 nginx]# sbin/nginx //启动服务

[root@server1 nginx]# netstat -anptu | grep :80

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

8)访问

[root@server1 nginx]# elinks --dump http://localhost

9)编辑网页文件

[root@server1 html]# pwd

/usr/local/nginx/html

[root@server1 html]# echo 123 > a.html

10)停止服务

[root@server1 logs]# pkill -int nginx

Pkill/kill –信号进程名/pid

信号: TERM ,INT快速关闭

       QUIT     从容关闭,关闭主进程顺便关闭工作子进程

HUP      重载配置用新的配置开始新的工作进程从容关闭旧的工作进程

USR1     重新打开日志文件

USR2     平滑升级可执行程序

WINCH   从容关闭工作进程,不会立即关闭子进程

3、平滑升级Nginx

   平滑升级:在不停止服务的情况下,升级版本

[root@server1 nginx]# sbin/nginx –v //查看当前版本

1)解压

[root@server1 /]# tar -zxf nginx-1.0.5.tar.gz

[root@server1 /]# cd nginx-1.0.5

       2)配置:高版本配置参数要与低版本的一致

[root@server1 nginx-1.0.5]# /usr/local/nginx/sbin/nginx –V //查看当前版本配置信息

[root@server1 nginx-1.0.5]# vim nginx_2.sh

./configure --prefix=/usr/local/nginx --user=nginx --group=nginx--with-http_stub_status_module --with-http_ssl_module

       3)编译

[root@server1 nginx-1.0.5]# make

4)将新版本的ngnix脚本拷贝到nginx安装目录

[root@server1 sbin]# pwd

/usr/local/nginx/sbin

[root@server1 sbin]# mv nginx nginx.old

[root@server1 objs]# pwd

/nginx-1.0.5/objs

[root@server1 objs]# cp nginx /usr/local/nginx/sbin/

5)命令升级版本

[root@server1 nginx-1.0.5]# pwd

/nginx-1.0.5

[root@server1 nginx-1.0.5]# make upgrade

[root@server1 sbin]# ./nginx -v

nginx: nginx version: nginx/1.0.5

4、配置Nginx虚拟主机(主配置文件-安装目录下conf/ngnix.conf

   全局配置:写在容器外边,对所有容器生效

  局部配置:写在容器内,只对当前容器有效

全局配置和局部配置同时配置时,局部优先。

server用于发布虚拟主机,location用于发布网页目录

[root@server1 conf]# grep -vE "#|^$" nginx.conf

worker_processes  1;                 //默认开启进程数

events {  

    worker_connections  1024;         //并发连接数

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {                            //发布网站

        listen       80;                //监听端口

        server_name  localhost;          //主机名

        location / {                      //发布网页目录

            root   html;                //网页目录的位置

            index  index.html index.htm;  //首页文件名

        }

        error_page   500 502 503 504  /50x.html;    //错误页面

        location = /50x.html {

            root   html;

        }

    }

}

1)基于ip地址-通过访问的ip区分用户的请求

客户端http://192.168.1.15     /www目录里的网页文件

客户端http://192.168.1.105    /bbs目录里的网页文件

A、服务器配置(两块网卡)

[root@server1 ~]# ifconfig eth0:0 192.168.1.15

[root@server1 ~]# ifconfig eth0:1 192.168.1.105  //生产环境中至少有两块网卡

[root@server1 conf]# vim nginx.conf

http {

server {                            

    listen  192.168.1.15 :80;              

        server_name  www.tarena.com; 

        location  / {                    

            root   /www;             

            index  index.html; 

        }

}

server {                            

   listen    192.168.1.105:80;              

        server_name  bbs.tarena.com; 

        location  / {                    

            root   /bbs;             

            index  index.html; 

        }

    }

}

[root@server1 conf]# pkill -HUP nginx

[root@server1 ~]# mkdir /www /bbs

[root@server1 ~]# echo www.tarena.com > /www/index.html

[root@server1 ~]# echo bbs.tarena.com > /bbs/index.html

B、客户端测试

[root@ser2 ~]# elinks --dump http://192.168.1.15

[root@ser2 ~]# elinks --dump http://192.168.1.105

 

2)基于域名-通过主机名区分用户的访问(客户端能够对访问的主机名做主机)

客户端http://www.tarena.com   192.168.1.15 /www目录里的网页文件

客户端http://bbs.tarena.com    192.168.1.15   /bbs目录里的网页文件

A、客户端配置

[root@ser2 ~]# cat /etc/hosts

# Do not remove the following line, or various programs

127.0.0.1               localhost.localdomain localhost

192.168.1.15  www.tarena.comwww

192.168.1.15  bbs.tarena.combbs

B、服务器配置

[root@server1 conf]# vim nginx.conf

http {

server {                            

   listen       80;              

        server_name  www.tarena.com; 

        location  / {                    

            root   /www;             

            index  index.html; 

        }

}

server {                            

   listen       80;              

        server_name  bbs.tarena.com; 

        location  / {                    

            root   /bbs;             

            index  index.html; 

        }

    }

}

C、客户端测试

[root@ser2 ~]# elinks --dump http://www.tarena.com

[root@ser2 ~]# elinks --dump http://bbs.tarena.com

3)基于端口-通过端口区分

客户端http://www.tarena.com:8080   192.168.1.15 /www目录里的网页文件

客户端http://www.tarena.com:8090   192.168.1.15 /bbs目录里的网页文件

A、服务器配置

[root@server1 conf]# vim nginx.conf

http {

server {                            

   listen       8080;              

        server_name  www.tarena.com; 

        location  / {                    

            root   /www;             

            index  index.html; 

        }

}

server {                            

   listen       8090;              

        server_name  www.tarena.com; 

        location  / {                    

            root   /bbs;             

            index  index.html; 

        }

    }

}

[root@server1 conf]# pkill -HUP nginx

B、客户端测试

[root@ser2 ~]# elinks --dump http://www.tarena.com:8080

[root@ser2 ~]# elinks --dump http://www.tarena.com:8090

5、客户端访问控制、用户访问认证

客户端访问控制:写在location容器中

allow 192.168.1.1;                                                 

deny all;             //只允许192.168.1.1                            

                                                                   

deny 192.168.1.1;                                                

allow all;            //只拒绝192.168.1.1                              

只允许ip地址是192.168.1.25的主机访问192.168.1.1580端口

[root@server1 conf]# vim nginx.conf

http {

       server {

           listen192.168.1.15:80;

           server_name  www.tarena.com;

           location / {

               root  /www;

               indexindex.html;

               allow192.168.1.25;

               deny all;

          }

       }

}

[root@server1 conf]# pkill -HUP nginx

        用户访问认证:用户访问页面时需输入正确的用户名和密码才能访问,写在location容器中

auth_basic  “xxx”    //指定认证域的名字

auth_basic_user_file /usr/local/ngnix/conf/xxxx //指定存放认证的用户名和密码的文件位置

编辑配置文件

[root@server1 conf]# vim nginx.conf

http {

       server {

           listen192.168.1.15:80;

           server_name  www.tarena.com;

           location / {

               root  /www;

               indexindex.html;

               allow192.168.1.25;

               deny all;

auth_basic"authuser";

              auth_basic_user_file /usr/local/ngnix/conf/authuser.txt;

          }

       }

}

[root@server1 conf]# pkill -HUP nginx

生成文件/usr/local/ngnix/conf/authuser.txt

[root@server1 nginx]# htpasswd -c /usr/local/nginx/conf/authuser.txtadmin

[root@server1 nginx]# which htpasswd

/usr/bin/htpasswd

[root@server1 nginx]# rpm -qf /usr/bin/htpasswd

httpd-2.2.3-74.el5



6、配置Nginx反向代理服务器

   1web_1web_2开启网站服务,编写主页并本机测试

  2)测试ngnix服务器与网站服务器的连通性

3)在ngnix主机上配置ngnix反向代理,把自己接收到的访问网站的请求,分发给后端的网站服务器

nginx.conf文件里定义后端的服务器组

[root@server1 nginx]# vim conf/nginx.conf

http {

    upstream webgrp {

        server192.168.1.100:80;

        server192.168.1.200:80;

    }

    server {

        listen       80;

        server_name  localhost;

        location / {

            root   html;

            index  index.html index.htm;

            proxy_passhttp://webgrp;

        }

}

}

[root@server1 conf]# pkill -HUP nginx

7Nginx反向代理服务方式

异构模式:服务器配置不一致(web_1配置高、web_2配置低)

1)轮循:默认以轮循方式分发,每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉能自动剔除

2weight 指定轮循几率,权重和访问比率成正比,通常用于后端服务器性能不同的情况,默认值为1

[root@server1 nginx]# vim conf/nginx.conf

http {

    upstream webgrp {

        server192.168.1.100:80 weight=2;

        server192.168.1.200:80 weight=1;

}

}

3ip_hash:每个请求按照访问ip地址的hash 结果分配,这样可以让每个访问固定访问一个服务器,可以解决session问题

[root@server1 nginx]# vim conf/nginx.conf

http {

upstream webgrp {

   ip_hash;

        server192.168.1.100:80 ;

        server192.168.1.200:80;

}

}

4fair 按后端服务器的响应时间来分配请求,响应时间短的优先分配。默认不支持fair。需事先安装包ip_fair

8、设置后端服务器组主机工作状态

  1down  表示当前的服务器暂时不参与负载

  2backup 其他所有的非backup机器down或者忙的时候,请求会发给backup机器

3max_fails 允许请求失败的次数,默认为1,当超过最大次数时返回proxy_nexxt_upstream模块定义的错误

4fail_timeout  max_fails次失败后暂停服务的时间

upstream webgrp {

   ip_hash;

        server192.168.1.100:80  down;

        server192.168.1.200:80 backup;

        server192.168.1.201:80 max_fails=2 fail_timeout=1;

}