Nginx 基础篇

安装

yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automake

yum -y install wget httpd-tools vim 

Nginx 特性 和 优点

1.IO多路复用  epoll (多个IO复用一个)
	相对于select(1024)模型优点   无最大连接限制
2.轻量级
	功能模块少  代码模块化
3.CPU亲和(Nginx工作进程和每个核心记性绑定)
	worker 每个worker一个CPU 可配置  减少CPU切换性能损耗
4.sendfile

Nginx版本

1.开发版本  Mainline version
2.稳定版本  stable version
3.历史版本  Legacy versions
http://nginx.org/en/download.html
pgb 检查下载nginx是够有 安全注入

Nginx 目录和配置语法

路径							类型			作用

Nginx 日志

error.log  access.log

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;	

log_format 自定义配置   
自定义变量也可以

NGinx 模块学习

官方模块  
	nginx -V 查看
	nginx -tc nginx.conf
	nginx -s reload -c nginx.conf
	
	模块一:http_stub_status_module 配置  直接访问路径即可   查看nginx的状态
			在server配置
		 	location /mystatus{
		 		stub_status;
		 	}
	模块二:http_random_index_module  目录随即显示一个目录下主页  多主页场景
			location / {
			root 文件路径;
			random_index on;
			}
	
	模块三: http_sub_moudle  http内容替换  
			sub_filter string replacement
			context: http server location
			栗子:sub_filter 'a' 'b'   a替换为b
			
			sub_filter_last_modified  on|off  缓存场景
			context: http server location
			
			sub_filter_once on|off  默认off 配合第一个一起使用   off只匹配第一个  on 都匹配
			context: http server location
	
	模块四:Nginx请求限制
			连接频率限制:limit_conn_moudle
			请求频率限制:limit_req_moudle
			注意:一次连接可多次请求
					http协议版本
					http1.0				tcp不能复用
					http1.1				顺序性复用
					http2.0				多路复用

			连接限制语法:limit_conn_zone key zone=name:size;   key为内置变量如IP  size为空间大小
				default -
				context http
				
				limit_conn zone  number 
				default -
				context http server location

				配置栗子: 客户端 同一IP地址 只能有一个连接
				limit_conn_zone $brnanry_remote_addr zone=conn_zone:1m;
						location /{
							limit_conn conn_zone 1;
						}
			请求限制语法: limit_req_zone key  zone=name:size rate=rate;  rate为限制个数
						default	-
						context http
						
						limit_req zone=name [burst=number] [nodelay]  
						default
						context http server location
			
				配置栗子:每1s内同一客户端IP只能请求一次
						limit_req_zone $brnanry_remote_addr zone=req_zone:1m rate=1r/s;
						location /{
							//根据需求三选一  burst参数 下一秒响应  nodelay直接返回503
							limit_req zone=req_zone burst=3 nodelay;
							limit_req zone=req_zone burst=3;
							limit_req zone=req_zone;
						}
			
			apache 基金会  AB 压测  ab压测栗子: ab -n 50 -c 20 url     -n 请求书  -c并发数

	模块五:访问控制模块
			基于IP访问控制:http_access_module
				语法:allow address | CIDR | unix: |all; 
					default -
					context http server location limit_except
					
					deny address | CIDR | unix: |all; 
					default -
					context http server location limit_except
				栗子:
				~模式匹配 该IP不可访问 其他可以访问  可以使网段  192.168.17.0/24
				deny ip; 
				allow all;
				
			局限性:不是客户端与服务端直接连接 IP就会拿到代理的IP 不会直接拿到客户端的IP  没起到作用
				
			基于用户新人登录:http_auth_basic_modul
				语法:auth_basic string | off
				default auth_basic off
				context http server location limit_except
				//file 文件路径  用户名 密码信息文件
				
				auth_basic_user_file file;
				defaykt -
				context http server location limit_except
				栗子:
				auth_basic "hello";
				auth_basic_user_file file;
			
			局限性:不好管理

第三方模块 lua+nginx

场景实践篇

一、静态资源web服务器
	静态资源:html css js  
			jepg gif png
			flv mp4
			txt  等任意文件
静态资源服务场景 CDN
	DNS-北京-(静态资源回源)-新疆
	配置语法:文件读取
	sendfile on | off
	default sendoff off;
	context http ,server ,location, if on location	
			
	引读:with-file-aio 异步文件读取

	tcp_nopush  sendfile开启情况下 提高网络提高效率
	tcp_nopush on |off;
	default off;
	context http server location
	
	tcp_nodelay  前提 keeplive
	syntax tcp_nodelay on |off
	default tcp_nodelay  on
	context http server location
	
	3.压缩:gzip  on |off   
	default gzip off;
	context: http server location,if on location
	
	压缩比
	gzip_comp_level level;
	default:gzip_comp_level 1;
	context: http server location
	
	gzip_http_version 1.0|1.1;
	default gzip_http_version 1.1;
	context: http server location
	
	4.扩展nginx压缩模块  gzip_static on; 预读   需要手动压缩 文件  不需要实时压缩   双份文件  硬盘容量  IO  使用少
	http_gzip_static_moudle 预读gzip功能
	http_gunzip_moudle 应用支持gunzip压缩方式
	 
	配置栗子: gzip 对文本压缩效率最高
	以什么结尾
	location ~ .*\.(jpg|png)$ {
	}
	以什么开题开头
	location ~ ^/download{
	tcp_nopush on;
	gzip on;
	gizp_http_version 1.1;
	gzip_comp_level 2;
	gzip_types text/plain image/gif image/png;
	}
	

