Nginx介绍
Nginx是一个轻量级高性能的HTTP服务、反向代理服务和电子邮件(IMAP\POP3)代理服务器。
优点:
作为Web服务器:相比Apache而言,Nginx使用更少的资源,支持更多的并发连接,体现更高的效率;nignx采用epoll and kqueue作为开发模型,能够支持高达50000个并发连接数的响应。
作为负载均衡器:Nginx既可以在内部直接支持Rails和PHP,也可以支持作为HTTP代理服务器和对外进行服务
作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器)
使用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版本和安装的模块信息
#检测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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。