Nginx (engine x) 是一个高性能的HTTP(解决C10k(万级并发)的问题)和反向代理服务器(
可以做负载均衡,动静分离技术,能把动态页面和静态页面分配到指定的服务器,能让整个网站的性能提升百分之20以上.)也是一个IMAP/POP3/SMTP(邮局)服务器。
Nginx的web优势:
IO多路复用:
理论方法:I/O多路复用 (单个线程,通过记录跟踪每个I/O流(sock)的状态,来同时管理多个I/O流 。)发明它的原因,是尽量多的提高服务器的吞吐能力。在同一个线程里面, 通过拨开关的方式,来同时传输多个I/O流
技术类型:
epoll,特点是异步,非阻塞:
每进来一个request,会有一个worker进程去处理。但不是全程的处理,处理到什么程度呢?处理到可能发生阻塞的地方,比如向上游(后端)服务器转发request,并等待请求返回。那么,这个处理的worker不会这么一直等着,他会在发送完请求后,
注册一个事件:“如果upstream返回了,告诉我一声,我再接着干”。于是他就休息去了。这就是异步。此时,如果再有request 进来,他就可以很快再按这种方式处理。这就是非阻塞和IO多路复用。
而一旦上游服务器返回了,就会触发这个事件,worker才会来接手,这个request才会接着往下走。这就是异步回调。
时分多路复用:
CPU时钟/中断设计(类似左手画方右手画圆,大脑高速切换。 类似开的程序多了鼠标一下切换到这,一下切换到那)
频分多路复用:
ADSL
yum安装:
/etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true安装:
yum install nginx -y
查看版本:
/usr/local/nginx/sbin/nginx -v
源码安装:
下载需要的源码包
wget -c http://nginx.org/download/nginx-1.18.0.tar.gz
安装依赖包
yum install gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y
解压安装包:
tar xf nginx-1.18.0.tar.gz
cd nginx-1.18.0/
编译:useradd -s /sbin/nologin nginx -M
mkdir -p /var/tmp/nginx/client/......................
能创建的文件提前创建好
./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/sbin/nginx --conf-path=/usr/local/nginx/conf/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_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/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre#编译安装
make && make install
使用systemctl 控制nginx服务:
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[Unit]
Description=nginx -high performance web server
Documentation=http://nginx.org/en/docs
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
WorkingDirectory=/usr/local/nginx
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
private Tmp=true
[Install]
WantedBy=multi-user.target
生产环境部署脚本使用systemctl控制服务:
chmod +x /usr/lib/systemd/system/nginx.service
systemctl daemon-reload
systemctl enable nginx.service
systemctl start nginx
systemctl status nginx
shell脚本函数控制nginx:
使用killall命令:
yum install psmisc
killall nginx
vim nginx.sh
#!/bin/bash
. /etc/init.d/functions
function usage() {
echo $"usage:$0 {start | stop | restart}"
exit 1
}
function start() {
/usr/local/nginx/sbin/nginx
sleep 1
if [ `netstat -antlpe | grep nginx | wc -l` -ge 0 ];then
action "nginx is started." /bin/true
else
action "nginx is started." /bin/false
fi
}
function stop() {
killall nginx &>/dev/null
sleep 1
if [ `netstat -antlpe | grep nginx | wc -l` -eq 0 ];then
action "nginx is stopped." /bin/true
else
action "nginx is stopped." /bin/false
fi
}
function main() {
if [ $# -ne 1 ];then
usage $0
fi
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
usage $0
;;
esac
}
main $*
使用脚本控制nginx
bash nginx.sh stop
bash nginx.sh start