设为首页 收藏本站
查看: 1229|回复: 0

[经验分享] nginx站点的安全

[复制链接]

尚未签到

发表于 2018-11-16 07:39:54 | 显示全部楼层 |阅读模式
  nginx:
  支持高并发连接.官方测试的是5w并发连接但在实际生产中可制成2-4w并发连接数,得益于nginx使用最新的epoll(linux 2.6内核)和kqueue(freebsd)网络I/O模型,而apache使用的则是传统的select模型,其比较稳定的prefork模式为多进程模式 ,需要经常派生子进程,所消耗的CPU等服务器资源要比nginx高的多.
  select 和epoll效率差的原因:
  select是轮询、epoll是触发式的,所以在特定的应用上效率相差会很多
  Select:
  1.Socket数量限制:该模式可操作的Socket数由FD_SETSIZE决定,内核默认32*32=1024.
  2.操作限制:通过遍历FD_SETSIZE(1024)个Socket来完成调度,不管哪个Socket是活跃的,都遍历一遍.
  Poll:
  1.Socket数量几乎无限制:该模式下的Socket对应的fd列表由一个数组来保存,大小不限(默认4k).
  2.操作限制:同Select.
  Epoll:
  1.Socket数量无限制:同Poll
  2.操作无限制:基于内核提供的反射模式,有活跃Socket时,内核访问该Socket的callback,不需要遍历轮询. 但是当所有Socket都活跃的时候,这时候所有的callback都被唤醒, 会导致资源的竞争.既然都是要处理所有的Socket, 那么遍历是最简单最有效的实现方式.
  需要用到的安装包:
  nginx-1.4.4.tar.gz 可到官网http://nginx.org/en/download.html 下载
  libevent-2.0.21-stable.tar.gz http://pan.baidu.com/s/1kTfWZqf
  一、环境:
  1.编译环境
  "Development tools"
  "Additional Development"
  2.系统环境:
  nginx服务器: CentOS-6.4-x86_64  192.168.80.10
  客户机:windows7,windows server 2003 192.168.80.1
  二、安装nginx:
  [root@localhost ~]# tar -zxvf nginx-1.4.4.tar.gz -C /usr/local/src/
  解压安装包
  安装帮助可见官方网站: http://nginx.org/en/docs/configure.html
  [root@localhost nginx-1.4.4]# groupadd -r nginx
  [root@localhost nginx-1.4.4]# useradd -r -g nginx -s /sbin/nologin -M  nginx
  创建一个nginx组,创建一个系统账号nginx并加入nginx组中,-M表示不再创建其他目录
  需要使用pcre的库,所以要安装pcre-devel
  [root@localhost nginx-1.4.4]# yum --disablerepo=* --enablerepo=c6-media install pcre-devel
  然后需要安装libevent事件库:
  [root@localhost ~]# tar -zxvf libevent-2.0.21-stable.tar.gz -C /usr/local/src/
  [root@localhost ~]# cd /usr/local/src/libevent-2.0.21-stable/
  [root@localhost libevent-2.0.21-stable]# ./configure --prefix=/usr/local/libevent 指明安装路径
  [root@localhost libevent-2.0.21-stable]# make && make install
  在/etc/ld.so.conf.d/下建立一个libevent.conf文件,并写入一下内容,这是为了把libevent生成的库应用到系统中:
  [root@localhost libevent]# vim /etc/ld.so.conf.d/libevent.conf
  /usr/local/libevent/bin
  [root@localhost libevent]# ldconfig 刷新缓存
  接下来来到nginx的目录下,安装nginx:
  [root@localhost libevent]# cd /usr/local/src/nginx-1.4.4/
  [root@localhost 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@localhost nginx-1.4.4]# make && make install
  来到nginx的安装目录:
  [root@localhost nginx]# cd /usr/local/nginx/sbin/
  使用-t来测试配置文件:
  [root@localhost sbin]# ./nginx -t
  发现测试结果如下:
