day08 Nginx模块
1.昨日内容习题
1、Nginx源码编译安装的步骤
1、下载源代码
2、解压
3、设置配置参数
4、编译
5、安装
2、设置worker进程数的配置项
worker_processes
3、配置端口的配置项
listen
4、加载其他文件的配置项
include
5、设置worker进程最大连接数
worker_connections
6、指定Nginx worker进程启动用户的配置项
user
7、假设baidu的源代码(html)在/usr/share/nginx/html目录,请写出nginx的配置
server {
server_name baidu.com;
listen 80;
location / {
root /usr/share/nginx/html;
index index.html;
}
}
#什么是lnmp架构
l是linux
n是nginx
m是mysql
p是python或者php
2.Nginx模块(python中的import)
2.1 认证模块
1.先安装 yum install httpd-tools -y
yum install httpd-tools -y
2.生成auth密码文件,后面指定用户名
[root@web02 conf.d]# htpasswd -c /etc/nginx/auth zhang
Adding password for user zhang
[root@web02 nginx]# cat auth #发现这个文件创建成功
zhang:$apr1$vSig0JDH$hCuvPG/O.7ip0EBYUVtzc0
3.在配置文件加入
auth_basic "This is lol gailun"; #开启认证模块
auth_basic_user_file /etc/nginx/auth; #指定账号密码路径
Syntax: auth_basic string | off; # 两个选项string(任意字符--开启) off(关闭)
Default: auth_basic off; #默认off(关闭)
Context: http, server, location, limit_except #可在哪个模块使用
-----------------------------------------------------------------
Syntax: auth_basic_user_file file; #用户名密码文件
Default: —
Context: http, server, location, limit_except
2.2nginx状态模块
Syntax: stub_status; #监控状态
Default: —
Context: server, location
-----------------------------------------------------------------
在配置文件加入
location /status {
stub_status;
}
访问时候加入http://lol.gailun.com/status
Active connections: 2 #当前活动客户端连接数,包括Waiting连接数。
server accepts handled requests #accepts:接受的客户端连接总数。handled:处理的连接总数.requests:客户端请求的总数。
2 2 2
Reading: 0 Writing: 1 Waiting: 1
#Reading:nginx 正在读取请求头的当前连接数。
#Writing:nginx 将响应写回客户端的当前连接数。
#Waiting:当前等待请求的空闲客户端连接数。
2.3禁用ip和开放ip访问
allow :允许某个IP或者IP段访问
deny :禁止某个IP或者IP段访问
-----------------------------------------------------------------
Syntax: allow address | CIDR | unix: | all; #allow 后边跟ip或者网段或者启动进程的socket文件或者全部,谁访问允许谁的IP,windows虚拟网卡是192.168.15.1 ,不是虚拟机的ip
Default: —
Context: http, server, location, limit_except #可在哪个模块使用
Syntax: deny address | CIDR | unix: | all; #deny 后边跟ip或者网段或者启动进程的socket文件或者全部,谁访问禁止谁的IP,windows虚拟网卡是192.168.15.1 ,不是虚拟机的ip
Default: —
Context: http, server, location, limit_except #可在哪个模块使用
-----------------------------------------------------------------
tail -f /var/log/nginx/access.log 实时监控nginx日志文件
-----------------------------------------------------------------
allow 192.168.15.1; #允许 192.168.15.1
deny all; #禁止 所有IP
#从上往下执行,如果第一条满足就不看下面的,第一条不满足在执行下面的,就是说如果执行deny的时候他肯定已经不是ip 192.168.15.1了,就实现了允许192.168.15.1访问,其他禁止
curl -H'Host:lol.gailun.com' 192.168.15.8 #在m01机器执行,用的是他自己的IP访问的
2.4目录索引模块(autoindex_module)
--------------------------------------------------
Syntax: autoindex on | off; #autoindex(目录索引) 有两个选项on(开启)和off(关闭)
Default: autoindex off; #默认关闭
Context: http, server, location #可在哪个模块使用:http, server, location
--------------------------------------------------
Syntax: autoindex_exact_size on | off; #autoindex_exact_size(#指定是否应在目录列表中输出确切的文件大小(on),或者四舍五入到千字节、兆字节和千兆字节。(off)) 有两个选项on(开启)和off(关闭)
Default: autoindex_exact_size on; #默认开启
Context: http, server, location #可在哪个模块使用:http, server, location
--------------------------------------------------
Syntax: autoindex_format html | xml | json | jsonp; #设置目录列表的格式 html | xml | json | jsonp
Default: autoindex_format html; #默认html格式
Context: http, server, location #可在哪个模块使用:http, server, location
--------------------------------------------------
Syntax: autoindex_localtime on | off; #设置指定目录列表中的时间是否应以本地时区(on)或 UTC 输出(off)。
Default: autoindex_localtime off; #默认关闭
Context: http, server, location #可在哪个模块使用:http, server, location
--------------------------------------------------
#由于一直写这个autoindex很麻烦所以在/etc/nginx下创建一个autoindex_params
autoindex on; #开启目录索引
autoindex_exact_size off; #指定是否应在目录列表中输出确切的文件大小(on),或者四舍五入到千字节、兆字节和千兆字节。(off) 格式化输出文件大小
autoindex_format html; #设置目录格式为html格式
autoindex_localtime on; #设置时间为本地时区
#/etc/nginx/conf.d/autoindex.conf 设置模块内容如下
server{
server_name lol.gailun.com;
listen 80;
include autoindex_params; #他会在根路径开始找,nginx的根路径 /etc/nginx
location / {
root /usr/share/nginx;
index index.html;
}
}
2.5限制连接模块(一个客户端同时访问这个网站多少次)
限制连接模块:ngx_http_limit_conn_module
1.创建内存空间存放访问者的IP,放在server之外,http之内
limit_conn_zone $remote_addr zone=gailun:10m;
#创建一个叫gailun的空间,主要用来存放客户端IP。。。大小10M $后面跟的ip路径这个在nginx.conf 日志文件可以看到remote_addr指定的就是ip ,zone是名字随便指定,最后边是开辟空间的大小
server{
server_name cjml.com;
listen 80;
location / {
2.设置每一个访问者的同时连接次数
#调用连接空间gailun,并限制同时最大连接数为1
limit_conn gailun 1;
root /usr/share/nginx/html5-mario;
index index.html;
}
}
测试:
ab:创建请求的命令,需要提前下载httpd-tools
-c:设置并发(同时访问)
-n:设置请求的总数
首先在etc/hosts加入解析
192.168.15.8 cjml.com
然后在执行ab -c 200 -n 10000 http://cjml.com/
Concurrency Level: 200 #同时访问200次
Time taken for tests: 0.748 seconds #时间
Complete requests: 10000 #总共执行10000次
Failed requests: 2223 #失败2223次
2.6限制请求数模块(长连接状态,限制请求比限制连接强度高,短连接则一样)
模块为:ngx_http_limit_req_module
1.创建内存空间存放访问者的IP,放在server之外,http之内
#rate每秒访问一次
limit_req_zone $remote_addr zone=nuoshou:10m rate=1r/s;
server{
server_name cjml.com;
listen 80;
location / {
2.设置每一个访问者的同时请求 次数
#调用连接空间gailun,并限制最多请求五次
limit_req zone=nuoshou burst=5;
root /usr/share/nginx/html5-mario;
index index.html;
}
}
测试:ab -c 200 -n 10000 http://cjml.com/
Concurrency Level: 200 #同时访问200次
Time taken for tests: 6.002 seconds #时间
Complete requests: 10000 #总共执行10000次
Failed requests: 9993 #失败9993次
3.nginx代理python
1.安装python3 (yum install python3)
yum install python3 -y
2.安装django框架
[root@web02 conf.d]# pip3 install django==2.2.2
3.创建django项目
cd /gailun
django-admin startproject gailun #后面跟的名字
4、在项目中创建应用
cd gailun/
django-admin startapp application
5、修改配置文件
vim /opt/gailun/gailun/settings.py
ALLOWD_HOSTS=['*']
6、启动,浏览器访问(启动要在项目根目录下启动)
#删掉或注释
DATAbaseS={}
python3 manage.py runserver 0.0.0.0:8000
案例:部署django框架代理python
# 为什么要用uWsgi
因为nginx不支持wsgi协议,无法直接调用py开发的webApp。
在nginx+uWsgi+Django的框架里,nginx代理+webServer,uWsgi是wsgiServer,Django是webApp。
nginx代理其实是uWsgi,uWsgi将django变成webserver(服务)
nginx接收用户请求,并判定哪些转发到uWsgi,uWsgi再去调用pyWebApp。
--------------------------------------------------------------------------------------------
# 创建项目用户(为了统一用户,给python代码来用)
groupadd django -g 888
useradd django -u 888 -g 888 -r -M -s /bin/sh
--------------------------------------------------------------------------------------------
# 安装依赖软件
yum install python3 libxml* python-devel gcc* pcre-devel openssl-devel python3-devel -y
--------------------------------------------------------------------------------------------
# 安装uwsgi
pip3 install uwsgi
pip3 install django=2.2.2
--------------------------------------------------------------------------------------------
# 创建python项目代码
cd /opt
django-admin startproject linux
cd linux
django-admin startapp linux1
--------------------------------------------------------------------------------------------
# 编辑项目启动配置文件
[root@localhost ~]# vim /opt/gailun/myuwsgi.ini
[uwsgi]
#上面的是跟yum源一样
# 端口号socket django启动之后的端口好
socket = :8000
# 指定项目的目录 指的是项目名称
chdir = /opt/gailun
# wsgi文件路径
wsgi-file = gailun/wsgi.py
# 模块wsgi路径 和外面的民资一样
module = gailun.wsgi
# 是否开启master进程
master = true
# 工作进程的最大数目
processes = 4
# 结束后是否清理文件
vacuum = true
--------------------------------------------------------------------------------------------
# 启动uwsgi
cd /opt/gailun
uwsgi -d --ini myuwsgi.ini
#-d 以守护进程方式运行(后台运行)
#--ini 指定配置文件路径
--------------------------------------------------------------------------------------------
#出现下面这个则为成功
[uWSGI] getting INI configuration from myuwsgi.ini
--------------------------------------------------------------------------------------------
#查看进程 一个master进程 四个工作进程
[root@web02 gailun]# ps -ef | grep uwsgi
root 6154 1 0 21:52 ? 00:00:00 uwsgi -d --ini myuwsji.ini
root 6156 6154 0 21:52 ? 00:00:00 uwsgi -d --ini myuwsji.ini
root 6157 6154 0 21:52 ? 00:00:00 uwsgi -d --ini myuwsji.ini
root 6158 6154 0 21:52 ? 00:00:00 uwsgi -d --ini myuwsji.ini
root 6159 6154 0 21:52 ? 00:00:00 uwsgi -d --ini myuwsji.ini
root 6204 5172 0 21:56 pts/0 00:00:00 grep --color=auto uwsgi
--------------------------------------------------------------------------------------------
# 编辑nginx配置文件
[root@localhost ~]# cat /etc/nginx/conf.d/python.conf
#配置网站
server {
#监听端口
listen 80;
#配置域名
server_name py.test.com;
#配置路径
location / {
#加载nginx代理uwsgi的配置项
include uwsgi_params;
#指定uwsgi访问地址 netstat -nutlp 产看端口信息
uwsgi_pass 127.0.0.1:8000;
#超时时间
uwsgi_read_timeout 2;
#自定义uwsgi代理项目的路径以及配置项
uwsgi_param UWSGI_script gailun.wsgi;
#指定ython项目的路径
uwsgi_param UWSGI_CHDIR /root/gailun;
#索引文件
index index.html index.htm;
#客户端上传文件最大值(可要可不要)
client_max_body_size 35m;
}
}
--------------------------------------------------------------------------------------------
# 重启Nginx
systemctl restart nginx