以下内容主要整理自 nginx官网文档

基本命令

nginx -h 帮助

nginx -c 指定配置文件

nginx -s stop|quit|reload|reopen

nginx -t 测试配置是否有语法错误

nginx -v 打印版本信息

热部署流程

  1. 备份原二进制文件为nginx.old
  2. 下载新版nginx编译出二进制文件
  3. 将新二进制文件覆盖过来 cp -f
  4. 找到nginx进程(master进程和worker)ps ef | grep nginx
  5. kill -USR2 master进程号
    • 此时再次查看会出现2组master和worker,老的进程已经不再接收新的请求了
  6. kill -WINCH 老的master进程号
    • 此时会把老的worker进程全部停掉,但是老的master会保留
    • 因为如果新版本有兼容问题可以优雅的回退到老版本
  7. nginx -s reload

日志切割

  1. mv access.log access_$(date -d yesterday +"%Y%m%d").log
  2. nginx -s reopen

展示文件目录

Syntax: index file ...;
Default: index index.html;
Context: http, server, location

Syntax: autoindex on | off;
Default: autoindex off;
Context: http, server, location

# index优先级高于auto_index且默认值index.html
location / {
    index dontfindme.html;
    autoindex on;
}

root和alias

Syntax: root path;
Default: root html;
Context: http, server, location, if in location

Syntax: alias path;
Default: —
Context: location

# www.test.com/aaa/root.txt 会把location后面的目录带过去
# /Users/tangxiaofeng/wwwroot/public/root/aaa/root.txt
location /aaa {
    root /Users/tangxiaofeng/wwwroot/public/root;
}

# www.test.com/bbb/alias.txt 不会把location后面的目录带过去
# /Users/tangxiaofeng/wwwroot/public/alias/alias.txt
location /bbb {
    alias /Users/tangxiaofeng/wwwroot/public/alias;
}

传输数据压缩


Syntax: gzip on | off;
Default: gzip off;
Context: http, server, location, if in location

gzip on;
# 小于多少字节不压缩
gzip_min_length 1024;
# 压缩级别 默认1(1最快压缩率最低,9最慢压缩率最高)
gzip_comp_level 2;
# 需要压缩的 mime-type 类型
gzip_types text/plain application/x-javascript text/css application/xml text/javascript image/jpeg;

server_name匹配

Syntax: server_name name ...;
Default: server_name "";
Context: server

# 优先级 精确匹配 > *在前的泛域名 > *在后的泛域名 > 正则(配置文件顺序)
# 都匹配不到的话,看 listen 有没有配置default
# listen没有设置default的话,进入第一个server
server_name www.fengfengphp.com *.fengfengphp.com www.fengfengphp.*;

账号密码登录

Syntax: auth_basic string | off;
Default: auth_basic off;
Context: http, server, location, limit_except

Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except

# 生成账号密码文件工具 yum install httpd-tools
htpasswd -c file -b user pass

location /{
    auth_basic "请输入账号密码:";
    auth_basic_user_file examples/passwd.db;
}

http请求相关的变量

# URL中某个具体参数的值
# 参数:a=3&b=4 则:$args_a=3 $args_b=4
$arg_参数名
# 全部URL参数
$args | $query_string
# 如果请求的URL中有参数则返回否则返回空
$is_args
# HTTP请求中标识包体长度的 Content-Length头部的值
$content_length
# 标识请求包体类型的 Content-Type头部的值
$content_type
# document_url 请求的URL(不包括?后的参数)
$uri
# 请求的URL(包括URI以及完整的参数)
$request_uri
# 协议名 如:HTTP、HTTPS
$scheme
# 请求方法 如:GET、POST
$request_method
# 所有请求内容的大小,包括请求行、头部、包体等
$request_length
# 由 HTTP Basic Authentication协议传入的用户名
$remote_user
# 如果请求包体很小就不用放文件中 如果设置 client_body_in_file_only则强制所有包体存入文件,且可决定是否删除
$request_body_file
# 请求中的包体,这个变量当且仅当使用反向代理,并且设定用内存暂存包体时才有效
$request_body
# 原始的url请求,含有方法与协议版本 如: GET /?a=1&b=22 HTTP/1.1
$request
# 先从请求行中获取,如果含有Host头部,则用其值替换掉请求行中的主机名,如果前两者都取不到,则使用匹配上的server_name
$host
# 返回一个具体的请求中的头部的值
# 特殊(NGINX会做微小处理)$http_host $http_user_agent $http_referer $http_via $http_x_forwarded_for $http_cookie
# 除了特殊以外的,获取HTTP请求head中的原始的值
$http_头部名字

tcp连接相关的变量

# 客户端地址的整形格式,对于IPv4是4字节,对于IPv6是16字节
$binary_remote_addr
# 递增的连接序号
$connection
# 当前连接上执行过的请求数,对keepalive连接有意义
$connection_requests
# 客户端地址
$remote_addr
# 客户端端口
$remote_port
# 若使用了proxy_protocol协议返回协议中的地址,否则返回空
$proxy_protocol_addr
# 若使用了proxy_protocol协议返回协议中的端口,否则返回空
$proxy_protocol_port
# 服务器端地址
$server_addr
# 服务器端端口
$server_port
# tcp内核层参数
$tcpinfo_rtt $tcpinfo_rttvar $tcpinfo_snd_cwnd $tcpinfo_rcv_space
# 服务器端协议,如HTTP/1.1
$server_protocol

nginx在处理请求过程中产生的变量

