一款高性能、轻量级Web服务软件
稳定性高
系统资源消耗低
对HTTP并发连接的处理能力高
单台物理服务器可支持30 000 ~ 50000个并发请求
占用内存少,并发能力强
[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@localhost ~]# useradd -M -S /sbin/nologin nginx '//-M 不创建家目录'
先mount.cifs挂载宿主机中的nginx软件包文件夹
[root@localhost ~]# tar zxvf nginx-1.12.2.tar.gz
[root@localhost ~]# cd nginx-1.12.0
[root@localhost nginx-1.12.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_ status_ module '//开启stub_status状态统计模块'
[root@localhost nginx-1.12.0]# make && make install
[root@localhost nginx-1.12.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin '//nginx命令执行路径优化'
[root@locaThost nginx-1.12.0]# ls -l /usr/local/sbin/nginx
Irwxrwxrwx 1root root27 5月16 16:50 /usr/local/sbin/nginx ->/usr/local/nginx/sbin/nginx
[root@localhost ~]# nginx -t '//检查'
[root@localhost ~]# nginx '//启动'
[root@localhost ~]# netstat -anpt | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7180/nginx: master
[root@localhost ~]# yum -y install elinks
[root@localhost ~]# elinks http://localhost '//显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx '//-S选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx '//QUIT信号表示退出进程'
Nginx添加为系统服务
第一种方法,使用systemctl工具进行管理
[root@localhost ~]# vim /lib/systemd/system/nginx.service '//添加使用systemctl工具进行管理'
[Unit]
Description=nginx '//描述'
After=network.target '//描述服务类别'
[Service]
Type=forking '//后台运行形势'
PIDFile =/usr/local/nginx/logs/nginx.pid '//PID文件位置'
ExecStart=/usr/local/nginx/sbin/nginx '//启动服务'
ExecReload=/usr/bin/kill -S HUP $MAINPID '//根据PID重载配置'
ExecStop=/usr/bin/kill -S QUIT $MAINPID '//根据PID终止进程'
PrivateTmp=true
[Install]
WantedBy=multi-user.target
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service
第二种方法,添加使用service工具进行管理
[root@localhost ~]# cd /etc/inid.d '//或者添加使用service工具进行管理'
[root@localhost init.d]# ls
[root@localhost init.d]# vim nginx
#!/bin/bash
# chkconfig: - 99 20
# description: Nginx Service Control Script
PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"
case "$1" in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop
$0 start
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "Usage: $0 {start|stop|restart|reload}"
exit 1
esac
exit 0
[root@localhost init.d]# chmod +x nginx
[root@localhost init.d]# chkconfig --add nginx
[root@localhost init.d]# chkconfig --level 35 nginx on
[root@localhost init.d]# service nginx start
[root@localhost init.d]# systemctl stop firewalld
[root@localhost init.d]# setenforce 0
[root@localhost init.d]# netstat -ntap | grep nginx
#user nobody;
worker_ processes 1;
#error_ log logs/error.log;
#pid logs/nginx.pid;
events {
use epoll;
worker connections 4096;
}
http {
....
access_log logs/access.log main;
sendfile on;
...
keepalive_ _timeout 65;
server {
listen 80;
server name localhost;
charset utf-8;
location / {
root html;
index index.html index.php; }
error_ page 500 502 503 504 /50x.html;
location = /50x.html {
root html; }}
}
启用HTTP_ STUB_ STATUS状态统计模块
配置编译参数时添加–with-http stub status module
nginx -V查看已安装的Nginx是否包含HTTP_ STUB _STATUS模块
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
配置步骤与Apache基本一致
生成用户密码认证文件
修改主配置文件对相应目录,添加认证配置项
重启服务,访问测试
生成用户密码认证文件
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
通过客户端IP地址,决定是否允许对页面访问
配置规则
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
配置步骤
修改主配置文件nginx.conf,添加相应配置项。
除主机192.168.195.128之外允许其他客户端访问
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
Nginx支持的虚拟主机有三种
基于域名的虚拟主机
基于IP的虚拟主机
基于端口的虚拟主机
通过"server{}" 配置段实现
配置步骤
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
配置步骤
修改配置文件
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module
配置步骤
主机配置两个IP地址
修改配置文件
[root@localhost ~]# nginx -V
nginx version: nginx/1.12.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx --user=nginx --
group= nginx --with-http_ stub_ status_ module