基于Nginx的中间件架构
常用的http服务有哪些 :
HTTPD --基于apache基金会 IIS --微软 GWS --Google
优点:
1.IO多路复用: 多个描述符的I/O操作都能在一个线程内并发交替地顺序完成.(复用指的是复用同一个线程)
IO多路复用的实现方式 select poll epoll(nginx采用的模型)
2.功能模块少 代码模块化
3.cpu亲和:是一种把cpu核心和nginx工作进程绑定方式,把每个worker进程固定在一个cpu上执行,减少切换cpu的cache miss,获得更好的性能
4.采用sendfile 直接会把文件通过内核空间传到socket,排除了去用户空间的损耗
=========================================================================
nginx的快速安装可以在nginx.org官网去查看,最好使用稳定版
vim /etc/yum.repos.d/nginx.repo
加上配置
yum list|grep nginx查看版本,然后安装就好
安装目录的分析
rpm -ql|nginx 可以查看安装的rpm包
nginx文件介绍
/etc/logrotate.d/nginx 配置文件 nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx
/etc/nginx/nginx.conf nginx主配置文件
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf 目录.配置文件 nginx默认配置
/etc/nginx/fastcgi_params 配置文件 cgi配置相关,fastcgi配置
/etc/nginx/uwsgi_params
/etc/nginx/scgi_params
/etc/nginx/koi-utf 配置文件 编码转换映射转化文件
/etc/nginx/koi-win
/etc/nginx/win-utf
/etc/nginx/mime.types 配置文件 设置http协议的content-type与扩展名对应关系
/usr/lib/systemd/system/nginx-debug.service 配置文件 用于配置出系统守护进程管理器管理方式
/usr/lib/systemd/system.nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/sbin/nginx
/usr/sbin/nginx-debug 命令 nginx服务的启动管理的终端命令
/var/cache/nginx 目录 nginx的缓存目录
/var/log/nginx 目录 nginx的日志目录
nginx默认配置语法
user 设置nginx服务的系统使用用户
worker_processes 工作进程数 与cpu数保持一致
error_log nginx的错误日志
pid nginx服务启动时候pid
events worker_connections 每个进程允许最大连接数 65535 一般10000就可以
use 工作进程数
http{
server{
listen 80;
server_name localhost;
location /{
root ....
}
}
}
修改配置后
systemctl restart nginx.service
systemctl reload nginx.service
Http请求 request -包含请求行,请求头部,请求数据
response -包含状态行,消息报头,响应正文
查看 curl -v http://www.baidu.com > /dev/null
tail -n 200 日志
nginx -t(检查) -c(路径) /etc/nginx/nginx.conf
nginx模块
模块一
http_stub_status_module nginx链接的客户端的状态 在server location下配置
配置:
location /mystatus{
stub_status;
}
nginx -tc /etc/nginx/nginx.conf
nginx -s reload -c /etc/nginx/nginx.conf
ip a 可以查看地址
模块二
with-http_random_index_module 目录中选择一个随机主页 在location下配置
location /{
root /opt/app/code;
random_index on;
}
模块三
with-http_sub_module http内容替换 在http.server,location下配置
location /{
root /opt/app/code;
sub_filter '<a>imooc' '<a>IMOOC';//这个默认只替换第一个
sub_filter_once off; //这个设置是为了让全局都替换
}
模块四
access_module 访问控制
location /{
root /opt/app/code;
deny 101.201.222.230;//禁止这个ip访问
rollow all;
}
模块五
http_auth_basic_module 用户登录访问控制
rpm -qf /usr/bin/htpasswd 首先看下安装了没有
没有的话 yum install httpd-tools -y
nginx的请求限制
连接频率限制 limit_conn_module
请求频率限制 limit_req_module