day08 Nginx模块

发布时间:2022-07-01 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了day08 Nginx模块脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

day08 Nginx模块

lnmp架构

l	:Linux
n	:Nginx
m	:MySQL
p	:Python/PHP
lnmp架构:是最简单的架构

Nginx中的模块(Python模块):前提是代码编译安装的

前提准备

# 前提是会查nginx的文档
	https://nginx.org/en/docs/  --->documentation---->搜索autoindex_module(目录索引的模块)
	
    [root@web03 nginx-1.20.1]# ./configure --help | grep autoindex   # 查找目录索引模块
    --wIThout-http_autoindex_module    disable ngx_http_autoindex_module   # 默认启动
    
1、模块
    auth_basic	 :认证模块  # 官网搜索
    stub_status  :状态模块
    ngx_http_limit_conn_module:限制连接数模块
    autoindex_module:目录索引模块
    limit_req:限制请求数模块

认证模块

Syntax :	auth_basic string | off;        # string:开启
Default:	auth_basic off;                 # 默认关闭
Context:	http, server, location, limit_except   # 适用模块

Syntax:	auth_basic_user_file file;          # file:用户密码文件
Default:	—
Context:	http, server, location, limit_except

# htpasswd :生成密码文件命令

1、安装htpasswd命令
	[root@web03 conf.d]# yum install httpd-tools -y
	
2、生成密码文件
	[root@web03 conf.d]# htpasswd -c /etc/nginx/auth meng  -c:指定路径和用户名,下面设置密码
	[root@web03 conf.d]# cat /etc/nginx/auth
	meng:$aPR1$tMLuqooC$6mvHi9hay1GrDuA/XZYig/   # 用户名+密码
	
3、修改配置项
    [root@web03 conf.d]# vim autoindex.conf   # 修改配置
    [root@web03 conf.d]# nginx -t              # 测试
    [root@web03 conf.d]# Systemctl restart nginx   # 重启nginx服务
---------------------------------------------------------------------------------------------------
server{
        server_name index.test.COM;
        listen 80;

        include autoindex_params;

        # 开启认证
        auth_basic "hello world";
        # 加载密码文件
        auth_basic_user_file /etc/nginx/auth;

        location /{
                root /usr/share/nginx;
                index index.html;
        }
}
---------------------------------------------------------------------------------------------------

状态模块

语法:
    Syntax:	stub_status;    # 提供对基本状态信息的访问
    Default:	—
    Context:	server, location  # 适用模块

1、修改配置项
    [root@web03 conf.d]# vim autoindex.conf   # 修改配置
    [root@web03 conf.d]# nginx -t              # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
---------------------------------------------------------------------------------------------------   
server{
        server_name index.test.com;
        listen 80;

        include autoindex_params;

        auth_basic "hello world";
        auth_basic_user_file /etc/nginx/auth;

        # 状态模块      
        location /status{     # /status:是一个路径,比如:index.test.com/status
                stub_status;
        }

        location /{
                root /usr/share/nginx;
                index index.html;
        }
}
---------------------------------------------------------------------------------------------------
# 监控,但不常用
2、浏览器访问(对nginx的监控)
	http://index.test.com/status   # 后面加上/status
	监控:# 对nginx的监控
        Active connections: 2 
        server accepts handled requests
         2 2 4 
        Reading: 0 Writing: 1 Waiting: 1
        
     翻译:
        当前活动连接数:2 个
        server accepts:接受的客户端连接总数    handled:处理的连接总数   requests:客户端请求的总数
                   2                             2                       4   # 一一对应
        正在读取:0     响应连接数:1     空闲客户端连接数:1

禁用IP和开放IP访问