DSC0000.jpg

  少了一个临时目录:
  [root@localhost sbin]# mkdir -pv /var/tmp/nginx/client
  创建完成后测试成功!
  [root@localhost sbin]# ./nginx  运行nginx
  创建控制脚本:
  [root@localhost sbin]# cd /etc/init.d
  [root@localhost init.d]# vim nginx
  [root@localhost init.d]# chmod a+x nginx 给它执行权限
  脚本内容如下:
  #!/bin/bash
  prog=/usr/local/nginx/sbin/nginx
  lockfile=/var/lock/nginx.lock
  start () {
  if [ -e $lockfile ];then
  echo "the nginx server is started" && exit
  else
  echo -n "the nginx server is starting......"
  sleep 1
  $prog && echo ok && touch $lockfile || echo "fail"
  fi
  }
  stop () {
  if [ ! -e $lockfile ];then
  echo "the nginx server is stoped " && exit
  else
  echo -n "the nginx server is stopping...."
  $prog -s stop && echo ok && rm -rf $lockfile
  fi
  }
  configtest () {
  $prog -t
  }
  case "$1" in
  start)
  start
  ;;
  stop)
  stop
  ;;
  restart)
  stop
  start
  ;;
  configtest)
  configtest
  ;;
  *)
  echo "USAGE:start|stop|restart"
  esac
  但是当执行stop命令的时候发现出错,
  [root@localhost init.d]# service nginx stop
  the nginx server is stopping....nginx: [error] invalid PID number "" in "/var/run/nginx/nginx.pid"
  此时编辑/var/run/nginx/nginx.pid,在其中写入“01”,保存后退出,再次执行以上命令时没有出错。
  [root@localhost init.d]# vim /var/run/nginx/nginx.pid
  启动nginx了之后,在浏览器中输入http://192.168.80.10会看到如下界面:
DSC0001.jpg

  到这里nginx安装完毕!
  三、来做一些基本的测试:
  1.先来做一个关于基于ip地址虚拟主机的试验:
  在本机上添加一个ip地址:
  [root@localhost ~]# ifconfig eth0:0 192.168.80.11
  然后在/usr/local/nginx/目录下创建tec文件夹,并添加一个静态网页index.html,写入内容"tec"
  [root@localhost nginx]# mkdir tec
  [root@localhost nginx]# echo "tec" >tec/index.htm
  来到客户机上修改C:\Windows\System32\drivers\etc下的hosts文件,添加如下内容
  192.168.80.10 www.tyedus.com
  192.168.80.11 tec.tyedus.com
  然后来编辑nginx的配置文件:
  [root@localhost init.d]# vim /etc/nginx/nginx.conf
  将配置文件中的第35行至47行拷贝一下,粘贴在第80行,然后分别作如下配置:
DSC0002.jpg

DSC0003.jpg

  完成后保存退出。
  重启nginx服务:
  (如果显示错误的话,可用pkill杀掉nginx进程,并手动删除/var/lock/nginx.lock后再启动nginx)
  然后在客户机上分别访问:http://www.tyedus.com:
DSC0004.jpg

  http://tec.tyedus.com:
DSC0005.jpg

  2.再来做一个关于基于端口虚拟主机的试验:
  先把另一个地址给关掉:
  [root@localhost ~]# ifconfig eth0:0 down
  80端口和800端口分别对应不同的网页:
  192.168.2.101  /usr/local/nginx/html   www.tyedus.com  80
  192.168.2.101    /usr/local/nginx/tec    www.tyedus.com  800
  然后来编辑nginx的配置文件:
  [root@localhost init.d]# vim /etc/nginx/nginx.conf
  编辑如下:
  然后重启nginx服务:
  [root@localhost init.d]# service nginx restart
  然后在客户机中分别访问:
  http://192.168.80.10 (默认是80端口)
  http://192.168.80.10:800 结果如下:
  接下来用nginx来做对apache站点的安全控制:
  http://wiki.nginx.org/Modules   这是官网上的对模块的帮助文档
  http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html   身份验证
  http://nginx.org/en/docs/http/ngx_http_access_module.html   访问控制
  3.然后来做一个基于主机头虚拟主机的试验,并实现身份认证:
  编辑nginx的配置文件:
  [root@localhost init.d]# vim /etc/nginx/nginx.conf
  编辑如下:
