nginx介绍
php
nginx是一个轻量级高性能的HTTP服务、反向代理服务和电子邮件(IMAP\POP3)代理服务器。nginx
优势:c++
做为Web服务器:相比Apache而言,nginx使用更少的资源,支持更多的并发链接,体现更高的效率;nignx采用epoll and kqueue做为开发模型,可以支持高达50000个并发链接数的响应。数据库
做为负载均衡器:Nginx既能够在内部直接支持Rails和php,也能够支持做为HTTP代理服务器和对外进行服务vim
做为邮件代理服务器:Nginx同时也是一个很是优秀的邮件代理服务器(最先开发这个产品的目的之一也是做为邮件代理服务器)浏览器
Nginx安装简单,配置文件简介(支持perl语法)bash
使用yum安装nginx服务器
#关闭防火墙 [root@y-nginx ~]# systemctl stop firewalld [root@y-nginx ~]# setenforce 0 #配置yum源 [root@y-nginx ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@y-nginx ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@y-nginx ~]# yum repolist [root@y-nginx ~]# yum install -y nginx [root@y-nginx ~]# systemctl start nginx [root@y-nginx ~]# systemctl enable nginx
#确认ip,在浏览器访问该ip地址,出现如下页面表示安装成功并发
#查看nginx安装版本 [root@y-nginx ~]# nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled
nginx编译安装与配置使用
负载均衡
#安装编译环境 [root@b-nginx ~]# yum install -y gcc gcc-c++ #安装pcre软件包(使nginx支持http rewrite模块) yum install -y pcre pcre-devel #安装openssl-devel(使nginx支持ssl) yum install -y openssl openssl-devel #安装zlib yum install -y zlib zlib-devel #建立用户nginx [root@b-nginx ~]# useradd nginx [root@b-nginx ~]# passwd nginx Changing password for user nginx. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.
安装nginx
[root@b-nginx ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@b-nginx ~]# ls anaconda-ks.cfg nginx-1.14.2.tar.gz [root@b-nginx ~]# tar xf nginx-1.14.2.tar.gz -C /usr/local [root@b-nginx ~]# cd /usr/local [root@b-nginx local]# cd nginx-1.14.2/
[root@b-nginx nginx-1.14.2]# ./configure \--group=nginx \--user=nginx \--prefix=/usr/local/nginx \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--http-client-body-temp-path=/tmp/nginx/client_body \--http-proxy-temp-path=/tmp/nginx/proxy \--http-fastcgi-temp-path=/tmp/nginx/fastcgi \--pid-path=/var/run/nginx.pid \--lock-path=/var/lock/nginx \--with-http_stub_status_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-pcre
#模块功能具体参数 --prefix=/usr/local/nginx //指向安装目录 --conf-path=/etc/nginx/nginx.conf //指定配置文件 --http-log-path=/var/log/nginx/access.log //指定访问日志 --error-log-path=/var/log/nginx/error.log //指定错误日志 --lock-path=/var/lock/nginx.lock //指定lock文件 --pid-path=/run/nginx.pid //指定pid文件 --http-client-body-temp-path=/var/lib/nginx/body //设定http客户端请求临时文件路径 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi //设定http fastcgi临时文件路径 --http-proxy-temp-path=/var/lib/nginx/proxy //设定http代理临时文件路径 --http-scgi-temp-path=/var/lib/nginx/scgi //设定http scgi临时文件路径 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi //设定http uwsgi临时文件路径 --with-debug //启用debug日志 --with-pcre-jit //编译PCRE包含“just-in-time compilation” --with-ipv6 //启用ipv6支持 --with-http_ssl_module //启用ssl支持 --with-http_stub_status_module //获取nginx自上次启动以来的状态 --with-http_realip_module //容许从请求标头更改客户端的IP地址值,默认为关 --with-http_auth_request_module //实现基于一个子请求的结果的客户端受权。若是该子请求返回的2xx响应代码,所述接入是容许的。若是它返回401或403中,访问被拒绝与相应的错误代码。由子请求返回的任何其余响应代码被认为是一个错误。 --with-http_addition_module //做为一个输出过滤器,支持不彻底缓冲,分部分响应请求 --with-http_dav_module //增长PUT,DELETE,MKCOL:建立集合,COPY和MOVE方法 默认关闭,需编译开启 --with-http_geoip_module //使用预编译的MaxMind数据库解析客户端IP地址,获得变量值 --with-http_gunzip_module //它为不支持“gzip”编码方法的客户端解压具备“Content-Encoding: gzip”头的响应。 --with-http_gzip_static_module //在线实时压缩输出数据流 --with-http_image_filter_module //传输JPEG/GIF/PNG 图片的一个过滤器)(默认为不启用。gd库要用到) --with-http_spdy_module //SPDY能够缩短网页的加载时间 --with-http_sub_module //容许用一些其余文本替换nginx响应中的一些文本 --with-http_xslt_module //过滤转换XML请求 --with-mail //启用POP3/IMAP4/SMTP代理模块支持 --with-mail_ssl_module //启用ngx_mail_ssl_module支持启用外部模块支持
[root@b-nginx nginx-1.14.2]# make && make install
#查看nginx版本和安装的模块信息
#查看配置文件/etc/nginx/nginx.conf
#检测nginx配置文件 [root@b-nginx ~]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #启动nginx服务 [root@b-nginx ~]# /usr/sbin/nginx
#nginx控制命令
#修改配置后从新加载生效 nginx -s reload #从新打开日志文件 nginx -s reopen #中止nginx服务 nginx -s stop #完整有序的中止nginx nginx -s quit #测试当前配置文件是否正确 nginx -t
#实现nginx开机自启
#添加启动脚本 [root@b-nginx ~]# vim /etc/init.d/nginx #!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac #添加脚本权限 [root@b-nginx ~]# chmod +x /etc/init.d/nginx #重载系统启动文件 [root@b-nginx ~]# systemctl daemon-reload #设置开机自启 [root@b-nginx ~]# systemctl start nginx