Nginx站点安全

一、nginx简介

Nginx("engine x") 是一个高性能的 HTTP 反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。NginxIgor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发。

Nginx ("engine x") 是一个高性能的 HTTP 反向代理服务器,也是一个 IMAP/POP3/SMTP代理服务器 Nginx 是由 Igor Sysoev 俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004104日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。201161日,nginx 1.0.4发布。

支持高并发连接.官方测试的是5w并发连接但在实际生产中可制成2-4w并发连接数,得益于nginx使用最新的epoll(linux 2.6内核)kqueue(freebsd)网络I/O模型.apache使用的则是传统的select模型,其比较稳定的prefork模式为多进程模式,需要经常派生子进程,所消耗的CPU等服务器资源要比nginx高的多.

二、安装nginx

安装环境:虚拟机vmware10centos6.4-64位操作系统最小化安装

软件版本:nginx-1.4.4.tar 下载地址:www.nginx.org

还需要我们安装事件库,因为nginx是事件触发机制。软件包版本:libevent-2.0.16-stable.tar

还需要注意的是预编译环境,在安装时会检测预编译环境的,如果出错,一定要检测自己自己的预编译环境。

安装过程:

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

[root@ahao~]# cd /usr/local/src/nginx-1.4.4/

[root@ahaonginx-1.4.4]# groupadd -r nginx

[root@ahaonginx-1.4.4]# useradd -r -g nginx -s /sbin/nologin -M nginx

[root@ahaonginx-1.4.4]# yum install pcre-devel

[root@ahao~]# tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src

[root@ahao~]# cd /usr/local/src/libevent-2.0.16-stable/

[root@ahaolibevent-2.0.16-stable]# ./configure --prefix=/usr/local/libevent

[root@ahaolibevent-2.0.16-stable]# make && make install

[root@ahaolibevent-2.0.16-stable]# cd /usr/local/libevent

[root@ahaolibevent]# ll lib

[root@ahaolibevent]# vim /etc/ld.so.conf.d/libevent.conf//添加如下目录:

                           /usr/local/libevent/lib

[root@ahaolibevent]# ldconfig /重新配置

[root@ahaolibevent]# ldconfig -pv |grep libevent 查看一下

[root@ahao~]# cd /usr/local/src/nginx-1.4.4/

[root@ahao nginx-1.4.4]# ./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@ahao nginx-1.4.4]# yum install openssl-devel  //出现错误,需要安装openssl-devel

[root@ahaonginx-1.4.4]# make && make install

[root@ahaonginx]# cd /usr/local/nginx/sbin/

[root@ahaosbin]# ./nginx –t  /测试安装之后的配置文件是否有错,