# 请求处理到现在的耗时,单位为秒,精确到毫秒
$request_time
# 匹配上请求的server_name值
$server_name
# 如果开启了TLS/SSL,则返回on,否则返回空
$https
# 若请求处理完则返回OK,否则返回空
$request_completion
# 以16进制输出的请求标识id,该id共含有16个字节,是随机生成的
$request_id
# 待访问文件的完整路径
$request_filename
# 由URI和root/alias规则生成的文件夹路径
$document_root
# 将document_root中的软链接等换成真实路径
$realpath_root
# 返回客户端响应时的限速上限,单位为每秒字节数。可以通过set指令修改对请求产生效果
# set $limit_rate 1k;
$limit_rate

发送http响应时相关的变量

# 响应中body包体的长度
$body_bytes_sent
# 全部http响应的长度 
$bytes_sent
# http响应中的返回码
$status
# 把响应结尾内容里的值返回
$sent_trailer_名字
# 把响应结尾的头部的值返回
# nginx会做特殊处理的变量如下
# $sent_http_content_type $sent_http_content_length $sent_http_location 
# $sent_http_last_modified $sent_http_connection $sent_http_keep_alive 
# $sent_http_transfer_encoding $sent_http_cache_control $sent_http_link
$sent_http_头部名称

nginx系统变量

# 以本地时间标准输出的当前时间,如:14/Nov/2019:15:15:15 +0800
$time_local
# 使用 ISO 8601 标准输出的当前时间 如:2019-12-12T13:12:22+08:00
$time_iso8601
# nginx版本号
$nginx_version
# 所属worker进程的进程id
$pid
# 使用了管道则返回p,否则返回.
$pipe
# 所在服务器的主机名,与hostname命令输出一致
$hostname
# 1970年1月1日到现在的时间,单位为秒,小数点后精确到毫秒
$msec

if指令

# 直接判断
if ($is_args) {
    return 1;
}

# 字符串匹配 = 或 !=
if($request_method = POST) {
    return 405;
}

# 正则匹配 ~或!~  不区分大小写 ~*或!~*
if($request_uri !~* /cms/index.php) {
    return 404;
}

# 文件是否存在  -f 或 !-f
if(!-f /home/wwwroot/public/index.php) {
    return 404;
}

# 目录是否存在  -d 或  !-d
if(!-d /home/wwwroot/public) {
    return 404;
}

# 文件、目录、软连接是否存在 -e 或 !-e
if(-e /home/wwwroot/public/index.exe) {
    return 404;
}

# 是否可执行文件 -x 或 !-x   
if(-x /home/wwwroot/public/index.exe) {
    return 404;
}

日志

# 日志格式 
# log_format 自定义名称 日志内容(以下是默认配置,可以使用其它变量)
log_format my_name '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';

# 记录日志
# access_log | error_log 日志路径 对应的format_name
access_log  /home/wwwlogs/access.log  my_name;

return和404

# 直接返回
return code [text];
return code URL;
# 不写code 默认302。301永久重定向、302临时重定向
return URL;

# 返回404
error_page 404 /404.html;
error_page 404=200 /empty.jpg;
error_page 404=/404.php;
error_page 404=301 http://test.ee.com;
location / {
    error_page 404 = @fallback;
}
localtion @fallback {
    proxy_pass http://backend;
}

rewrite

# flag如下:   
# last 用 replacement 这个URI进行新的location匹配
# break 停止指定执行
# redirect 302重定向
# permanent 301重定向
Syntax: rewrite regex replacement [flag];
Default: —
Context: server, location, if

location

# = 精确匹配
# 前缀字符串匹配,匹配上后不再进行正则匹配
# @ 内部跳转
# ~ | ~* 正则匹配,后者不区分大小写
# 优先级:精确匹配= > 使用^~ > conf文件顺序 > 最长匹配
Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }
location @name { ... }
Default: —
Context: server, location

# 合并连续的/符号
Syntax: merge_slashes on | off;
Default: merge_slashes on;
Context: http, server

限制并发连接数

# 定义共享内存 Context http
limit_conn_zone key zone=name:size;
# 限制并发连接数 Context http,server,location
limit_conn zone number;
# 限制并发后的日志级别 默认error Context http,server,location
limit_conn_log_level info | notice | warn | error;
# 限制并发后的返回错误码 默认503
limit_conn_status code;

# for example
limit_conn_zone $binary_remote_addr zone=addr:10m;
localtion / {
    limit_conn_status 500;
    limit_conn_log_level warn;
    limit_rate 50;          #限制每秒只返回50字节,便于测试并发
    limit_conn addr 1;      #限制并发连接数1 便于测试
}

限制请求速率

# 定义共享内存 Context http 
# rate: r/s | r/m; request/second
limit_req_zone key zone=name:size rate=rate;
# Context: http,server,location
# burst 默认0 nodelay对burst中的请求不再采用延时处理,而是立刻处理
limit_req zone=name [burst=number][nodelay];
# Context: http, server, location
# level: info | notice | warn | error; default error
limit_req_log level
# Context: http, server, location; default 503
limit_req_status code

# for example
limit_req_zone $binary_romote_addr zone=one:10m rate=2r/m;
location / {
    #limit_req zone=one burst=3 nodelay;
    limit_req zone=one;
}

allow和deny

allow address|CIDR|unix:|all;
Context: http,server,location,limit_except

deny address|CIDR|unix:|all;
Context: http,server,location,limit_except

# 顺序执行,满足一条就不再往下走
location / {
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 10.1.1.0/16;
    allow 2001:0db8::/32;
    deny all;
}

results matching ""

    No results matching ""