nginx模块

本文详细介绍了Nginx中的各种模块,包括目录索引模块的配置,如autoindex_exact_size和autoindex_localtime选项,以及访问限制模块的allow和deny规则。还提到了认证模块、Nginx状态模块,并探讨了如何使用Nginx代理Python应用程序,特别是与Django框架的结合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx中的模块(Python模块)

目录索引模块
示例:
location / {
    autoindex on;
}

1、启用或禁用目录列表输出
语法:	autoindex on | off;
默认:	autoindex off;
语境:	http,server,location

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2、指定是否应在目录列表中输出确切的文件大小(四舍五入)
语法: autoindex_exact_size on | off;
默认: autoindex_exact_size on;
语境: http,server,location
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

3、设置目录列表的格式
语法: autoindex_format html | xml | json | jsonp;
默认: autoindex_format html;
语境: http,server,location
image image image image
在这里插入图片描述
在这里插入图片描述

4、指定目录列表中的时间是否应以本地时区或UTC输出
语法: autoindex_localtime on | off;
默认: autoindex_localtime off;
语境: http,server,location

访问限制模块

allow : 允许IP访问
deny : 禁止IP访问

案例1:只允许192.168.15.1来访问。
	1、允许192.168.15.1来访问
		allow 192.168.15.1;
					
	2、禁止其他所有IP来访问
		deny all;
			
案例2:只允许192.168.15.0来访问。
	1、允许192.168.15.0来访问
		allow 192.168.15.0/24;
				
	2、禁止其他所有IP来访问
		deny all;
				
案例3:要求禁止192.168.15.1来访问。
	1、禁止192.168.15.1来访问
		deny 192.168.15.1;
				
	2、允许其他所有的IP来访问
		allow all;

认证模块

yum install httpd-tools -y
	
Syntax:	auth_basic string | off;	如果跟string,就代表开启。
Default:	
auth_basic off;
Context:	http, server, location, limit_except

1、生成密码
[root@web02 conf.d]# htpasswd -c /etc/nginx/auth chenyang

2、配置加密
auth_basic "This is Auth Basic!";
auth_basic_user_file /etc/nginx/auth;


在命令行中的访问方式
[root@m01 ~]# curl -H'Host: index.test.com' http://chenyang:123456@192.168.15.8

Nginx状态模块

location /status {
	stub_status;
}

禁用IP和开放IP访问

allow	: 允许IP访问
deny	:禁止IP访问

案例1:只允许192.168.15.1来访问。
	
	1、允许192.168.15.1来访问
	
		allow 192.168.15.1;
		
	2、禁止其他所有IP来访问
	
		deny all;

案例2:只允许192.168.15.0来访问。

	
	1、允许192.168.15.0来访问
	
		allow 192.168.15.0/24;
	
	2、禁止其他所有IP来访问
	
		deny all;
	
案例3:要求禁止192.168.15.1来访问。

	1、禁止192.168.15.1来访问
	
		deny 192.168.15.1;
	
	2、允许其他所有的IP来访问
	
		allow all;

目录索引模块

# 开启目录索引
autoindex on;

# 格式化文件大小
autoindex_exact_size off;

# 输出的格式
autoindex_format html;

# 使用时区
autoindex_localtime on;

限制连接数模块

1、创建一个内存空间存放访问者的IP

2、设置每一个访问者的同时连接次数

示例:
http {
	# 创建一个叫addr的空间,主要用来存放客户端ip,大小为10m
    limit_conn_zone $binary_remote_addr zone=addr:10m;
    ...
    server {
        ...
        location /download/ {
        	#调用addr的空间,限制连接数为1
            limit_conn addr 1;
        }
        
$binary_remote_add:客户端IP  cat /etc/nginx/nginx.conf中log_format下面的第一个
zone=addr:指定一个名字
10m:创建空间的大小

知识储备:
	ab : 创建请求的命令,(yum install httpd-tools -y )
		-c : 设置并发
		-n :  设置请求的总数
	ab -c 200 -n 10000 http://game.test.com/   #200个并发,10000个请求

限制请求数模块

1、创建一个内存空间存放访问者的IP

2、设置每一个访问者的同时请求次数
[root@web02 conf.d]# cat game.conf 
# 创建一个叫linux的空间,主要用来存放客户端IP,大小给10M
limit_conn_zone $remote_addr zone=linux:10m;

示例:

http {
	limit_req_zone $remote_addr  zone=python:10m rate=1r/s;

	server {
		server_name game.test.com;
		listen 80;
		location / {
			# 调用linux空间, 限制连接数为1
			# limit_conn linux 1;
			limit_req zone=python burst=5;
			root /usr/share/nginx/html5-mario;
			index index.html;
		}
	}
}
ab : 创建请求的命令,(yum install httpd-tools -y )

	-c : 设置并发
	-n :  设置请求的总数

Nginx代理Python

使用django框架

1、安装python3

	[root@web02 ~]# yum install python3 -y 

2、安装django框架

	[root@web02 ~]# pip3 install django==2.2.2
	
3、创建django项目
	[root@web02 opt]# cd /opt/
	[root@web02 opt]# django-admin startproject linux

4、在项目中创建应用

	[root@web02 opt]# cd linux/
	[root@web02 linux]# pwd
	/opt/linux
	[root@web02 linux]# django-admin startapp application


5、修改配置文件

	[root@web02 linux]# vim /opt/linux/linux/settings.py
	ALLOWED_HOSTS = ['*']
	DATABASES = {}


6、启动,浏览器访问
[root@web02 linux]# pwd
/opt/linux
[root@web02 linux]# python3 manage.py runserver 0.0.0.0:8000

讲解Nginx代理Python

1、创建用户

	[root@web02 linux]# groupadd django -g 888
	[root@web02 linux]# useradd django -u 888 -g 888 -r -M -s /bin/sh

2、安装依赖包

	yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
	
3、安装uwsgi和django

	pip3 install uwsgi 
	pip3 install django==2.2.2

4、创建Python项目代码

	cd /opt
    django-admin startproject linux
    cd linux
    django-admin startapp linux1

5、编辑项目启动配置文件
	[root@web02 linux]# cat myuwsgi.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@web02 linux]# cd /opt/linux
	[root@web02 linux]# uwsgi -d --ini myuwsgi.ini
	[uWSGI] getting INI configuration from myuwsgi.ini
	
	# 查看进程列表
	[root@web02 linux]# ps -ef | grep uwsgi
	root      26395      1  3 12:17 ?        00:00:00 uwsgi -d --ini myuwsgi.ini
	root      26397  26395  0 12:17 ?        00:00:00 uwsgi -d --ini myuwsgi.ini
	root      26398  26395  0 12:17 ?        00:00:00 uwsgi -d --ini myuwsgi.ini
	root      26399  26395  0 12:17 ?        00:00:00 uwsgi -d --ini myuwsgi.ini
	root      26400  26395  0 12:17 ?        00:00:00 uwsgi -d --ini myuwsgi.ini

7、配置Nginx连接uwsgi

cat /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 /opt/linux;
			# 索引文件
			index  index.html index.htm;
			# 客户端上传文件的最大值
			client_max_body_size 35m;
		}
	}

8、重启Nginx

	[root@web02 conf.d]# systemctl restart nginx
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值