网站首页 > 技术教程 正文
Nginx的特点
- 擅长处理静态小文件(1M)
- 支持高并发(支持epoll模型)
- 占用资源少
- 2W并发连接,10个进程,200M内存
- 配置简单、灵活、轻量
- 功能丰富(Web、Proxy、Cache)
- 工作在IOS第七层(支持限速、连接数限制等)
Nginx基本功能
- 静态服务(图片、视频、css、js、html)
- 基于域名/IP/端口的虚拟主机
- Http/Https、SMTP、POP3反向代理
- TCP/UDP反向代理
- FastCGI、uWSGI反向代理
- 负载均衡
- 页面缓存(CDN)
- 支持gzip、expirse
- URL Rewrite
- 路径识别
- 基于IP、用户的访问控制
- 支持访问速率、并发限制
- 反向代理(适用2000WPV、并发连接1W/秒)
Nginx原理
master process
- 与外界通讯和工作进程管理
- 读取nginx配置文件并验证有效性
- 建立、绑定和关闭Socket
- 按照配置文件生成、管理和结束工作进程
- nginx重启、停止、重载配置文件、平滑升级、管理日志文件
worker process
- 接收客户端请求,讲请求交给各个功能模块处理
- 系统IO调用,获取相应的数据,发送相应给客户端
- 数据缓存管理
- 接收主进程指令,比如重启、关闭
缓存索引重建及管理进程
cache模块主要由缓存索引重建和缓存索引管理两个进程完成,缓存索引重建进程是在nginx服务启动一段时间后(默认1分钟),由主进程生成,对本地磁盘的索引文件在内存中建立元数据,包括扫描、过期更新等操作,完成后退出
- 模块只有使用时才加载
Nginx常用模块
核心模块
- Core functionality
标准模块
- ngx_http_core_module
- ngx_http_access_module
- ngx_http_fastcgi_module
- ngx_http_gzip_module
- ngx_http_rewrite_module
- ngx_http_upstream_module
- ngx_http_proxy_module
- ngx_http_limit_conn_module
- ngx_http_limit_req_module
- ngx_http_auth_basic_module
- ngx_http_log_module
- ngx_http_ssl_module
- ngx_http_status_module
- ngx_http_realip_module
Nginx(Proxy)支持的算法
rr 轮询
wrr 权重轮询
iphash hash算法(解决session命中)
urlhash hash算法
fair 动态算法(响应时间最短)
Nginx部署
安装依赖
yum install openssl-devel pcre-devel pcre gcc zlib -y
- pcre 正则处理需要
- gcc 编译需要
- zlib 压缩需要
- openssl 安全链接需要
编译安装
useradd nginx -s /sbin/nologin -M wget http://nginx.org/download/nginx-1.12.2.tar.gz tar xzf nginx-1.12.2.tar.gz && cd nginx-1.12.2 ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_geoip_module --with-stream --with-http_stub_status_module --with-http_realip_module make && make install
基本操作
#启动 /usr/local/nginx/sbin/nginx #停止 /usr/local/nginx/sbin/nginx -s stop # 检查语法 /usr/local/nginx/sbin/nginx -t #重启 /usr/local/nginx/sbin/nginx -s #重启原理:生成基于新配置的线程,新请求转发到新线程,旧线程处理完成后停止。
主配置文件 nginx.conf
user nginx nginx;
# 设置所有worker进程可以打开的最大文件句柄数
worker_rlimit_nofile 65535;
worker_processes auto;
worker_cpu_affinity auto;
events {
# 让多个worker进程轮询相应新请求
accept_mutex on;
# 限制每个worker进程的并发连接数
worker_connections 10240;
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
# 开启高效文件传输模式
sendfile on;
tcp_nodelay on;
tcp_nopush on;
# 设置客户端连接保持会话的超时时间
keepalive_timeout 10;
# 设置客户端请求头读取超时时间
client_header_timeout 15;
# 设置客户端请求主体读取超时时间
client_body_timeout 15;
# 指定响应客户端的超时时间
send_timeout 15;
# 设置最大的容许的客户端请求主体大小
client_max_body_size 10M;
# 将http请求响应头里的nginx版本号信息隐藏
server_tokens off;
# 开启压缩
gzip on;
# 设置容许压缩的页面最小字节数
gzip_min_length 1k;
# 申请4个单位为32K的内存作为压缩结果流缓存
gzip_buffers 4 32k;
# 压缩的版本
gzip_http_version 1.1;
# 由于IE6对Gzip压缩效果不好,建议在IE6的环境下关闭压缩功能
gzip_disable "MSIE[1-6]";
# 压缩比率
gzip_comp_level 3;
# 指定压缩的类型
gzip_types text/plain text/css text/xml application/javascript application/x-javascript;
# 设置从web服务器传给缓存服务器的时候不被解压。只有传送到用户后才解压缩
gzip_vary on;
# 设置为Json格式,方便ELK日志分析软件处理
log_format json_main '{"@timestamp":"$time_iso8601",'
'"remote_addr":"$server_addr",'
'"remote_user":"$remote_user",'
'"request_time":"$request_time",'
'"request":"$request",'
'"body_bytes_sent":"$body_bytes_sent",'
'"request_method":"$request_method",'
'"upstream_response_time":"$upstream_response_time",'
'"upstream_addr":"$upstream_addr",'
'"url":"$uri",'
'"http_x_forwarded_for":"$http_x_forwarded_for",'
'"http_referrer":"$http_referer",'
'"status":"$status"}';
access_log logs/access.log json_main;
error_log logs/error.log error;
}
扩展配置文件(便于管理) vhost.conf
server {
listen 8000;
server_name www.node1.com;
root /data/webroot/;
set_real_ip_from 10.0.0.0/16;
set_real_ip_from 192.168.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location / {
index index.html;
}
# 设置资源缓存时间
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
expires 3650d;
}
# 设置资源缓存时间
location ~ .*\.(js|css)?$ {
expires 30d;
}
# 设置状态页访问信息
location /ngx_status {
stub_status on;
access_log off;
allow 192.168.0.0/16;
deny all;
}
access_log logs/lego_access.log json_main;
}
扩展配置 proxy.conf
# 当后端web服务器上也配置多个虚拟主机时,需要用该header来区分反向代理哪个主机名。 proxy_set_header Host $host; # 通过$remote_addr变量获取前端客户真实IP地址。 proxy_set_header X-Real-IP $remote_addr; # 通过$remote_addr变量获取前端客户真实IP地址。 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 设置缓冲区 proxy_buffering on; # 设置后端服务器的响应头大小,是响应头的缓冲区。 proxy_buffer_size 4k; # 设置缓冲区的数量和大小,nginx从代理的后端服务器获取的响应信息会放置到缓冲区。 proxy_buffers 8 32k; #此设置表示nginx会在没有完全读完后端响应的时候就开始向客户端传送数据,所以它会划出一部分缓冲区来专门向客户端传送数据。建议为proxy_buffers中单个缓冲区大小的2倍) proxy_busy_buffers_size 64K; # 指定当响应内容大于proxy_buffers指定的缓冲区时, 写入硬盘的临时文件的大小。 proxy_max_temp_file_size 1024m; # 一次访问能写入的临时文件的大小,默认是proxy_buffer_size和proxy_buffers中设置的缓冲区大小的2倍。 proxy_temp_file_write_size 128k; # proxy_request_buffering on; # proxy_ignore_headers Set-Cookie; # 表示与后端服务器连接的超时时间 proxy_connect_timeout 60s; # 表示后端服务器的数据回传时间 proxy_send_timeout 300; # 设置nginx从代理的后端服务器获取信息的时间 proxy_read_timeout 300s;
猜你喜欢
- 2024-10-14 深入浅出Nginx,如何做到高并发下的高效处理?如何做到热部署?
- 2024-10-14 「Docker类」 使用Dockerfile构建并部署nginx
- 2024-10-14 你所熟悉的nginx还可以用docker这样部署~
- 2024-10-02 如何使用Terraform部署NGINX容器(terraform构建与运行)
- 2024-10-02 Nginx基础部署说明,小白自用(nginx部署配置)
- 2024-10-02 nginx安装部署和配置管理(nginx1.16安装)
- 2024-10-02 小白爱折腾·其五:如何在Nginx下部署项目文件夹
- 2024-10-02 nginx的完全离线部署教程,涨知识了
欢迎 你 发表评论:
- 12-18qq客户端在哪里进入(qq客户端在哪里打开)
- 12-18影音先锋下载资源在线播放(影音先锋2019手机版官方下载)
- 12-17surface pro9测评(surface+pro)
- 12-17flash中心下载安装(flash中心app)
- 12-17360桌面助手官网(360桌面助手安装包叫什么)
- 12-17u盘启动pe蓝屏(u盘pe启动后蓝屏)
- 12-17qq手机版最新版本(最新qq手机版本官方下载)
- 12-17小米资源下载中心(小米资源网)
- 最近发表
- 标签列表
-
- 下划线是什么 (87)
- 精美网站 (58)
- qq登录界面 (90)
- nginx 命令 (82)
- nginx .http (73)
- nginx lua (70)
- nginx 重定向 (68)
- Nginx超时 (65)
- nginx 监控 (57)
- odbc (59)
- rar密码破解工具 (62)
- annotation (71)
- 红黑树 (57)
- 智力题 (62)
- php空间申请 (61)
- 按键精灵 注册码 (69)
- 软件测试报告 (59)
- ntcreatefile (64)
- 闪动文字 (56)
- guid (66)
- abap (63)
- mpeg 2 (65)
- column (63)
- dreamweaver教程 (57)
- excel行列转换 (56)

本文暂时没有评论,来添加一个吧(●'◡'●)