Nginx笔记

开源且高效、可靠的HTTP中间件、web服务器和代理服务器

基于Nginx的中间件架构

环境

  • 确认系统网络/yum可用/关闭iptables规则/停用selinux
iptable -L
iptable -F
iptable -t nat -L
iptable -t nat -F

getenforce
setenforce 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/logrotate.d/nginx配置文件Nginx日志轮转,用于logrotate服务的日志切割
/etc/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
编译选项作用
–prefix=
–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变量

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.service
    systemctl reload nginx.service

Nginx指令

Nginx模块

  • Nginx官方模块
    参考:Modules reference

    编译选项作用SyntaxDefaultContext
    –with-http_stub_status_moduleNginx客户端状态stub_statusserver,location
    –with-http_random_index_module目录中选择一个随机主页random_index on | offrandom_index offlocation
    –with-http_sub_moduleHTTP内容替换① 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的请求限制

编译选项作用SyntaxDefaultContext说明
–with-http_limit_conn_module连接频率限制① limit_conn_zone key zone=name:size;
② 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请求。

Created with Raphaël 2.1.0 Client Client Server Server SYN SYN,ACK ACK HTTP(Req) HTTP(Resp) FIN ACK FIN ACK

Nginx的访问控制

编译选项作用SyntaxDefaultContext说明
–with-http_access_module基于IP的访问控制allow address | CIDR | unix: | all;
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_auth_basic_module的局限性:
    一、用户信息依赖文件方式;
    二、操作管理机械,效率低下
    解决方案:

    1. Nginx结合LUA实现高效验证;
    2. Nginx和LDAP打通,利用nginx-auth-ldap模块

注意:Windows操作系统下使用auth_basic_user_file指令时,文件路径文件分隔符要用/,而不是\

Windows下操作Nginx指令

参考:nginx for Windows

start nginx.exe
tasklist -fi "imagename eq nginx.exe"
命令说明
nginx -s stopfast shutdown
nginx -s quitgraceful shutdown
nginx -s reloadchanging configuration,
starting new worker processes with a new configuration,
graceful shutdown of old worker processes
nginx -s reopenre-opening log files

进阶学习 - 常见Nginx中间架构

静态资源web服务

Created with Raphaël 2.1.0 客户 客户 Nginx Nginx 静态文件存储 静态文件存储 ReQ:jpeg、htm/html、flv...
  • 静态资源类型:非服务器动态运行生成的文件
    浏览器端渲染: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对文本文件的压缩效果最明显的

  • 扩展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

动态缓存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值