一、Nginx搭建

Nginx简介:
Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。由俄罗斯的程序设计师Igor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler(俄文:Рамблер)使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:新浪、网易、腾讯等。

 

Nginx官方站点:http://www.nginx.org

 

(1)安装支持软件:
Nginx的配置及运行需要pcre、zlib等软件包的支持,因此应预先安装这些软件的开发包(devel),以便提

供相应的库和头文件,确保Nginx的安装顺利完成。

[root@localhost ~]# service iptables stop
[root@localhost ~]# setenforce 0
[root@localhost ~]# mount /dev/cdrom /mnt/
[root@localhost ~]# vim /etc/yum.repos.d/yum.repo
[base]
name=RedHat Enterprise Linux Server
baseurl=file:///mnt/Server
gpgcheck=0
[root@localhost ~]# yum -y install pcre-devel zilb-devel

 

(2)创建运行用户、组:
Nginx服务程序默认以nobody身份运行,建议为其创建专门的用户账号,以便更准确地控制其访问权限,增加灵活性、降低安全风险。如:创建一个名为nginx的用户,不建立宿主目录,也禁止登录到shell环境。

[root@localhost ~]# useradd -M -s /sbin/nologin nginx

 

(3)编译安装nginx
下载路劲 http://nginx.org/download/nginx-1.6.2.tar.gz

[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/
[root@localhost ~]# tar xf nginx-1.6.2.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.2/
[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx

--with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-

http_gzip_static_module && make && make install

 

--prefix  设定Nginx的安装目录
--user和—group 指定Nginx运行用户和组
--with-http_stub_status_module 启用http_stub_status_module模块以支持状态统计
--with-http_ssl_module  启用SSL模块
--with-http_flv_module 启用FLV模块,提供寻求内存使用基于时间的偏移量文件

 

(4)为了使Nginx服务器的运行更加方便,可以为主程序nginx创建链接文件,以便管理员直接执行nginx命令就可以调用Nginx的主程序。


[root@localhost nginx-1.6.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.6.2]# ll /usr/local/bin/nginx
lrwxrwxrwx 1 root root 27 01-11 04:58 /usr/local/bin/nginx -> /usr/local/nginx/sbin/nginx

 

(5)启动、停止Nginx:
[root@localhost nginx-1.6.2]# nginx
[root@localhost nginx-1.6.2]# netstat -anpt | grep :80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN     

6074/nginx: master

[root@localhost ~]# killall -s HUP nginx
[root@localhost ~]# killall -s QUIT nginx

HUP  重载配置 等同于-1
QUIT 退出进程 等同于-3
KILL 杀死进程 

 

(6)测试是否成功
[root@localhost ~]# elinks --dump http://localhost
                               Welcome to nginx!


(8)编写启动脚本


[root@nginx ~]# vim /etc/init.d/nginx

#!/bin/bash
# chkconfig: 2345 99 20
# description: Nginx Server Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
 $PROG
;;
stop)
 kill -s QUIT $(cat $PIDF)
;;
restart)
 $0 stop
 $0 start
;;
reload)
 kill -s HUP $(cat $PIDF)
;;
*)
 echo "Usage: $0 (start|stop|restart|reload)"
 exit 1
esac
exit 0