allow	:允许IP访问
deny	:禁止IP访问
语法:
    Syntax: allow address | CIDR | unix: | all;    # address;IP地址,CIDR :网段,all :所有
    Default:    —								# deny :允许访问
    Context:    http, server, location, limit_except

    Syntax: deny address | CIDR | unix: | all;     # deny :禁止访问
    Default:    —
    Context:    http, server, location, limit_except
    
 编辑文件:
     [root@web03 conf.d]# vim /etc/nginx/conf.d/autoindex.conf
     [root@web03 conf.d]# nginx -t     # 测试
     [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
---------------------------------------------------------------------------------------------------
    server{
        server_name index.test.com;
        listen 80;

        include autoindex_params;   # 他会从/etc/nginx根目录找文件。根目录为/etc/nginx
		
        allow 192.168.15.1;  # 只允许192.168.15.1访问
        deny all;            # 禁止所有IP访问

        location /{
                root /usr/share/nginx;
                index index.html;
        }
}
--------------------------------------------------------------------------------------------------- 

案例1:只允许192.168.15.1访问
	1、编写文件(带入上面配置)
        第一步:允许192.168.15.1来访问
            allow 192.168.15.1;
        第二步:禁止其他所有IP来访问
            deny all;
            
    2、测试
		[root@web03 conf.d]# tail -f /VAR/LOG/nginx/access.log   # 看日志ip,实时监控
		[root@m01 ~]# curl -H'Host: index.test.com' 192.168.15.9
		<head><title>403 Forbidden</title></head>    # 进不去
		# 电脑C盘hosts改成 172.16.1.9 index.test.com
		
案例2:只允许192.168.15.0来访问。
    1、编写文件(带入上面配置)
        第一步:允许192.168.15.0来访问
            allow 192.168.15.0/24;
        第二步:禁止其他所有IP来访问
            deny all;
            
    2、测试
		[root@web03 conf.d]# tail -f /var/log/nginx/access.log   # 看日志ip,实时监控
		[root@m01 ~]# curl -H'Host: index.test.com' 172.16.1.9
		<head><title>403 Forbidden</title></head>    # 进不去
		# c盘hosts改成 192.168.15.9 index.test.com
		
案例3:要求禁止192.168.15.1来访问。
    1、编写文件(带入上面配置)
        第一步:禁止192.168.15.1来访问
            deny 192.168.15.1;
        第二步:允许其他所有IP来访问
            allow all;
            
    2、测试
		[root@web03 conf.d]# tail -f /var/log/nginx/access.log   # 看日志ip,实时监控
		[root@m01 ~]# curl -H'Host: index.test.com' 172.16.1.9
		<head><title>403 Forbidden</title></head>    # 可以进去
		# 电脑c盘hosts改成 192.168.15.9 index.test.com

目录索引模块

1、目录索引的配置项
	1)、开启目录索引
        Syntax:	    autoindex on | off;     # on:打开,off:关闭
        Default:	autoindex off;          # 默认关闭
        Context:	http, server, location  # 适用的模块
    	
    2)、格式化文件大小
        Syntax:	    autoindex_exact_size on | off;    # on:打开,off:关闭
        Default:	autoindex_exact_size on;          # 默认开启
        Context:	http, server, location            # 适用的模块
        
    3)、输出的格式
        Syntax:	    autoindex_format html | XMl | JSON | jsonp;   # 使用什么格式
        Default:	autoindex_format html;						# 使用html格式
        Context:	http, server, location						# 适用的模块
    	
    4)、使用时区
        Syntax:	    autoindex_localtime on | off;
        Default:	autoindex_localtime off;            # 默认使用UTC时间,修改成本地时间 on
        Context:	http, server, location
        
2、编辑目录索引的配置文件
	[root@web01 ~]# cd /etc/nginx/conf.d
	[root@web03 conf.d]# vim autoindex.conf 
---------------------------------------------------------------------------------------------------
server{
        server_name index.test.com;
        listen 80;

        # 开启目录索引
        autoindex on;
        # 格式化文件大小
        autoindex_exact_size off;
        # 输出的格式
        autoindex_format html;
        # 使用时区
        autoindex_localtime on;

        location /{
                root /usr/share/nginx;
                index index.html;
        }
}
---------------------------------------------------------------------------------------------------
3、测试并重启nginx服务
    [root@web03 conf.d]# nginx -t     # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
    
4、避免重复写入,编写autoindex_params文件
	[root@web03 conf.d]# cd /etc/nginx/
	[root@web03 nginx]# vim autoindex_params
---------------------------------------------------------------------------------------------------
# 开启目录索引
autoindex on;
# 格式化文件大小
autoindex_exact_size off;
# 输出的格式
autoindex_format html;
# 使用时区
autoindex_localtime on;
---------------------------------------------------------------------------------------------------

5、重新编辑
	[root@web03 conf.d]# vim /etc/nginx/conf.d/autoindex.conf      # 编辑旧版本
