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;
}
}
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]
Adding password for user zhang
[root@web02 nginx]
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;
Default: auth_basic 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
server accepts handled requests
2 2 2
Reading: 0 Writing: 1 Waiting: 1
2.3禁用ip和开放ip访问
allow :允许某个IP或者IP段访问
deny :禁止某个IP或者IP段访问
-----------------------------------------------------------------
Syntax: allow address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
Syntax: deny address | CIDR | unix: | all;
Default: —
Context: http, server, location, limit_except
-----------------------------------------------------------------
tail -f /var/log/nginx/access.log 实时监控nginx日志文件
-----------------------------------------------------------------
allow 192.168.15.1;
deny all;
curl -H'Host:lol.gailun.com' 192.168.15.8
2.4目录索引模块(autoindex_module)
--------------------------------------------------
Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location
--------------------------------------------------
Syntax: autoindex_exact_size on | off;
Default: autoindex_exact_size on;
Context: http, server, location
--------------------------------------------------
Syntax: autoindex_format html | xml | json | jsonp;
Default: autoindex_format html;
Context: http, server, location
--------------------------------------------------
Syntax: autoindex_localtime on | off;
Default: autoindex_localtime off;
Context: http, server, location
--------------------------------------------------
autoindex on;
autoindex_exact_size off;
autoindex_format html;
autoindex_localtime on;
server{
server_name lol.gailun.com;
listen 80;
include autoindex_params;
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;
server{
server_name cjml.com;
listen 80;
location / {
2.设置每一个访问者的同时连接次数
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
Time taken for tests: 0.748 seconds
Complete requests: 10000
Failed requests: 2223
2.6限制请求数模块(长连接状态,限制请求比限制连接强度高,短连接则一样)
模块为:ngx_http_limit_req_module
1.创建内存空间存放访问者的IP,放在server之外,http之内
limit_req_zone $remote_addr zone=nuoshou:10m rate=1r/s;
server{
server_name cjml.com;
listen 80;
location / {
2.设置每一个访问者的同时请求 次数
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
Time taken for tests: 6.002 seconds
Complete requests: 10000
Failed requests: 9993
3.python测试django
1.安装python3 (yum install python3)
yum install python3 -y
2.安装django框架
[root@web02 conf.d]
3.创建django项目
cd /opt
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
案例:部署nginx代理uwsgi,uwsgi启动python项目
因为nginx不支持wsgi协议,无法直接调用py开发的webApp。
在nginx+uWsgi+Django的框架里,nginx代理+webServer,uWsgi是wsgiServer,Django是webApp。
nginx代理其实是uWsgi,uWsgi将django变成webserver(服务)
nginx接收用户请求,并判定哪些转发到uWsgi,uWsgi再去调用pyWebApp。
--------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------
pip3 install uwsgi
pip3 install django=2.2.2
--------------------------------------------------------------------------------------------
cd /opt
django-admin startproject linux
cd linux
django-admin startapp linux1
--------------------------------------------------------------------------------------------
[root@localhost ~]
[uwsgi]
socket = :8000
chdir = /opt/gailun
wsgi-file = gailun/wsgi.py
module = gailun.wsgi
master = true
processes = 4
vacuum = true
--------------------------------------------------------------------------------------------
cd /opt/gailun
uwsgi -d --ini myuwsgi.ini
--------------------------------------------------------------------------------------------
[uWSGI] getting INI configuration from myuwsgi.ini
--------------------------------------------------------------------------------------------
[root@web02 gailun]
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
--------------------------------------------------------------------------------------------
[root@localhost ~]
server {
listen 80;
server_name py.test.com;
location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
uwsgi_read_timeout 2;
uwsgi_param UWSGI_SCRIPT gailun.wsgi;
uwsgi_param UWSGI_CHDIR /opt/gailun;
index index.html index.htm;
client_max_body_size 35m;
}
}
--------------------------------------------------------------------------------------------
systemctl restart nginx