关于nginx初体验
一、Nginx 概述
Nginx 是一个网站架设软件,与 Apache 一样可以完成网站架设。由俄罗斯程序设计师
Igor Sysoev 开发。目前新浪、网易、腾讯、Plurk 等都在使用。
Nginx,发音为[engine x],专为性能优化而开发,其最知名的优点是它的稳定性和低系
统资源消耗,以及对 HTTP 并发连接的高处理能力,单台物理服务器可支持 30000~50000 个
并发请求。
正因如此,大量提供社交网络、新闻资讯、电子商务及虚拟主机等服务的企业纷纷选择
Nginx 来提供 Web 服务。
二、Nginx 优点
轻量级,同样架设 Web 服务,比 Apache 占用更少的内存及资源
高并发,Nginx 处理请求是异步非阻塞的,而 Apache 则是阻塞性的,在高并发下 Ngin能保存低资源消耗高性能
高度模块化的设计,编写模块相对简单
可作为负载均衡服务器,支持 7 层负载均衡
静态处理性能比 Apache 高 3 倍以上,但动态处理不足,需要通过后端服务器支持, 如 Apache
三、Nginx 的安装与控制
安装支持软件
[root@localhost ~]# yum -y install pcre-devel zlib-devel
[root@localhost ~]# yum -y install gcc gcc-c++ make
上传源码包安装 Nginx
[root@localhost ~]# ls
anaconda-ks.cfg initial-setup-ks.cfg nginx-1.6.0.tar.gz
创建运行用户,组
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tail -1 /etc/passwd
nginx❌1001:1001::/home/nginx:/sbin/nologin
[root@localhost ~]# tar xf nginx-1.6.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nginx-1.6.0/
编译安装 Nginx
[root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
–with-http_stub_status_module && make && make install
为主程序 nginx 创建链接文件
[root@localhost nginx-1.6.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
[root@localhost nginx-1.6.0]# ll /usr/local/sbin/nginx
lrwxrwxrwx. 1 root root 27 5月 3 15:43 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx //启动 nginx 服务
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 42920/nginx: master
killall -1 nginx //平滑重启 nginx (reload)
killall -s HUP nginx //平滑重启 nginx (reload)
killall -3 nginx //正常停止 nginx (stop)
killall -s QUIT nginx //正常停止 nginx (stop)
killall -s USR1 nginx //用于 nginx 的日志切换,也就是重新打开一个日志文
件,例如每天要生成一个日志文件时,可以使用这个信号来控制
killall -s USR2 nginx //用于平滑升级可执行程序
3、编写 nginx 服务脚本
[root@localhost ~]# vim /etc/init.d/nginx
#!/bin/bash
#chkconfig: 2345 99 20
#description: Nginx Server Control Scripts shell
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case “$1” in
start)
if [ -f $PIDF ];then
echo “Nginx is running…Start it is error”
else
$PROG
fi
;;
stop)
if [ -f $PIDF ];then
kill -3 $(cat $PIDF)
rm -f $PIDF
else
echo “Nginx is stopping…Stop it is error”
fi
;;
restart)
$0 stop
$0 start
;;
reload)
if [ -f $PIDF ];then
kill -1 $(cat $PIDF)
else
echo “Nginx is stopping…reload it is error”
fi
;;
status)
if [ -f $PIDF ];then
echo “Nginx is running”
else
echo “Nginx is stopped”
fi
;;
*)
echo “Usage:$0 (start|stop|restart|reload|status)”
exit 1
esac
exit 0
[root@localhost ~]# chmod +x /etc/init.d/nginx
[root@localhost ~]# /etc/init.d/nginx status
Nginx is stopped
[root@localhost ~]# /etc/init.d/nginx start
[root@localhost ~]# /etc/init.d/nginx start
Nginx is running…Start it is error
[root@localhost ~]# /etc/init.d/nginx status
Nginx is running
4、编写开机启动脚本
[root@localhost ~]# vim /lib/systemd/system/nginx.service
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true
#EnvironmentFile=/etc/sysconfig/rdisc
#ExecStart=/sbin/rdisc $RDISCOPTS
[Install]
WantedBy=multi-user.target
[root@localhost ~]# systemctl enable nginx.service
[root@localhost ~]# systemctl list-unit-files | grep nginx
nginx.service enabled
四、Nginx 配置文件
[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# cp nginx.conf nginx.conf.origin
[root@localhost conf]# vim nginx.conf在这里插入图片描述](https://img-blog.csdnimg.cn/20200310224014538.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDY5OTk5MQ==,size_16,color_FFFFFF,t_70)
在这里插入图片描述](https://img-blog.csdnimg.cn/20200310224147150.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NDY5OTk5MQ==,size_16,color_FFFFFF,t_70)
[root@localhost conf]# pwd
/usr/local/nginx/conf
[root@localhost conf]# mkdir …/html/mailcom
[root@localhost conf]# echo “
www.amber.com
” > …/html/index.html[root@localhost conf]# echo “
mail.amber.com
” > …/html/mailcom/index.html[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: [warn] 10240 worker_connections exceed open file resource limit: 1024
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# ulimit -n 65536
[root@localhost conf]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost conf]# /etc/init.d/nginx reload
49 location ~ /status {
50 stub_status on;
51 access_log off;
52 }
页面访问