---------------------------------------------------------------------------------------------------	
server{
        server_name index.test.com;
        listen 80;
		
        # 加载其他文件
        include autoindex_params;  # 他会从/etc/nginx根目录找文件。根目录为/etc/nginx  

        location /{
                root /usr/share/nginx;
                index index.html;
        }
}
---------------------------------------------------------------------------------------------------
    [root@web03 conf.d]# nginx -t     # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务

限制连接数模块

语法:
    Syntax:	limit_conn zone number;  # 用于限制每个定义的键的连接数,特别是来自单个 IP 地址的连接数
    Default:	—
    Context:	http, server, location

1、创建一个内存空间存放访问者的IP
    [root@web03 conf.d]# vim /etc/nginx/conf.d/game.conf
    [root@web03 conf.d]# nginx -t     # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
---------------------------------------------------------------------------------------------------
# 创建一个叫addr的空间,用来存放客户端IP,大小10m。remote_addr:客户端IP
limit_conn_zone $remote_addr zone=addr:10m;

server{
        server_name game.test.com;
        listen 80;

        location /{
                # 调用addr空间,限制连接数为1 
                limit_conn addr 1;
                root /usr/share/nginx/HTML5-mario;
                index index.html;
        }

}
---------------------------------------------------------------------------------------------------
2、设置每一个访问者的同时连接次数
	 limit_conn addr 1;
	 
3、编写虚拟机的域名解析
	[root@web03 conf.d]# vim /etc/hosts
	192.168.15.9 game.test.com
	
4、测试
	[root@web03 conf.d]# ab -c 200 -n 10000 http://game.test.com/
    Concurrency Level:      200               # 并发级别
    Time taken for tests:   0.075 seconds     # 测试所用时间
    complete requests:      10000             # 完成申请
    Failed requests:        3424              # 失败的请求
    
5、证明
    # 调用addr空间,限制连接数为1 
    limit_conn addr 1;    # 证明了阻挡同时请求的3424次

限制请求数模块(测试连接数)

知识储备:
	ab : 创建请求的命令,(yum install httpd-tools -y )
        -c : 设置并发
        -n :  设置请求的总数
        
长链接:前后端链接后,除非一端主动断开,不然一直链接
短连接:链接完就会断开

1、创建一个内存空间存放访问者的IP
    [root@web03 conf.d]# vim /etc/nginx/conf.d/game.conf
    [root@web03 conf.d]# nginx -t     # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
---------------------------------------------------------------------------------------------------
# 设置一个储存ip地址,储存大小为10m,空间名字meng,一秒只能请求一次的空间。
limit_req_zone $remote_addr zone=meng:10m rate=1r/s;

server{
        server_name game.test.com;
        listen 80;

        location /{
                # 调用空间名字是meng的空间,最大限度可以同时访问五次。
                limit_req zone=meng burst=5;
                
                root /usr/share/nginx/html5-mario;
                index index.html;
        }
}
---------------------------------------------------------------------------------------------------
2、设置每一个访问者的同时请求次数
	 limit_req zone=meng burst=5;

3、编写虚拟机的域名解析
	[root@web03 conf.d]# vim /etc/hosts
	192.168.15.9 game.test.com
	
4、测试
	[root@web03 conf.d]# ab -c 200 -n 10000 http://game.test.com/
    Concurrency Level:      200               # 并发级别
    Time taken for tests:   5.002 seconds     # 测试所用时间
    Complete requests:      10000             # 完成申请
    Failed requests:        9994              # 失败的请求
    
5、证明
    # 调用addr空间,限制连接数为1 
    limit_conn addr 1;    # 证明了阻挡同时请求的9994次

配置django框架

使用django框架

1、安装python3
	[root@web03 conf.d]# yum install python3 -y 

2、安装django框架
	[root@web03 ~]# piP3 install django==2.2.2

3、创建django项目
    [root@web03 ~]# cd /opt/
    [root@web03 opt]# django-admin startproject linux

4、在项目中创建应用
    [root@web03 opt]# cd linux/
    [root@web03 linux]# pwd
    /opt/linux
    [root@web03 linux]# django-admin startapp application


5、修改配置文件
    [root@web03 linux]# vim /opt/linux/linux/settings.py
    Allowed_HOSTS = ['*']
    DATABASES = {}


