nginx配置

本文详细介绍了Nginx的热部署过程,包括备份、替换二进制文件及信号控制。还讨论了配置文件、常用变量及其在HTTP请求中的应用。重点讲解了Nginx的负载均衡策略,如权重分配、故障转移及多种负载均衡算法,并涉及反向代理和缓存管理,包括缓存策略、缓存状态及清理。此外,还提到了性能优化技巧,如TCP连接参数调整和FastOpen功能的启用。
摘要由CSDN通过智能技术生成

/usr/local/nginx/sbin/nginx -t(检查conf正确性) -s reload/stop/quit -c  /opt/nginx/conf/nginx.conf -V(看版本)

1.热部署升级版本:
   启动nginx;
   确保新版本与旧版本目录结构一致,新版本make但不要install,nginx二进制文件在/objs下;
   /sbin目录下备份:cp nginx nginx.bak
   cp -f /nginx-new/objs/nginx /usr/local/nginx/sbin/
   ps -ef | grep nginx
   kill -s SIGUSR2 27394#重启
   kill -s SIGWINCH 27394#回滚:kill -s SIGHUP 27394(并SIGQUIT新的)
   kill -s SIGQUIT 27394
2.配置文件:nginx.conf_weixin_46078854的博客-CSDN博客
3.常用变量:
使用:eg:return {200 "uri: $uri"}; set $limit_rate 50;
1>.TCP连接:
remote_addr客户端IP地址remote_port客户端端口binary_remote_addr二进制客户端IP地址
server_addr服务端IP地址server_port服务端端口server_protocol服务端协议
connection TCP连接的序号,递增connection_request TCP连接当前的请求数量
proxy_protocol_addr若使用了proxy_protocol协议,则返回协议中地址,否则返回空
proxy_protocol_port若使用了proxy_protocol协议,则返回协议中端口,否则返回空
2>.发送http请求:
uri请求的URL,不包含参数request_uri请求的URL,包含参数
scheme协议名,http或https
request_method请求方法request_length全部请求的长度,包括请求行、请求头、请求体
args 全部参数字符串 arg_参数名 特定参数值 is_args URL中有参数,则返回?否则空 query_string 与args相同 
remote_user 由HTTP Basic Authentication协议传入的用户名eg:curl admin:1234
host先看请求行,再看请求头,最后找server_name
http_user_agent用户浏览器http_referer从哪些链接过来的请求
http_via经过一层代理服务器,添加对应代理服务器的信息
http_x_forwarded_for获取用户真实IP http_cookie用户cookie
3>.处理http请求:
request_time 处理请求已耗费的时间request_completion请求处理完成返回OK,否则空
server_name匹配上请求的server_name值
https若开启https,则返回on,否则空
request_filename磁盘文件系统待访问文件的完整路径
document_root由URI和root/alias规则生成的文件夹路径
realpath_root将document_root中的软链接换成真实路径
limit_rate返回响应时的速度上限值,可通过set修改。
4>.反向代理,动静分离,负载均衡
upstream模块用于定义上游服务的相关信息:
    upstream $hostname{
            server address [parameters];
    }
    location / {proxy_pass http://$hostname/路径参数;}
(1).server 
address:  ip:port或url
parameters:
  weight=number 权重,默认1
  max_conns=number上游服务器最大并发连接数
  fail_timeout=10s 服务器不可用的判定时间
  max_fails=number 服务器不可用的检查次数
  backup 备份服务器,仅当其他服务器都不可用时
  down 标记服务器长期不可用,离线维护
(2).zone test 10M;定义共享内存,用于跨worker子进程
(3).keepalive 10;对上游服务启用长连接数量
(4).keepalive_requests 20;单个长连接最多请求个数
(5).keepalive_timeout 60s;空闲长连接的最长保持时间 
(6).hash key [consistent]哈希负载均衡算法
(7).ip_hash依据客户端IP的哈希负载均衡算法
(8).least_conn最少连接数负载均衡算法,与zone连用
(9).least_time最短响应时间负载均衡算法
(10).random随机负载均衡算法
负载均衡上游服务器返回异常的容错机制:
proxy_next_upstream error|timeout|invalid_header|http_500/502/503/504/403/404/429|noidempotent|off
proxy_next_upstream_timeout times;
proxy_next_upstream_tries number;
5>.请求:
接收用户请求包体:
proxy_request_buffering on缓存,吞吐量高并发能力低off及时响应减少磁盘IO
client_body_in_single_buffer on|off缓存在连续的空间,可以提高性能
client_body_temp_path client_body_temp缓存目录
client_body_in_file_only on所有都存|clean清除完成的|off禁止缓存
client_body_timeout time等待发送包体时间
client_max_body_size 10k/1M限制请求包体大小
client_body_buffer_size 8k|16k超过8k|16k就缓存 
更改发往上游的用户请求:
请求行:GET /index.html HTTP/1.0请求头: 请求体:参数
proxy_method method;proxy_http_version 1.0/1.1;
proxy_set_header field value;proxy_pass_request_header on|off;
proxy_set_body value;proxy_pass_request_body on|off;
长连接设置:
proxy_http_version 1.1;proxy_set_header Connection alive|close;
客户端到Nginx:keepalive_timeout 65;Nginx到上游服务:keepalive 32;keepalive_requests 100;keepalive_timeout 60s;
Nginx到上游服务器建立连接超时时间:proxy_connect_timeout time;
复用tcp层的长连接:proxy_socket_keepalive on|off;
没有发送任何内容的超时时间:proxy_send_timeout time;
代理服务器主动忽略客户端放弃的连接:proxy_ignore_client_bort on|off;
6>.缓存:
proxy_cache zone|off;
http段:proxy_cache_path path [level=levels] [use_temp_path=on|off]
             keys_zone=name:size [inactive=time] [max_siz/301=size] 
             [manager files=number] [manager sleep=time] [manager threshold=time] 
             [loader files=number] [loader sleep=time] [loader threshold=time]
             [purger=on/off] [purger fles=number] [purger sleep=time] [purger threshold=time]; 
proxy_cache_key $scheme$proxy_host$request_uri;
proxy_cache_valid [code...] time;#只对200、301、302响应码缓存
变量:upstream_cache_status MISS:未命中缓存 HIT:命中缓存EXPIRED:缓存过期STALE:命中了陈旧缓存
          REVALIDDATED:Nginx验证陈旧缓存依然有效 UPDATING:内容陈旧,但正在更新 BYPASS:响应从原始服务器获取
不缓存上游服务特定内容:proxy_no_cache string;proxy_cache_bypass string;
缓存失效降低上游压力机制:
(1).合并源请求:proxy_cache_lock on|off;  proxy_cache_lock_timeout time;proxy_cache_lock_age time;
(2).启用陈旧缓存:proxy_cache_use_stale  error|timeout|invalid_header|updating|http_500/502/503/504/403/404/429|off...;
                            proxy_cache_background_update on|off;
清除缓存:proxy_cache_purge zone_name key;
7>.高可用KeepAlived实现虚IP在多服务器节点漂移:
8>.性能优化:
(1).CPU利用效率:
将worker子进程与每个CPU绑定:worker_cpu_affinity 01 10 01 10;
提高worker子进程的优先级:worker_priority -20;(-20~19)
延迟处理新连接:listen 80 deferred;
(2).TCP建立连接内核参数优化:
net.ipv4.tcp_syn_retries = 6
net.ipv4.tcp_synack_retries = 5(5大约180s,1大约36s)
net.ipv4.tcp_syncookies =1
net.core.netdev_max_backlog = 65535
net.ipv4.tcp_max_syn_backlog = 256
net.core.somaxconn = 65535
系统下查看:sysctl -a |grep syn_retries或sysctl -p修改:vim /etc/sysctl.conf
listen 443 ssl deferred backlog=65535;
(3).启用TCP的Fast Open功能:
net.ipv4.tcp_fastopen = 3(0禁止1上游启用2nginx服务启用3都启用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值