【转自asd1123509133的博客】1.背景介绍:Nginx是一个高性能的HTTP和反向代理服务器,可以选择高效的epoll(linux2.6kernel)、kqueue(freebsd)、eventport(solaris10)作为网络I/O模型,最高可支持50000个并发连接,同时对内存、CPU等系统资源的消耗非常低,运行非常稳定。选择理由:*支持高并发连接:nginx采用高效的多路复用模型(epoll/linux,kqueue/freebsd,eventport/solaris)*内存消耗少:服务器3W并发连接下,10个Nginx进程消耗150MB内存(15MB*10)*低成本:购买F5BIG-IP、NetScaler等负载均衡交换机需要几十万人民币,开源Nginx替代了这些商用设备。*其他原因:网络配置简单;支持rewrite重写规则,可以根据不同的域名和URL将HTTP请求分成不同的后端服务器组;内置健康检查功能;节省带宽,支持GZIP压缩,可以添加浏览器本地缓存的Header;支持热部署,可以在不中断服务的情况下升级软件版本应用范围:*Web服务:设置多虚拟主机服务,配合fast-cgi或tomcat支持动态网页Nginx是国内流行的www服务软件最近几年。它与Apache、lighttpd和tomcat具有相似的功能,但nginx比前者有更优越的性能,例如:它采用epoll模型,具有内存消耗小的优点;*反向代理,Proxyformulti-virtualhosts:指使用代理服务器接受互联网上的连接请求,然后将请求转发给内网的服务器,并将服务器得到的结果返回给内网请求连接的客户端互联网;*七层负载均衡:单多虚拟主机的不同服务器之间访问;负载均衡是由多台服务器以对称方式组成的服务器集合,每台服务器处于同等位置,通过一定的负载分担技术,将外部请求以对称结构平均分配到某台服务器上,并且接收请求的服务器独立响应客户端的请求;*正向代理:作用于Internet,将内部网络的连接请求代理到Internet,客户端必须指定一个代理服务器,并将本应直接发送给web服务器的http请求发送给代理服务器,代理服务器会请求并返回响应内容;*缓存服务是对proxy和fastcgi的缓存服务,提高访问速度,相当于squid的功能;2.环境[root@nginx~]#cat/etc/redhat-releaseCentOSrelease6.8(Final)[root@nginx~]#uname-r2.6.32-504.el6.x86_643.installation*暂时关闭selinux(可选)[root@nginx~]#setenforce0*关闭iptables(可选)[root@nginx~]#serviceiptablesstop*创建www用户[root@nginx~]#useradd-r-s/sbin/nologin-Mwww*安装pcre库依赖[root@nginx~]#yuminstallpcrepcre-devel-y*安装ssl库依赖[root@nginx~]#yuinstallopensslopenssl-devel-y*进入下载目录cd/usr/local/src*下载nginx源码包wgethttp://nginx.org/download/nginx-1.11.10.tar.gz*解压nginx源码包tarzxvfnginx-1.11.10.tar.gz*进入nginx包目录cdnginx-1.11.10*指定安装目录,用户,模块[root@nginx~]#./configure--prefix=/usr/local/nginx-1.11.10--user=www--group=www--with-http_ssl_module--with-http_stub_status_module*编译安装[root@nginx~]#make&&makeinstall*制作nginx软链接[root@nginx~]#ln-s/usr/local/nginx-1.11.10/usr/local/nginx4.创建启动脚本*/etc/init.d/nginx#!/bin/sh##nginx-thisscriptstartsandstopshenginxdaemon##chkconfig:-8515#description:NGINXisanHTTP(S)server,HTTP(S)reverse\#proxyandIMAP/POP3proxyserver#processname:nginx#config:/usr/local/nginx/conf/nginx.conf#config:/etc/sysconfig/nginx#pidfile:/var/run/nginx.pid#Sourcefunctionlibrary../etc/rc.d/init.d/functions#Sourcenetworkingconfiguration../etc/sysconfig/network#Checkthatnetworkingisup.["$NETWORKING"="no"]&&exit0nginx="/usr/local/nginx/sbin/nginx"prog=$(basename$nginx)NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"[-f/etc/sysconfig/nginx]&&./etc/sysconfig/nginxlockfile=/var/lock/subsys/nginxmake_dirs(){#makerequireddirectoriesuser=`$nginx-V2>&1|grep"configurearguments:"|sed's/[^*]*--user=\([^]*\).*/\1/g'-`if[-z"`grep$user/etc/passwd`"];thenuseradd-M-s/bin/nologin$userfioptions=`$nginx-V2>&1|grep'configurearguments:'`foroptin$options;doif[`echo$opt|grep'.*-temp-path'`];thenvalue=`echo$opt|cut-d"="-f2`if[!-d"$value"];then#echo"creating"$valuemkdir-p$value&&chown-R$user$valuefifidone}start(){[-x$nginx]||exit5[-f$NGINX_CONF_FILE]||exit6make_dirsecho-n$"启动$prog:"daemon$nginx-c$NGINX_CONF_FILEretval=$?echo[$retval-eq0]&&touch$lockfilereturn$retval}stop(){echo-n$"Stopping$prog:"killproc$prog-QUITretval=$?echo[$retval-eq0]&&rm-f$lockfilereturn$retval}res开始(){$nginx-t-c$NGINX_CONF_FILE}rh_status(){status$prog}rh_status_q(){rh_status>/dev/null2>&1}case"$1"instart)rh_status_q&&exit0$1;;stop)rh_status_q||exit0$1;重启|configtest)$1;;reload)rh_status_q||exit7$1;;force-reload)force_reload;;status)rh_status;;condrestart|try-restart)rh_status_q||exit0;;*)echo$"Usage:$0{start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"exit2esac*更改nginx脚本文件权限[root@nginx~]#chmod755/etc/init.d/nginx*添加到服务管理service并设置开机启动[root@nginx~]#chkconfig--addnginx[root@nginx~]#chkconfignginxon5.服务启动测试[root@nginx~]#servicenginxstart可以看到80默认80端口nginx已经开始监听6、访问测试*通过浏览器测试,nginx主机ip为192.168.222.128,访问成功,nginx成功返回到7页。总结需求驱动技术,技术本身没有区别,只商业