二、浏览器缓存 特殊头信息和服务端验证   部分浏览器机制 cache-contorl max-age=0  每次都要确定 last-modified  返回是 304还是200
	客户端无缓存
	浏览器请求-无缓存-请求web服务器-请求响应协商-呈现
	
	客户端有缓存
	浏览器请求-有缓存-校验过期-请求响应协商-呈现

	校验过期机制:
	header						expires cache-control(max-age)  判断是否过期 才去etag然后去last-modified
	协议中etag头信息校验			etag 
	last-modified头信息验证		last-modified  (客户端文件日期 与服务端文件日期不一致 就会跟新)

	nginx 配置语法:expires
	添加 cache-control expires
	syntax expires [modified] time ;
	expires epoch |max|off;
	default expires off;
	context: http ,server ,location ,if in location

三、跨域访问

	为什么浏览器禁止跨域访问?
		安全考虑,同源策略。容易出现 csrf(跨站攻击)
	nginx怎么做 打开
	syntax:add_header name value [alawys];
	default -
	context http, server, location, if in location
	
	Access-Control-Allow-Origin
	
	配置栗子:
	add_header Access-Control-Allow-Origin http://www.baidu.com
	add_header Access-Control-Allow-Methods GET,POST,PUT;


四、防盗链
	目的:防止网站资源被盗用
	首要任务:哪些是正常用户 哪些不是
	http_refer防盗链配置模块
	syntax:vaild_referers none | blocked | server_names | string ...;
	default -
	context : server location
	配置栗子:
		valid_referers none blocked 192.168.17.22;    支持正则匹配方式
		if($invalid_referer){
		return 403;
		}

代理服务

四层代理:tcp、udp
七层代理:http

http代理
	正向代理: 对象是客户端  场景 比如说服务器不支持客户端IP访问  可以考虑正向代理 先访问正向代理机 代理到服务器 在返回
	反向代理:对象是服务端
	
	配置语法:
	syntax: proxy_pass url;  支持 http https
	default -;
	context:location , if in location , limit_except

	location /test {
	 	proxy_pass url;
	}
	
	缓冲区:proxy_buffering  on | off;
	default on;
	context : http server location
	扩展:proxy_buffer_size  proxy_buffers proxy_busy_buffer_size

跳转重定向
	syntax:proxy_redirect default;proxy_redirect off; proxy_redirect redirect replacement;
	default default;
	context http server location
	
头信息
	syntax proxy_set_header file value;
	default proxy_set_header Host $proxy_host;
			proxy_set_header Connection close;
	context:http server location 
	扩展:proxy_hide_header proxy_set_body
	
超时
	syntax proxy_connent_timeout time;
	default	60s;
	context:http server location 
	扩展:proxy_send_timeout proxy_read_timeout
	
企业配置:栗子
prox_pass;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_buffering on;
proxy_connect_timeout 180s;
proxy_send_timeout 180s;
proxy_read_timeout 180s;
proxy_buffer_size 64k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 256k ;
proxy_temp_file_write_size 256k;
proxy_redirect off;
配置较长 可以 include 引入文件 

负载均衡 SLB

proxy_pass -----> upstream server;

upstream语法:
syntax:upstream name {...}
default -
context http

参数:
	down 不参与负载均衡
	backup 预留备份服务器
	max_fails 允许请求失败次数
	fail_timeout 经过max_fails后 服务暂停时间
	max_conns 限制最大接受的连接数

栗子: 默认轮训
	upstream server1 {
		server 22.86.181.33:8088 weight=5 max_fails=5 fail_timeout=60s;
		server 22.86.182.33:8088 max_fails=5 fail_timeout=60s;
		server 22.86.183.33:8088 max_fails=5 fail_timeout=60s;
		
		server 22.86.184.33:8088 max_fails=5 fail_timeout=60s backup;
		server 22.86.185.33:8088 max_fails=5 fail_timeout=60s backup; 备份节点
		keepalive 128;
	}

	localhost / {
		proxy_pass http://server1/test;
	}


nginx 调度算法:
	默认轮训
	权重					weight=5
	ip_hash   			来自同一IP会分配到同一服务器  
	least_conn 			最少连接数
	url_hash 			每个URL定向到一个服务器
	hash关键数值			hash自定义的key

缓存服务器

代理缓存: 客户端访问 nginx-- nginx没有缓存---- 请求服务端-----  nginx本地缓存 -------返回客户端

核心配置:
proxy_cache_path
default -
context http

proxy_cache zone |off;
default off;
context http server location

缓存过期周期
proxy_cache_valid [code ...] time;
default -;
context http server location 

缓存维度
proxy_Cache_key string;
default $scheme$proxy_host$request_uri;
context http server location 

配置栗子:inactive60m 60分钟不活跃 删掉 最大10g触发
proxy_cache_path /opt/cache levels=1:2 key_zone=mycache:10m max_size10g inactive60m use_temp_path=off; 
location /{
	proxy_cache mycache;
	proxy_cache_vaild 200 304 12h;   200 304 12小时
	proxy_cache_vaild any 10m;   其他10分钟
	proxy_cache_key $host$uri$is_args$args;
	add_header Nginx_Cache "$upstream_cache_status";
	
	proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;  

//如果有单服务有这些错误 直接下一台
}

补充:清理指定缓存
	官方没有:
	第三方模块 ngx_cache_purge

如何让部分页面不缓存
	可以:
	proxy_no_cache string ...;  支持正则
	default -;
	context http server location

分片请求:1.9版本之后支持
	大文件分片请求
	syntax: slice size;
	default: slice 0;
	context: http ,server,location

	http_slice_moudle;
	优势:每个子请求都会形成一个文件,一个请求断了 其他请求不受影响
	缺点:文件大 分片成很多文件 增加链接 和消耗
posted @   苏子墨  阅读(66)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示