zpjx 发表于 2018-11-11 12:36:24

nginx站点安全

  Nginx站点安全
  一、nginx简介
  Nginx("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器。Nginx由Igor Sysoev 为俄罗斯访问量第二的Rambler.ru 站点开发。
  Nginx ("engine x") 是一个高性能的 HTTP 和反向代理服务器,也是一个 IMAP/POP3/SMTP代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。
  支持高并发连接.官方测试的是5w并发连接但在实际生产中可制成2-4w并发连接数,得益于nginx使用最新的epoll(linux 2.6内核)和kqueue(freebsd)网络I/O模型.而apache使用的则是传统的select模型,其比较稳定的prefork模式为多进程模式,需要经常派生子进程,所消耗的CPU等服务器资源要比nginx高的多.
  二、安装nginx
  安装环境:虚拟机vmware10下centos6.4-64位操作系统最小化安装
  软件版本:nginx-1.4.4.tar 下载地址:www.nginx.org
  还需要我们安装事件库,因为nginx是事件触发机制。软件包版本:libevent-2.0.16-stable.tar
  还需要注意的是预编译环境,在安装时会检测预编译环境的,如果出错,一定要检测自己自己的预编译环境。
  安装过程:
  # tar -zxvf nginx-1.4.4.tar.gz -C /usr/local/src
  # cd /usr/local/src/nginx-1.4.4/
  # groupadd -r nginx
  # useradd -r -g nginx -s /sbin/nologin -M nginx
  # yum install pcre-devel
  # tar -zxvf libevent-2.0.16-stable.tar.gz -C /usr/local/src
  # cd /usr/local/src/libevent-2.0.16-stable/
  # ./configure --prefix=/usr/local/libevent
  # make && make install
  # cd /usr/local/libevent
  # ll lib
  # vim /etc/ld.so.conf.d/libevent.conf//添加如下目录:
  /usr/local/libevent/lib
  # ldconfig /重新配置
  # ldconfig -pv |grep libevent 查看一下
  # cd /usr/local/src/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
  # yum install openssl-devel//出现错误,需要安装openssl-devel包
  # make && make install
  # cd /usr/local/nginx/sbin/
  # ./nginx –t/测试安装之后的配置文件是否有错,
  mkdir:cannot create directory `/var/tmp/nginx/client': No such file or directory
  # mkdir -pv/var/tmp/nginx/client
  # ./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 " && 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站点安全是基于模块实现的不同的安全设置需要不同的模块。
  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_nametec.abc.com;
  location / {
  root   /usr/local/nginx/tec;
  indexindex.html index.htm;
  }
  auth_basic      "a-hao";
  auth_badic_user_file    /usr/local/nginx/tec/.htpasswd;
  }

  这里需要我们知道.htpasswd文件的产生,需要httpd-tools软件包,所以需要在主机上安装这样软件包。
  # yum install httpd-tools
  #mkdir/usr/local/nginx/tec
  # cd /usr/local/nginx/tec
  # vim index.html //在文件添加一行显示就可以。比如:tec
  现在我们生成.htpasswd文件
  # htpasswd -c /usr/local/nginx/tec/.htpasswdaaa
  完成之后我们可以在浏览器中访问一下(实验环境虚拟机ip是:192.168.1.199)

  证明身份验证成功。
  2、来源控制
  来源控制是基于access模块实现的
  参考网站:http://nginx.org/en/docs/http/ngx_http_access_module.html
  配置如下:
  Access可以在server里引用,也可以在location中引用。
  为了便于实现的需要,我先安装一个文本编辑器------lynx
  vim/etc/nginx/nginx.conf /编辑nginx主配置文件添加

  lynx/etc/hosts中添加
  192.168.1.199www.abc.com
  192.168.1.199tec.abc.com
  完成之后你可以测试:
  root@ahao tec]# lynxhttp://tec.abc.com
  这个页面是需要经过身份验证的,测试中,成功的话会让你身份验证的。
  # vim /etc/nginx/nginx.conf

  # service nginx restart
  重启之后测试:(本地物理地址是192.168.1.105)

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

  这样就达到目的,物理机192.168.1.105可以访问服务,但是在虚拟机中192.168.1.199就不能访问服务器。
  3、加密访问
  加密访问使用的模块是:ssl
  参考网站:http://nginx.org/en/docs/http/ngx_http_ssl_module.html
  配置:
  CA服务器配置:
  # vim /etc/pki/tls/openssl.cnf
  首先我们要简单的配置ssl模块文件:
  根据文件中需要一个数据文件来记录证书颁发的情况。所以我们就建立这样的                        文件,
  # cd /etc/pki/CA
  # touch index.txt
  # touch serial //这是记录证书序号的文件,也需要手动建立
  # echo "01">serial //加入序号,以“01”开始
  在openssl.cnf文件中还需要更改证书颁发所允许的地区,要给成不受限的。
  countryName             = optional
  stateOrProvinceName   =optional
  organizationName      = optional
  还可以根据自己的需要更改默认颁发证书的国家,省份,城市。
  # openssl genrsa 1024 >private/cakey.pem //产生私钥文件
  # chmod 600 private/cakey.pem//更改权限
  //产生公钥文件
  # 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) :
  State or Province Name (full name) :
  Locality Name (eg, city) :
  Organization Name (eg, company) :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服务的私钥文件
  # mkdir -pv /etc/nginx/certs
  mkdir: created directory `/etc/nginx/certs'
  # cd /etc/nginx/certs/
  # openssl genrsa 1024 >nginx.key
  # chmod 600 nginx.key //更改权限
  # openssl req -new -keynginx.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) :
  State or Province Name (full name) :HENAN
  Locality Name (eg, city) :ZHENGZHOU
  Organization Name (eg, company) :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 []:
  # 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>  D1:DD:F8:9A:46:B1:01:1D:ED:B2:19:49:6C:7A:E8:48:47:CE:A9:CB

  X509v3 Authority Key>  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
  1 out of 1 certificate requests certified, commit? y
  Write out database with 1 new entries
  Data Base Updated
  # cat /etc/pki/CA/index.txt //查看是否有证书文件
  V       141130001754Z                01   unknown/C=CN/ST=HENAN/O=TUOYUANEDU/OU=tec/CN=tec.abc.com
  我们还需要将证书与web服务器绑定,
  # vim /etc/nginx/nginx.conf //在主配置文档中添加绑定项:

  我们现在启动一台windowsxp虚拟机,以它来访问web服务,事先在C:\windows\system32\drives\etc\hosts文件中添加192.168.1.199tec.abc.com
  设置之后我们可以访问试试:出现下面的情况

  出现这样的情况,是主机不能识别证书是哪个权威机构颁发的,就是不认可证书,这样的情况在nginx中有一种方法就是需要我们将CA服务器证书与web服务中的证书合成一张证书。
  # cp /etc/pki/CA/cacert.pem.///将CA证书拷贝过来
  # mv nginx.cert nginx.cert.bak//改名字便于区分,
  # cat nginx.cert.bakcacert.pem >nginx.cert   // 合成一张证书,合成名字还是原来WEB服务的证书名字。

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

  就可以正常访问了。

页: [1]
查看完整版本: nginx站点安全