Nginx网站服务(访问状态统计,基于授权和客户端的访问控制,基于域名、端口、IP的虚拟主机访问)

前言

  • 在各种网站服务器软件中,除了Apache HTTP Server外,还有一款轻量级的HTTP服务器软件–Nginx,其稳定,高效的特性逐渐被越来越多的用户认可
  • 其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名
  • 其特点是:占有内存少,并发能力强
  • 中国大陆使用nginx网站用户有:百度、京东新浪网易腾讯淘宝等。

一:Nginx服务基础

1.1:Nginx概述

  • 一款高性能、轻量级Web服务软件

    ●稳定性高

    ●系统资源消耗低

    ●对HTTP并发连接的处理能力高

    ●单台物理服务器可支持30 000 ~ 50000个并发请求

    ●占用内存少,并发能力强

1.2:Nginx编译安装

安装环境

CentOs 7.6 Nginx 1.15
mark

  • 安装支持软件
[root@localhost opt]# iptables -F
[root@localhost opt]# setenforce 0
[root@localhost opt]# mkdir LNMP
[root@localhost opt]# cd LNMP/
#把包移到opt目录中
[root@localhost LNMP]# rz -E
rz waiting to receive.
[root@localhost LNMP]# ls
Discuz_X3.4_SC_UTF8.zip    nginx-1.12.2.tar.gz
mysql-boost-5.7.20.tar.gz  php-7.1.10.tar.bz2
ncurses-5.6.tar.gz
[root@localhost LNMP]# tar zxvf nginx-1.12.2.tar.gz -C /opt
  • 安装环境依赖包
[root@localhost nginx-1.12.2]# yum -y install gcc gcc-c++ zlib-devel pcre pcre-devel 
  • 创建用户、组进行管理
-M:不创建家目录  -s:用于登录shell
[root@localhost nginx-1.12.2]# useradd -M -s /sbin/nologin nginx
[root@localhost nginx-1.12.2]# id nginx
uid=1001(nginx) gid=1001(nginx)=1001(nginx)
  • 编译安装
[root@localhost nginx-1.12.2]# ls
auto     CHANGES.ru  configure  html     man     src
CHANGES  conf        contrib    LICENSE  README
[root@localhost nginx-1.12.2]# ./con
conf/      configure  contrib/   
[root@localhost nginx-1.12.2]# ./configure \
> --prefix=/usr/local/nginx \                 #设置安装路径
> --user=nginx \                              #运行用户和组设为nginx
> --group=nginx \
> --with-http_stub_status_module             #启动模块来支持状态统计
#编译
[root@localhost nginx-1.12.2]# make && make install
  1. 路径优化
  • 为了是Nginx服务器的运行更加方便,可以为主程序nginx创建软链接文件,以便于管理员执行“nginx”命令就可以调用Nginx的主程序
[root@localhost nginx-1.12.2]# ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/
[root@localhost nginx-1.12.2]# ls /usr/local/bin/
nginx
#测试语法是否正确
[root@localhost nginx-1.12.2]# 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 nginx-1.12.2]# cd /usr/local/nginx/
[root@localhost nginx]# ls
client_body_temp  html        sbin
conf              logs        scgi_temp
fastcgi_temp      proxy_temp  uwsgi_temp
[root@localhost nginx]# cd conf/
#nginx.conf就是主配置文件
[root@localhost conf]# ls
fastcgi.conf            nginx.conf
fastcgi.conf.default    nginx.conf.default
fastcgi_params          scgi_params
fastcgi_params.default  scgi_params.default
koi-utf                 uwsgi_params
koi-win                 uwsgi_params.default
mime.types              win-utf
mime.types.default
#html是主页文件
[root@localhost conf]# cd ..
[root@localhost nginx]# ls
client_body_temp  html        sbin
conf              logs        scgi_temp
fastcgi_temp      proxy_temp  uwsgi_temp
[root@localhost nginx]# cd html/
[root@localhost html]# ls
50x.html  index.html
[root@localhost html]# netstat -anpt | grep nginx
##服务没有开启
  • 启动服务

    扫描二维码关注公众号,回复: 11837244 查看本文章
  • 直接运行Nginx即可启动Nginx服务器

[root@localhost html]# nginx
[root@localhost html]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      90530/nginx: master 
  • 宿主机访问一下

mark

  • 关闭进程可用pkill(关闭所有服务进程)
[root@localhost html]# pkill nginx
[root@localhost html]# netstat -anpt | grep nginx
  • 启动、重载配置、停止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		3显示"Welcome to nginx!"页面,表明Nginx服务已经正常运行'
[root@localhost ~]# killall -s HUP nginx	#-S选项指定信号种类,HUP信号表示重载配置'
[root@localhost ~]# killall -s QUIT nginx	#QUIT信号表示退出进程
  • Nginx添加为系统服务
[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]# vim /usr/local/nginx/conf/nginx.conf
####添加下面的图片内容

#在重启服务
[root@localhost init.d]# service nginx stop
[root@localhost init.d]# service nginx start
[root@localhost init.d]# 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 init.d]# netstat -ntap | grep nginx 
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      90311/nginx: master 

mark

  • 去宿主机测试
  • 刷新会进行统计
    在这里插入图片描述
    mark

