理论+实验——Nginx服务(Nginx安装、Nginx访问控制)

前言

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

一、Nginx服务基础

1.1、关于Nginx

概述:
Nginx是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP代理服务器。
Nginx是一款轻量级的Web服务器/反向代理服务器以及电子邮件代理服务器,并在一个BSD-like协议下发行。由俄罗斯的程序设计师lgor Sysoev所开发,供俄国大型的入口网站及搜索引擎Rambler使用。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好。
Nginx相较于Apache\lighttpd具有占有内存少,稳定性高等优势,并且依靠并发能力强,丰富的模块库以及友好灵活的配置而闻名。在Linux操作系统下,nginx使用epoll事件模型,得益于此,nginx在Linux操作系统下效率相当高。同时Nginx在OpenBSD或FreeBSD操作系统上采用类似于Epoll的高效事件模型kqueue.
—款高性能、轻量级Web服务软件

  • 稳定性高
  • 系统资源消耗低
  • 对HTTP并发连接的处理能力高
  • 单台物理服务器可支持3万~ 5万个并发请求
  • 占用内存少,并发能力强

1.2、安装Nginx

[root@localhost ~]# yum -y install gcc gcc-c++ make pcre-devel zlib-devel
[root@localhost ~]# useradd -M -s /sbin/nologin nginx
[root@localhost ~]# tar xf nginx-1.15.9.tar.gz
[root@localhost ~]# cd nginx-1.15.9/
[root@localhost nginx-1.15.9]# ./configure \
> --prefix=/usr/local/nginx \
> --user=nginx \
> --group=nginx \
> --with-http_stub_status_module
[root@localhost nginx-1.15.9]# make -j3
[root@localhost nginx-1.15.9]# make install
[root@localhost nginx-1.15.9]# ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin
[root@localhost nginx-1.15.9]# ls -l /usr/local/sbin/nginx
lrwxrwxrwx 1 root root 27 Sep  3 02:59 /usr/local/sbin/nginx -> /usr/local/nginx/sbin/nginx
[root@localhost nginx-1.15.9]# 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.15.9]# nginx
[root@localhost nginx-1.15.9]# netstat -anpt | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      78877/nginx: master 
[root@localhost ~]# yum -y install psmisc        ###最小安装没有killall令需要安装 
[root@localhost ~]# killall -s HUP nginx  ## 重载Nginx配置文件(相当于刷新)
[root@localhost ~]# killall -s QUIT nginx  ## 退出 Nginx

为了使Nginx服务的启动、停止、重载等操作更加方便,可以编写基于CentOs 7.6的
Nginx服务控制文件使用systemctl工具来进行管理.CentOS7.6系统的管理习惯

[root@localhost ~]# vi /lib/systemd/system/nginx.service
[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
==>> wq 保存
[root@localhost ~]# chmod 754 /lib/systemd/system/nginx.service 
[root@localhost ~]# systemctl enable nginx.service 
[root@localhost ~]# systemctl start nginx  ## 这样我们就可以用这种方法来开启 Nginx 了

注: 在使用 systemctl 命令开启 nginx 之前要先使用 killall 命令将 nginx 服务关闭

[root@localhost ~]# killall -s QUIT nginx

测试:
在这里插入图片描述

1.3、配置文件 nginx.conf

全局配置

[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
#user  nobody;
worker_processes  1;
#error_log  logs/error.log;
#pid        logs/nginx.pid;

I/O时间配置

[root@localhost nginx-1.15.9]# vi /usr/local/nginx/conf/nginx.conf
events {
    
    
    use epoll;
    worker_connections  4096;
}

HTTP配置

http {
    
    
  . . . . . . . . . . . . 
    access_log  logs/access.log  main;
    sendfile        on;
    . . . . . . . . 
    keepalive_timeout  65;
    . . . . . . . . 
    server {
    
    
        listen       80;
        server_name  www.51xit.top;
        charset utf-8;
         . . . . . . . . 
        location / {
    
    
            root   html;
            index  index.html index.php;
        }
                . . . . . . . . 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
    
    
            root   html;
        }

Nginx的访问状态统计

  • 启用HTTP_ STUB_ STATUS状态统计模块
  • 配置编译参数时添加–with-http stub status module
  • nginx -V查看已安装的Nginx是否包含HTTP_ STUB _STATUS模块
[root@localhost nginx-1.15.9]# nginx -V
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) 
configure arguments: --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module

修改nginx.conf配置文件

http{
    
    
	server {
    
    
		listen 80;
		server name localhost;
		charset utf-8;
		location / {
    
    
			root html;
			index index.html index.htm;
        }
		location ~/status {
    
    		##添加此段##
			stub_status on;
			access_log off;}

测试:
在windows中输入20.0.0.11/status即可查看当前的状态统计信息
在这里插入图片描述

二、Nginx访问控制

2.1、基于授权的访问控制

生成用户密码认证文件

[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;
	}
[root@localhost ~]# killall -s HUP nginx	

在这里插入图片描述
登录后会出现出现 welcome to nginx!
点击登陆后 出现 welcome to nginx!

2.2、基于客户端的访问控制

通过客户端IP地址,决定是否允许对页面访问
配置规则:

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

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

  • 修改主配置文件nginx.conf,添加相应配置项
  • 除主机20.0.0.1之外允许其他客户端访问
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
    
    
            Order deny,allow;
            Deny from 20.0.0.1;
        }
[root@localhost ~]# killall -s HUP nginx

浏览器输入 20.0.0.25 ,登陆不了
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ZG_66/article/details/108490310