mkdir:cannot create directory `/var/tmp/nginx/client': No such file or directory

[root@ahaosbin]# mkdir -pv  /var/tmp/nginx/client

[root@ahaosbin]# ./nginx –t

nginx:the configuration file /etc/nginx/nginx.conf syntax is ok

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

编写控制脚本:


#!/bin/bash

# ./etc/init.d/functions

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

lockfile=/var/lock/nginx.lock


start (){

     if [ -e $lockfile ];then

    echo "the nginx server isstarted"

    else

    echo -n "the nginx server isstarting......"

    sleep 1

    $prog && echo -e"[\033[32mOK\033[0m] " && touch $lockfile ||echo"failer"

   fi


}

stop () {

      if [ ! -e $lockfile ];then

    echo "the nginx server isstoped"

    else

    echo -n "the nginx server isstoping......"

    sleep 1

    $prog -s stop  && echo "OK" && rm-rf $lockfile || echo "failer"

 fi

}

restart(){

if [ -e $lockfile ]; then

         echo -n "the nginx isstoping..."

         $prog -s stop && echo  "OK" && rm -rf $lockfile ||echo "failer"

         echo -n "the nginx isstarting..."

         $prog && echo "OK"&& touch $lockfile ||echo "failer"

       fi

}

case"$1" in


start)

   start

   ;;

stop)

   stop

   ;;

restart)

stop

start

  ;;

*)

echo"USAGE:start|stop|restart"

esac

service nginx start//启动

先测试访问web服务器:

nginx站点安全_来源控制

三、站点安全

Nginx站点安全是基于模块实现的不同的安全设置需要不同的模块。

1、身份验证

身份验证需要Auth Basic模块。

参考网站:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html

配置如下:

vim /etc/nginx/nginx.conf /编辑nginx主配置文件添加

  server {

         listen       192.168.1.199:80;

         server_name  tec.abc.com;


         location / {

             root   /usr/local/nginx/tec;

             index  index.html index.htm;

         }  

         auth_basic      "a-hao";

         auth_badic_user_file    /usr/local/nginx/tec/.htpasswd;

}

nginx站点安全_身份验证_02

这里需要我们知道.htpasswd文件的产生,需要httpd-tools软件包,所以需要在主机上安装这样软件包。

[root@ahao init.d]# yum install httpd-tools

[root@ahao init.d]#  mkdir/usr/local/nginx/tec

[root@ahao init.d]# cd /usr/local/nginx/tec

[root@ahao tec]# vim index.html //在文件添加一行显示就可以。比如:tec

现在我们生成.htpasswd文件

[root@ahao tec]# htpasswd -c /usr/local/nginx/tec/.htpasswdaaa

完成之后我们可以在浏览器中访问一下(实验环境虚拟机ip是:192.168.1.199

nginx站点安全_身份验证_03

证明身份验证成功。

2、来源控制

来源控制是基于access模块实现的

参考网站:http://nginx.org/en/docs/http/ngx_http_access_module.html

配置如下:

Access可以在server里引用,也可以在location中引用。

为了便于实现的需要,我先安装一个文本编辑器------lynx

          vim/etc/nginx/nginx.conf /编辑nginx主配置文件添加

nginx站点安全_nginx_04

                  lynx  /etc/hosts中添加

192.168.1.199  www.abc.com

192.168.1.199  tec.abc.com

完成之后你可以测试:

root@ahao tec]# lynx  http://tec.abc.com

这个页面是需要经过身份验证的,测试中,成功的话会让你身份验证的。

[root@ahao tec]# vim /etc/nginx/nginx.conf

nginx站点安全_加密访问_05

[root@ahao tec]# service nginx restart

重启之后测试:(本地物理地址是192.168.1.105

nginx站点安全_加密访问_06

所以在我们物理机上是可以访问的。

nginx站点安全_身份验证_07

这样就达到目的,物理机192.168.1.105可以访问服务,但是在虚拟机中192.168.1.199就不能访问服务器。

3、加密访问

加密访问使用的模块是:ssl

参考网站:http://nginx.org/en/docs/http/ngx_http_ssl_module.html

配置:

CA服务器配置:

                  [root@ahao~]# vim /etc/pki/tls/openssl.cnf

                   首先我们要简单的配置ssl模块文件:

                   根据文件中需要一个数据文件来记录证书颁发的情况。所以我们就建立这样的                        文件,

                  [root@ahao~]# cd /etc/pki/CA

           [root@ahao CA]# touch index.txt

                  [root@ahaoCA]# touch serial //这是记录证书序号的文件,也需要手动建立

                  [root@ahaoCA]# echo "01">serial //加入序号,以“01”开始

                   在openssl.cnf文件中还需要更改证书颁发所允许的地区,要给成不受限的。

                  countryName             = optional

           stateOrProvinceName     =optional

                  organizationName        = optional

                   还可以根据自己的需要更改默认颁发证书的国家,省份,城市。

                  [root@ahaoCA]# openssl genrsa 1024 >private/cakey.pem //产生私钥文件

                  [root@ahaoCA]# chmod 600 private/cakey.pem  //更改权限

                  //产生公钥文件

                  [root@ahaoCA]# openssl req -new -key private/cakey.pem -x509 -out                             cacert.pem

           You are about to be asked to enter information that will                                 beincorporated

           into your certificate request.

           What you are about to enter is what is called a Distinguished Nameor a                     DN.

           There are quite a few fields but you can leave some blank

           For some fields there will be a default value,

           If you enter '.', the field will be left blank.

           -----

           Country Name (2 letter code) [CN]:

           State or Province Name (full name) [BEIJING]:

           Locality Name (eg, city) [BEIJING]:

           Organization Name (eg, company) [Default Company Ltd]:CA

           Organizational Unit Name (eg, section) []:tec  

           Common Name (eg, your name or your server's hostname) []:rootca.org

           Email Address []:


   Web服务器现在需要向CA服务器获取证书(证书的获取是需要个过程,证书上要有web服务        的公钥,公钥从哪里来呢,公钥是从私钥中产生的,所以在请求的过程中,必须要先                有WEB服务的私钥,再发送请求,获得证书。)

//产生web服务的私钥文件

[root@ahao CA]# mkdir -pv /etc/nginx/certs

mkdir: created directory `/etc/nginx/certs'