三:配置虚拟主机

3.1:Nginx虚拟主机应用

  • Nginx支持的虚拟主机有三种

    ●基于域名的虚拟主机

    ●基于IP的虚拟主机

    ●基于端口的虚拟主机

  • 通过"server{}" 配置段实现

3.2:基于域名的虚拟Web主机

  • 准备网站目录及测试文件
[root@localhost init.d]# cd /var/
[root@localhost var]# ls
account  crash  games     lib    log   opt       spool   yp
adm      db     gopher    local  mail  preserve  target
cache    empty  kerberos  lock   nis   run       tmp
[root@localhost var]# mkdir www
[root@localhost var]# cd www/
#新建两个站点
[root@localhost www]# mkdir shuai mei
[root@localhost www]# ls
mei  shuai
[root@localhost www]# cd shuai/
[root@localhost shuai]# vim index.html
#添加以下内容
<h1>hell</h1>
[root@localhost shuai]# cd ../
[root@localhost www]# ls mei/
[root@localhost mei]# vim index.html
#添加网页内容
<h1>this is mei web</h1>

#下载tree
[root@localhost www]# yum -y install tree
[root@localhost www]# tree ./
./
├── mei
│   └── index.html
└── shuai
    └── index.html

2 directories, 2 files

#配置域名解析

[root@localhost www]# vim /etc/named.conf

在这里插入图片描述

#区域配置
在这里插入图片描述
配置区域数据

[root@localhost named]# cp -p named.localhost abc.com.zone
[root@localhost named]# vim abc.com.zone 

mark

[root@localhost named]# cp -p abc.com.zone ab.com.zone
#重启服务
[root@localhost named]# systemctl start named
  • 修改配置文件
[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf

mark

server {
    
    
       server_name www.shuai.com;
       location / {
    
    
         root /var/www/shuai;
         index index.html index.php;
       }
    }
    server {
    
    
       server_name www.shi.com;
       location / {
    
    
         root /var/www/shi;
         index index.html index.php;
       }
    }
  • 重启服务
[root@localhost named]# 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 named]# service nginx stop
[root@localhost named]# service nginx start
  • 客户机测试

mark

mark

3.3:基于端口的虚拟Web主机

  • 配置步骤

  • 修改配置文件

[root@localhost named]# vim /usr/local/nginx/conf/nginx.conf

server {
    
    
       server_name 20.0.0.41:8080;     ## 将域名修改为IP地址+端口
       listen 20.0.0.41:8080;          ## 增加这一行,内容为IP地址+监听端口
       location / {
    
    
         root /var/www/shuai;
         index index.html index.php;
       }
    }
    server {
    
    
       server_name 20.0.0.41:80;      ## 同上
       listen 20.0.0.41:80;           ## 同上
       location / {
    
    
         root /var/www/shi;
         index index.html index.php;
       }
    }

3.4:基于IP的虚拟主机配置

  • 添加一块网卡并配置一个IP地址(20.0.0.50)

编辑nginx配置文件

[root@localhost /]# vim /usr/local/nginx/conf/nginx.conf    ## 编辑配置文件

server {
    
    
       server_name 20.0.0.41:80;     ## IP地址+80端口
       listen 20.0.0.41:80;          ## IP地址+80端口
       location / {
    
    
         root /var/www/shuai;
         index index.html index.php;
       }
    }
    server {
    
    
       server_name 20.0.0.50:80;     ## 另一个IP地址+80端口
       listen 20.0.0.50:80;          ## 另一个IP地址+监听端口80
       location / {
    
    
         root /var/www/shi;
         index index.html index.php;
       }
    }

四:Nginx访问控制

4.1:基于授权的访问控制

  • 配置步骤与Apache基本一致

    ●生成用户密码认证文件

    ●修改主配置文件对相应目录,添加认证配置项

    ●重启服务,访问测试

  • 生成用户密码认证文件

[root@localhost shuai]# yum -y install httpd
[root@localhost shuai]# which htpasswd
/usr/bin/htpasswd
#创建用户
[root@localhost shuai]# htpasswd -c /usr/local/nginx/passwd.db liu
New password:                           #输入密码
Re-type new password:                   #再次输入密码
Adding password for user liu
#把账户树组变为nginx
[root@localhost shuai]# chown nginx /usr/local/nginx/passwd.db 
#设置权限
[root@localhost shuai]# chmod 400 /usr/local/nginx/passwd.db 
  • 修改主配置文件对相应目录,添加认证配置项
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf

mark

##验证语法
[root@localhost shuai]# 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 shuai]# service nginx stop
[root@localhost shuai]# service nginx start

客户机测试

mark

4.2:基于客户端的访问控制

  • 通过客户端IP地址,决定是否允许对页面访问

  • 配置规则

    deny IP/IP段:拒绝某个IP或IP段的客户端访问

    allow IP/IP段:允许某个IP或IP段的客户端访问

    规则从上往下执行,如匹配则停止,不再往下匹配

  • 配置步骤

    • 修改主配置文件nginx.conf,添加相应配置项
[root@localhost shuai]# vim /usr/local/nginx/conf/nginx.conf

mark

mark

mark

猜你喜欢

转载自blog.csdn.net/weixin_47151643/article/details/108033319