6、启动,浏览器访问
    [root@web03 linux]# pwd
    /opt/linux    # 在项目根目录启动
    [root@web03 linux]# python3 manage.py runserver 0.0.0.0:8000

Nginx代理Python

为什么要用uWsgi:
    因为nginx不支持wsgi协议,无法直接调用py开发的webApp。
    在nginx+uWsgi+Django的框架里,nginx代理+webServer,uWsgi是wsgiServer,Django是webApp。
    nginx接收用户请求,并判定哪些转发到uWsgi,uWsgi再去调用pyWebApp。
    
1、创建项目用户
    [root@web03 linux]# groupadd django -g 888
    [root@web03 linux]# useradd django -u 888 -g 888 -r -M -s /bin/sh
    
2、安装依赖包
	[root@web03 linux]# yum install python3 libxml* python-devel gcc* pcre-devel oPEnssl-devel python3-devel -y
	
3、安装uwsgi和django
	[root@web03 linux]# pip3 install uwsgi
	[root@web03 linux]# pip3 install django==2.2.2
	
4、创建Python项目代码
    [root@web03 linux]# cd /opt
    [root@web03 opt]# django-admin startproject linux
    [root@web03 opt]# cd linux
    [root@web03 linux]# django-admin startapp linux1
    
5、编辑项目启动配置文件
	[root@web03 linux]# vim /opt/linux/myweb_uwsgi.ini 
---------------------------------------------------------------------------------------------------	
[uwsgi]
# 端口号
socket            = :8000
# 指定项目的目录
chdir           = /opt/linux
# wsgi文件路径
wsgi-file       = linux/wsgi.py
# 模块wsgi路径
module          = linux.wsgi
# 是否开启master进程
master          = true
# 工作进程的最大数目
processes       = 4
# 结束后是否清理文件
vacuum          = true
---------------------------------------------------------------------------------------------------

6、启动uwsgi
	uwsgi参数:
        -d : 以守护进程方式运行
        --ini : 指定配置文件的路径
        
    [root@web03 linux]# cd /opt/linux      
    [root@web03 linux]# uwsgi -d --ini myweb_uwsgi.ini
    [root@web03 linux]# ps -ef | grep uwsgi  # 过滤下进程
    root       2820      1  0 21:50 ?        00:00:00 uwsgi -d --ini myweb_uwsgi.ini  # 1个守护进程
    root       2822   2820  0 21:50 ?        00:00:00 uwsgi -d --ini myweb_uwsgi.ini  # 4个松祚进程
    root       2823   2820  0 21:50 ?        00:00:00 uwsgi -d --ini myweb_uwsgi.ini
    root       2824   2820  0 21:50 ?        00:00:00 uwsgi -d --ini myweb_uwsgi.ini
    root       2825   2820  0 21:50 ?        00:00:00 uwsgi -d --ini myweb_uwsgi.ini
    
7、配置Nginx连接uwsgi
    [root@web03 linux]# vim /etc/nginx/conf.d/python.conf 
---------------------------------------------------------------------------------------------------
# 配置一个网站
server {
    # 监听端口
    listen 80;
    # 配置域名
    server_name py.test.com;
    # 配置域名路径
    location / {
        # 加载nginx代理uwsgi的配置项
        include uwsgi_params;
        # 指定uwsgi的访问地址
        uwsgi_pass 127.0.0.1:8000;
        # uwsgi的超时时间
        uwsgi_read_timeout 2;
        # 自定义uwsgi代理项目的路径以及配置项
        uwsgi_param UWSGI_SCRIPT linux.wsgi;
        # 指定Python项目的路径
        uwsgi_param UWSGI_CHDIR /root/linux;
        # 索引文件
        index  index.html index.htm;
        # 客户端上传文件的最大值
        client_max_body_size 35m;
    }
}
---------------------------------------------------------------------------------------------------
8、测试并重启Nginx
    [root@web03 conf.d]# nginx -t     # 测试
    [root@web03 conf.d]# systemctl restart nginx   # 重启nginx服务
    
9、修改本地域名解析
	C:WindowsSystem32driversetchosts
	192.168.15.9 py.test.com

脚本宝典总结

以上是脚本宝典为你收集整理的day08 Nginx模块全部内容,希望文章能够帮你解决day08 Nginx模块所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。