[root@ahao CA]# cd /etc/nginx/certs/

[root@ahao certs]# openssl genrsa 1024 >nginx.key

                  [root@ahaocerts]# chmod 600 nginx.key //更改权限


[root@ahaocerts]# openssl req -new -key  nginx.key-out nginx.crq // 公钥文件的建立

You are about to be asked to enter information that will beincorporated

into your certificate request.

What you are about to enter is what is called a Distinguished Nameor a DN.

There are quite a few fields but you can leave some blank

For some fields there will be a default value,

If you enter '.', the field will be left blank.

-----

Country Name (2 letter code) [CN]:

State or Province Name (full name) [BEIJING]:HENAN

Locality Name (eg, city) [BEIJING]:ZHENGZHOU

Organization Name (eg, company) [Default CompanyLtd]:TUOYUANEDU    

Organizational Unit Name (eg, section) []:tec

Common Name (eg, your name or your server's hostname) []:tec.abc.com

Email Address []:


Please enter the following 'extra' attributes

to be sent with your certificate request

A challenge password []:

An optional company name []:


      [root@ahaocerts]# openssl ca -in nginx.crq -out nginx.cert //建立请求                        文件

      Usingconfiguration from /etc/pki/tls/openssl.cnf

Check that the request matches the signature

Signature ok

Certificate Details:

      Serial Number: 1 (0x1)

      Validity

          Not Before: Nov 30 00:17:54 2013 GMT

          Not After : Nov 30 00:17:54 2014 GMT

      Subject:

           countryName               = CN

          stateOrProvinceName       = HENAN

          organizationName          =TUOYUANEDU

          organizationalUnitName    = tec

          commonName                =tec.abc.com

      X509v3 extensions:

          X509v3 Basic Constraints:

               CA:FALSE

          Netscape Comment:

               OpenSSL Generated Certificate

          X509v3 Subject Key Identifier:

              D1:DD:F8:9A:46:B1:01:1D:ED:B2:19:49:6C:7A:E8:48:47:CE:A9:CB

          X509v3 Authority Key Identifier:

              keyid:3E:A6:CE:D8:37:0C:F8:2E:5B:6C:59:2A:DE:39:EF:F9:AF:F3:34:45


Certificate is to be certified until Nov 30 00:17:54 2014 GMT (365days)

Sign the certificate? [y/n]:y

1 out of 1 certificate requests certified, commit? [y/n]y

Write out database with 1 new entries

Data Base Updated

[root@ahao certs]# cat /etc/pki/CA/index.txt //查看是否有证书文件

V       141130001754Z                01     unknown  /C=CN/ST=HENAN/O=TUOYUANEDU/OU=tec/CN=tec.abc.com

我们还需要将证书与web服务器绑定,

[root@ahao nginx]# vim /etc/nginx/nginx.conf //在主配置文档中添加绑定项:

nginx站点安全_身份验证_08


我们现在启动一台windowsxp虚拟机,以它来访问web服务,事先在C\windows\system32\drives\etc\hosts文件中添加192.168.1.199  tec.abc.com

设置之后我们可以访问试试:出现下面的情况

nginx站点安全_来源控制_09

出现这样的情况,是主机不能识别证书是哪个权威机构颁发的,就是不认可证书,这样的情况在nginx中有一种方法就是需要我们将CA服务器证书与web服务中的证书合成一张证书。

[root@ahao certs]# cp /etc/pki/CA/cacert.pem  ./  //CA证书拷贝过来

[root@ahao certs]# mv nginx.cert nginx.cert.bak  //改名字便于区分,

[root@ahao certs]# cat nginx.cert.bak  cacert.pem >nginx.cert   // 合成一张证书,合成名字还是原来WEB服务的证书名字。

nginx站点安全_来源控制_10

出现CA服务证书,就证明对了,我们就安装证书就可以了


nginx站点安全_来源控制_11

就可以正常访问了。