Nginx 介绍
常见的 WebServer
- 老牌:httpd(Apache),开源,市场份额较高
- 微软:IIS
- 轻量:Lighttpd/Caddy,性能高,低耗能,功能欠缺
Nginx 诞生
- 2004 年 10 月发布,俄国人 Igor Sysoev 开发
Nginx 官网/版本
- http://nginx.org/ 目前稳定版 1.16.1
- 国内分支 Tengine(http://tengine.taobao.org/)
Nginx 功能介绍
- Http 服务,反向代理,负载均衡,邮件代理,缓存加速,SSL,FLV/MP4 流媒体
Nginx 安装(yum 安装)
安装
vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=0
enabled=1
yum -y install nginx
systemctl start/stop/restart/reload nginx
测试
浏览器访问或者 curl 访问
检查服务进程:ps aux | grep nginx
检查端口监听:netstart -lntp | grep ":80"
有防火墙,则需要增加规则:iptables -I INPUT -p tcp --dport 80 -j ACCEPT
nginx -V 查看版本以及各个目录/参数
Nginx 安装(源码安装)
安装
yum -y install gcc pcre-devel.x86_64 openssl-devel.x86_64 zlib-devel
cd /usr/local/src/
wget http://nginx.org/download/nginx-1.16.1.tar.gz
tar xvf nginx-1.16.1.tar.gz
cd nginx-1.16.1/
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio
make && make install
配置
vim /etc/init.d/nginx
#!/bin/bash
# chkconfig: - 30 21
# description: http service.
# Source Function Library
. /etc/init.d/functions
# Nginx Settings
NGINX_SBIN="/usr/local/nginx/sbin/nginx"
NGINX_CONF="/usr/local/nginx/conf/nginx.conf"
NGINX_PID="/usr/local/nginx/logs/nginx.pid"
RETVAL=0
prog="Nginx"
start()
{
echo -n $"Starting $prog: "
mkdir -p /dev/shm/nginx_temp
daemon $NGINX_SBIN -c $NGINX_CONF
RETVAL=$?
echo
return $RETVAL
}
stop()
{
echo -n $"Stopping $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -TERM
rm -rf /dev/shm/nginx_temp
RETVAL=$?
echo
return $RETVAL
}
reload()
{
echo -n $"Reloading $prog: "
killproc -p $NGINX_PID $NGINX_SBIN -HUP
RETVAL=$?
echo
return $RETVAL
}
restart()
{
stop
start
}
configtest()
{
$NGINX_SBIN -c $NGINX_CONF -t
return 0
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
restart)
restart
;;
configtest)
configtest
;;
*)
echo $"Usage: $0 {start|stop|reload|restart|configtest}"
RETVAL=1
esac
exit $RETVAL
chmod 755 /etc/init.d/nginx
chkconfig --add nginx
chkconfig nginx on
测试
浏览器访问或者 curl 访问
检查服务进程:ps aux | grep nginx
检查端口监听:netstart -lntp | grep ":80"
有防火墙,则需要增加规则:iptables -I INPUT -p tcp --dport 80 -j ACCEPT
pkill nginx:杀死 nginx 进程,停止 nginx 服务
检测配置文件的语法错误:/usr/local/nginx/sbin/nginx -t
重载配置:/usr/local/nginx/sbin/nginx -s reload