DSC0006.jpg

  来到客户机上修改C:\Windows\System32\drivers\etc下的hosts文件,添加如下内容
  192.168.80.10 www.tyedus.com
  192.168.80.10 tec.tyedus.com
  保存后退出,并重新启动nginx服务:
  要生成密钥文件需要一个httpd-tools工具,若系统中没有可以自行安装:
  [root@localhost ~]# yum --disablerepo=* --enablerepo=c6-media install httpd-tools
  安装好之后,
  [root@localhost ~]# htpasswd -c /usr/local/nginx/tec/.htpasswd user1
  创建.htpassword文件,并写入一个账号名为user1,然后输入密码。
  再次访问http://tec.tyedus.com,会出现如下界面,需要身份验证;
DSC0007.jpg

  4.使用CA证书技术来实现对nginx站点的安全控制:
  [root@localhost ~]# cd /etc/pki/
  [root@localhost pki]# vim tls/openssl.cnf
DSC0008.jpg

  如果有些文件/目录没有的话就自己创建一个:
  将match改为optional
DSC0009.jpg

  修改一些默认值,以免每次请求产生证书都要填写,当然也可以不该,在请求时自行填写:
  在这里还需要填写一些别的资料,(我这里都是随便填写的):
DSC00010.jpg

  编辑完成后保存退出:
  [root@localhost CA]# touch index.txt
  [root@localhost CA]# touch serial
  [root@localhost CA]# echo "01" >serial
  [root@localhost CA]# vim ../tls/openssl.cnf
  [root@localhost CA]# openssl genrsa 1024 >private/cakey.pem
  在当前目录的private目录下用openssl产生一个用rsa加密算法加密的1024为的私钥,文件名称为cakey.pem(这个文件名称必须和openssl.conf里的文件名相同)
  [root@localhost CA]# chmod 600 private/cakey.pem
  因为私钥是保密的,只能由管理员一个人可以访问所以改变它的权限
  [root@localhost CA]# openssl req -new -key private/cakey.pem -x509 -out cacert.pem
  请求产生一个证书,证书上的公钥从私钥上提取,证书格式为-x509,名称为cacert.pem(这个文件名称必须和openssl.conf里的文件名称相同
DSC00011.jpg

  [root@localhost CA]# mkdir -pv /etc/nginx/certs
  [root@localhost CA]# cd /etc/nginx/certs/
  [root@localhost certs]# openssl genrsa 1024 >nginx.key
  [root@localhost certs]# chmod 600 nginx.key
  [root@localhost certs]# openssl req -new -key nginx.key -out nginx.crq
  有了请求文件之后,可以基于请求文件来申请一份证书,产生的证书名称为nginx.crq
DSC00012.jpg

  [root@localhost certs]# openssl ca -in nginx.crq -out nginx.cert
DSC00013.jpg

  图
  编辑nginx的配置文件:
  [root@localhost init.d]# vim /etc/nginx/nginx.conf
  编辑如下:
DSC00014.jpg

  保存后退出,重新启动nginx服务:
  [root@localhost certs]# service nginx restart
  这里我用了一台windows server 2003作为客户机,ip地址为192.168.80.1
  修改C:\Windows\System32\drivers\etc下的hosts文件,添加一条“192.168.80.10 tec.tyedus.com”
  然后在浏览器中访问:https://tec.tyedus.com
  会发现证书不可信:
  回到服务器,把服务器的证书和CA的证书合在一起,拼成一个证书:
  [root@localhost certs]# cp /etc/pki/CA/cacert.pem  ./
  把CA的证书拷至当前目录
  [root@localhost certs]# mv nginx.cert nginx.cert.bak
  把服务器的证书重命名为nginx.cert.bak
  [root@localhost certs]# cat nginx.cert.bak cacert.pem >nginx.cert
  把两个证书内容都写入到一个nginx.cert中,作为新的证书。
  [root@localhost certs]# service nginx restart
  重启服务后再次访问,已经可以正常访问。


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-635554-1-1.html 上篇帖子: Nginx-TFS install 下篇帖子: 使用yum安装nginx-benzyi
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表