Nginx笔记
来源:互联网 发布:python语言及其应用 编辑:程序博客网 时间:2024/06/11 02:57
开源且高效、可靠的HTTP中间件、web服务器和代理服务器
基于Nginx的中间件架构
环境
- 确认系统网络/yum可用/关闭iptables规则/停用selinux
iptable -Liptable -Fiptable -t nat -Liptable -t nat -Fgetenforcesetenforce 0
- 一次初始化
cd /opt; mkdir app download logs work backup
- 安装基础依赖
yum -y install gcc gcc-c++ autoconfi pcre pcre-devel make automake
- 安装基本工具
yum -y install wget httpd-tools vim
常见HTTP服务
- HTTPD - Apache基金会
- IIS - 微软
- GWS - Google
为什么选择nginx
- IO多路复用epoll(一个线程内并发交替地顺序完成;实现方式select/poll/epoll)
- 轻量级(功能模块少、代码模块化)
- CPU亲和(affinity,CPU核心和Nginx工作进程worker进行绑定,减少切换cpu的cache miss)
- sendfile机制(Linux零拷贝,kernel space传输)
Nginx安装
- Mainline Version - 开发版本
- Stable Version - 稳定版
- Legacy Version - 历史版本
样本源安装方式
安装目录:
rpm -ql nginx
目录
/etc/nginx/nginx.conf
/etc/nginx/conf.d
/etc/nginx/conf.d/default.conf 目录、配置文件 Nginx主配置文件 /etc/nginx/fastcgi_params
/etc/nginx/uwsgi_params
/etc/nginx/scgi_params 配置文件 cgi配置相关,fastcgi配置 /etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf 配置文件 编码转换、映射转换文件 /etc/nginx/mime.types 配置文件 设置http协议的Content-Type与扩展名对应关系 /usr/lib/sustemd/system/nginx-debug.service
/usr/lib/sustemd/system/nginx.service
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug 配置文件 用于配置系统守护进程管理器管理方式 /usr/lib64/nginxmodules
/etc/nginx/modules 目录 Nginx模块目录 /user/sbin/nginx
/usr/sbin/nginx-debug 命令 Nginx服务的启动管理的终端命令 /usr/share/doc/nginx-x.xx.x
/usr/share/doc/nginx-x.xx.x/COPYRIGHT
/usr/share/man/man8/nginx.8.gz 文件、目录 Nginx的手册和帮忙文件 /var/cache/nginx 目录 Nginx的缓存目录 /var/log/nginx 目录 Nginx的日志目录
编译参数
查看编译参数:
nginx -V
–conf-path=conf/nginx.conf
–pid-path=logs/nginx.pid
–http-log-path=logs/access.log
–error-log-path=logs/error.log
–sbin-path=nginx.exe
–modules-path=
–lock-path= 安装目标目录或路径 –http-client-body-temp-path=temp/client_body_temp
–http-proxy-temp-path=temp/proxy_temp
–http-fastcgi-temp-path=temp/fastcgi_temp
–http-scgi-temp-path=temp/scgi_temp
–http-uwsgi-temp-path=temp/uwsgi_temp 执行对应模块时,Nginx所保留的临时性文件 –user=nginx
–group=nginx 设置Nginx进程启动的用户和组用户 –with-cc-opt= 设置额外的参数添加到CFLAGS变量中 –with-ld-opt= 设置附加参数,链接系统库
HTTP请求
curl -v http://www.baidu.com >/dev/null
Nginx日志类型
- error.log
- access.log
通过log_format信息配置
Nginx变量
- Http请求变量:
- arg_XXX:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_arg_
- http_XXX:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_
- sent_http_XXX:http://nginx.org/en/docs/http/ngx_http_core_module.html#var_sent_http_
- Nginx内置变量
- Alphabetical index of variables
- 自定义变量
Nginx命令行参数
参考:Command-line parameters
测试配置文件是否有语法错误:
nginx -t -c conf/nginx.conf
重新加载配置:
nginx -s reload -c conf/nginx.conf
查看nginx进程:
ps -aux|grep nginx
重启nginx,使nginx.conf/default.conf文件的配置生效:
systemctl restart nginx.servicesystemctl reload nginx.service
Nginx指令
- Alphabetical index of directives
Nginx模块
Nginx官方模块
参考:Modules reference编译选项 作用 Syntax Default Context –with-http_stub_status_module Nginx客户端状态 stub_status – server,location –with-http_random_index_module 目录中选择一个随机主页 random_index on | off random_index off location –with-http_sub_module HTTP内容替换 ① sub_filter string replacement;
② sub_filter_last_modified on | off;
③ sub_filter_once on | off ① –
②sub_filter_last_modified off
③sub_filter_once on http,server,location第三方模块
Nginx的请求限制
② limit_conn zone number; – ①http;
②http, server, location 1M共享空间可以保存3.2万个32位的状态,1.6万个64位的状态 –with-http_limit_req_module 请求频率限制 ①limit_req_zone key zone=name:size rate=rate;
②limit_req zone=name [burst=number] [nodelay]; – ①http;
②http, server, location
HTTP1.0:TCP不能复用
HTTP1.1:顺序性TCP复用
HTTP2.0:多路TCP复用
HTTP请求建立在一次TCP连接基础上;
一次TCP请求至少产生一次HTTP请求。
压测工具ab
参考:
超实用压力测试工具-ab工具
如何从Apache官网下载windows版apache服务器ab -n 50 -c 20 http://localhost:8080
Nginx的访问控制
②deny address | CIDR | unix: | all; – http, server, location, limit_except –with-http_x_forwarded_for –with-http_auth_basic_module 基于用户的信任登录 ①auth_basic string | off;
②auth_basic_user_file file; – http, server, location, limit_except
使用http_access_module的局限性:只能通过
$remote_addr
控制信任
访问:Client(IP1)->Proxy(IP2)->Nginx Server(IP3)
http_access_module控制结果:$remote_addr
是IP2,而不是IP1
考虑:- 方法一:采用别的HTTP头信息控制访问,如:http_x_forwarded_for
区别:x_forwarded_for=IP1,IP2,即Client IP、Proxy[N] IP - 方法二:结合geo模块
- 方法三:通过HTTP自定义变量传递
- 方法一:采用别的HTTP头信息控制访问,如:http_x_forwarded_for
使用http_auth_basic_module的局限性:
一、用户信息依赖文件方式;
二、操作管理机械,效率低下
解决方案:- Nginx结合LUA实现高效验证;
- Nginx和LDAP打通,利用nginx-auth-ldap模块
注意:Windows操作系统下使用auth_basic_user_file指令时,文件路径文件分隔符要用
/
,而不是\
- htpasswd工具,生成账号和加密密码
参考:htpasswd命令
Windows下操作Nginx指令
参考:nginx for Windows
start nginx.exetasklist -fi "imagename eq nginx.exe"
starting new worker processes with a new configuration,
graceful shutdown of old worker processes nginx -s reopen re-opening log files
查看端口被占用
参考:如何查看某个端口被谁占用netstat -aon|findstr "49157"tasklist|findstr "2720"taskkill /f /t /im soft.exe
进阶学习 - 常见Nginx中间架构
静态资源web服务
静态资源类型:非服务器动态运行生成的文件
浏览器端渲染:html/css/js
图片:jpeg/gif/png
视频:flv/mpeg
文件:txt/doc/xlsx/ppt静态资源服务场景 - CDN
如:北京的用户请求静态资源文件,但文件在新疆的资源存储中心,距离长,存在延时很长。因此,资源存储中心通过静态资源回源的方式,在北京的Nginx代理服务器上,分发存储了一份镜像。用户可以直接通过代理服务器获取资源。配置语法 - 文件读取
参考:sendfile
Syntax: sendfile on | off;
Default: sendfile off;
Context: http, server, location, if in location引读:–with-file-aio 异步文件读取
配置语法 - tcp_nopush
sendfile开启的情况下,提高网络包的传输效率
参考:tcp_nopush
Syntax: tcp_nopush on | off;
Default: tcp_nopush off;
Context: http, server, location配置语法 - tcp_nodelay
keepalive连接(强连接)下,实时性要求比较高时,提供网络包传输速率
参考:tcp_nodelay
Syntax: tcp_nodelay on | off;
Default: tcp_nodelay on;
Context: http, server, location配置语法 - 压缩
压缩传输,对应模块:–with-http_gzip_module
Nginx服务端压缩,浏览器解压,减少中间网络传输消耗,减少带宽、提高效能
参考:gzip- gzip
Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location - gzip_comp_level,压缩比
Syntax: gzip_comp_level level;
Default: gzip_comp_level 1;
Context: http, server, location - gzip_http_version,压缩协议版本
Syntax: gzip_http_version 1.0 | 1.1;
Default: gzip_http_version 1.1;
Context: http, server, location
Tips:一般情况下,gzip对文本文件的压缩效果最明显的
- gzip
扩展Nginx压缩模块
http_gzip_static_module,预读gzip功能
如读取1.html文件,先尝试读取1.html.gz预压缩文件
Nginx编译启动则加载–with-http_gzip_static_module模块
参考:ngx_http_gzip_static_module
Syntax: gzip_static on | off | always;
Default: gzip_static off;
Context: http, server, location*.gz文件通过gzip工具(http://www.gzip.org/)生成
http_gunzip_module,应用支持gunzip的压缩方式
用于解决少部分浏览器无法支持gzip解压方式问题,场景很少
参考:ngx_http_gzip_module
代理服务
负载均衡调度器SLB
动态缓存
- nginx笔记:nginx安装
- Nginx笔记
- nginx笔记
- nginx笔记
- nginx笔记
- nginx 笔记
- nginx笔记
- nginx笔记
- nginx笔记
- nginx 笔记
- nginx笔记
- Nginx笔记
- 【Nginx笔记】nginx配置文件详解
- nginx学习笔记
- Nginx学习笔记
- nginx研究笔记(1)
- nginx配置文件笔记
- nginx 源码 笔记
- 运维三:如何把项目部署到服务器?
- 《kubernetes-1.8.0》01-环境准备
- 验证52域数据是否被篡改且根据52域算出密码
- 长按Home键唤起Google Now
- Frequent values UVA
- Nginx笔记
- 从零开始搭建ELK+GPE监控预警系统
- transformClassesWithJarMergingForRelease解决办法 解决版本冲突 解决jar包冲突
- 迷宫求解问题
- H5部署上线测试完成有点小小的成就感
- 基于R-CNN的物体检测
- 单链表建立
- stm32 memcpy 错位问题
- Fragment写主页