[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# chkconfig --add nginx
[root@localhost ~]# chkconfig nginx on
[root@localhost ~]# chkconfig --list nginx
nginx           0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭

 

(9)配置文件nginx.conf:


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在Nginx服务器的主配置文件nginx.conf中,包括全局配置、I/O事件配置、HTTP配置这三大块内容,配置语句的格式为"关键字 值;"(末尾以分号表示结束),以"#"开始的部分表示注释。

 

user nginx;   //运行用户,Nginx的运行用户实际是编译时指定的nginx,若编译时未

指定则默认为nobody;
worker_processes 2;  //指定nginx启动的工作进程数量,建议按照cpu数目来指定,一般为它

的倍数。

 

events {
use epoll;   //使用epool模型,对于2.6以上的内核,建议使用epool模型以提高性能
worker_connections 4096;  //每个进程允许的最多连接数(默认为1024),每个进程的连接

数应根据实际需要来定,一般在10000以下,理论上每台nginx服务器的最大连接数
}

 

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" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;  //访问日志位置

    sendfile        on;      //支持文件发送(下载)
    keepalive_timeout  65;     //连接保持超时

    server {        //web服务的监听配置
        listen       80;     //监听地址及端口
        server_name  www.baidu.com;   //网站名称(FQDN)
  charset utf-8;      //网页的默认字符集

        location / {      //跟目录配置
            root   html;     //网站根目录的位置安装位置的

html中
            index  index.html index.htm; //默认首页(索引页)
        }

        error_page   500 502 503 504  /50x.html; //内部错误的反馈页面
        location = /50x.html {    //错误页面配置
            root   html;
        }
 }


二、状态访问加密(listen 支持采用 IP地址:端口 形式)

(1)、状态统计及虚拟主机应用:
Nginx内置了HTTP_STUB_STATUS状态统计模块,用来反馈当前的WEB访问情况。配置
编译参数时可添加—with-http_stub_stastus_module来启用此模块。要使用Nginx的状态统计功能,除了启用内建模块以外,还需要修改nginx.conf文件,指定访问位置并打开stub_status配置。在http{}配置的

server{}子配置内添加如下配置项


[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

        location ~ /status {
            stub_status on;  //打开状态统计功能
            access_log off;  //关闭此位置的日志记录
        }

 

[root@localhost ~]# service nginx restart

浏览器访问 http://192.168.3.10/status

Active connections: 1
server accepts handled requests
 1 1 3
Reading: 0 Writing: 1 Waiting: 0


Active connections 表示当前活跃的连接数,
第三行的三个数字表示Nginx当前总共 处理了1个连接,成功创建1次握手,总共处理了3个请求。
最后一行的Reading表示Nginx读取到客户端Header信息数,
Writing表示Nginx返回给客户端的Header信息数
“Waiting”表示Nginx已经处理完,正在等候下一次请求指令时的驻留连接数。


Nginx 支持为目录添加密码认证,使用apache 的 htpasswd 来创建密码文件

[root@localhost ~]# yum -y install httpd
[root@localhost ~]# htpasswd -c /usr/local/nginx/.htpasswd zhenyang
New password:
Re-type new password:
Adding password for user zhenyang

 

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location ~ /status {
            stub_status on;
            access_log off;
            auth_basic "Nginx Status";
            auth_basic_user_file /usr/local/nginx/.htpasswd;
        }
  
客户端访问控制:
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
        location ~ /status {
            stub_status on;
            access_log off;
            auth_basic "Nginx Status";
            auth_basic_user_file /usr/local/nginx/.htpasswd;
   allow 192.168.200.1;
   deny 192.168.200.0/24;
        }

注:allow 允许规则,deny拒绝规则;规则的执行是按从上向下执行,匹配某条规则后将不再检查其他规则

三、构建虚拟主机
2)虚拟主机:
使用Nginx搭建虚拟主机服务器时,每个虚拟WEB站点拥有独立的"server {}"配置段,各自监听的IP地址、

端口号可以单独指定,当然网站名称也是不同的。
例如:要创建两个站点www.baidu.com和www.sina.com
为两个虚拟WEB主机分别建立根目录,并准备测试首页

 

[root@localhost ~]# mkdir /usr/local/nginx/html/baidu
[root@localhost ~]# mkdir /usr/local/nginx/html/sina
[root@localhost ~]# echo "

恭喜你访问成功

" > /usr/local/nginx/html/benet/baidu
[root@localhost ~]# echo "

恭喜你访问成功了

" > /usr/local/nginx/html/accp/sina
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
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" "$http_x_forwarded_for"';

    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  www.bai.com;
 charset utf-8;
        access_log  logs/baidu.access.log  main;

        location / {
            root   html/baidu;
            index  index.html index.htm;
        }
    }

    server {
        listen       80;
        server_name  www.sina.com;
        charset utf-8;
        access_log  logs/sina.access.log  main;

        location / {
            root   html/sina;
            index  index.html index.htm;
        }
    }
}

 

[root@nginx ~]# service nginx reload


[root@nginx ~]# vim /etc/hosts               (若有DNS,此步可略,次步就是为了实现解析域名效果)
192.168.200.128 www.baidu.com
192.168.200.128 www.sina.com


[root@nginx ~]# elinks --dump http://www.baidu.com
                           恭喜你访问成功     
[root@nginx ~]# elinks --dump http://www.sina.com
                           恭喜你访问成功了      
实验完毕