一款高性能、轻量级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 不创建家目录'
编译安装Nginx
先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 '//检查'
启动、重载配置、停止Nginx
[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
此时,开启服务,关闭防火墙,就可以访问nginx网址了
[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;
1/O时间配置
events {
use epoll;
worker connections 4096;
}
HTTP配置
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
修改nginx.conf配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
http{
server {
listen 80;
server name localhost;
charset utf-8;
location / {
root html;
index index.html index.php;
}
location ~/status { '//添加此段'
stub_ status on;
access_ log off;
}
}
}
查看当前的状态统计信息
配置步骤与Apache基本一致
●生成用户密码认证文件
●修改主配置文件对相应目录,添加认证配置项
●重启服务,访问测试
生成用户密码认证文件
[root@localhost ~]# yum install -y httpd-tools '//因为没有htpasswd工具,所以需要安装'
[root@localhost ~]# htpasswd -c /usr/local/nginx/passwd.db test
New password:
Re-type new password:
Adding password for user test
root@localhost ~]# cat /usr/local/nginx/ passwd.db
test:$apr1$x.UaSXIM$RRLa2KJcKwsGBVsikGcrR/
修改主配置文件对相应目录,添加认证配置项
[root@localhost ~]# chmod 400 /usr/local/nginx/passwd.db
[root@localhost ~]# chown nginx /usr/local/nginx/passwd.db
[root@localhost ~]# ll -d /usr/local/nginx/passwd.db
-r------- 1 nginx root43 5月16 22:26
/usr/local/nginx/passwd.db
重启服务,访问测试
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
location / {
auth_basic "secret";
auth_basic_user_file /usr/local/nginx/passwd.db;
}
此时访问网页会要求输入账号密码
通过客户端IP地址,决定是否允许对页面访问
配置规则
deny IP/IP段:拒绝某个IP或IP段的客户端访问
allow IP/IP段:允许某个IP或IP段的客户端访问
规则从上往下执行,如匹配则停止,不再往下匹配
配置步骤
Nginx支持的虚拟主机有三种
●基于域名的虚拟主机
●基于IP的虚拟主机
●基于端口的虚拟主机
通过"server{}" 配置段实现
配置步骤
●准备网站目录及测试文件
[root@localhost ~]# mkdir -p /var/www/html/erbao/
[root@localhost ~]# mkdir -p /var/www/html/sanbao/
[root@localhost ~]# echo "this is erbao" >>
/var/www/html/erbao/index.html
[root@localhost ~]# echo "this is sanbao" >>
/var/www/html/sanbao/index.html
[root@localhost ~]# yum install bind -y '//以下是配置DNS服务过程,具体内容不再赘述,如果不懂,翻阅我之前博客'
[root@localhost ~]# vim /etc/named.conf
[root@localhost ~]# vim /etc/named.rfc1912.zones
[root@localhost ~]# cp -p /var/named/named.localhost /var/named/erbao.com.zone
[root@localhost ~]# vim /var/named/erbao.com.zone
[root@localhost ~]# cp -p /var/named/erbao.com.zone /var/named/sanbao.com.zone
[root@localhost ~]# systemctl start named
●调整nginx.conf配置文件
server {
listen 80;
server name www.erbao.com;
charset utf-8;
access_log logs/www.erbao.access.log;
location/ {
root /var/www/html/btcom;
......
}
这个简单,和通过域名访问即可
配置步骤
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.9.158:6666;
server name 192.1 68.9.1 58:6666;
......}
server {
listen 192.1 68.9.158:8888;
server name 192.1 68.9.158:8888;
......}
配置步骤
主机配置两个IP地址
修改配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 192.168.9.1 58:80;
server name 192.168.9.158:80;
....}
server {
listen 192.168.9.1 10:80;
server name 192.168.9.110:80;
....}