Nginx基础笔记

12次阅读

共计 3731 个字符,预计需要花费 10 分钟才能阅读完成。

压力测试工具:ab
ab -n 请求数 -c 并发数 请求 url

Nginx:
Nginx (engine x) 是一个高性能的 HTTP 和反向代理服务,也是一个 IMAP/POP3/SMTP 服务。
特点:

IO 多路复用 epoll
轻量级
CPU 亲和 (affinity):把每个 worker 进程固定在一个 cpu 上执行,减少切换 cpu 的 cache miss,获得更好的性能
sendfile: 静态文件不经过用户空间,直接通过内核进行传输

版本分类
Mainline version 开发版 (有更新的功能,但不一定稳定)Stable version 稳定版 (经过测试,有更好的稳定性)Legacy version 历史版本
命令

nginx -V 查看 nginx 版本及编译的模块信息
nginx -t -c .conf 文件路径 检查配置文件语法是否正确
nginx -s reload -c .conf 文件路径 重启配置文件

配置语法 nginx.conf
#运行用户
user nobody;
#启动进程, 通常设置成和 cpu 的数量相等
worker_processes 1;

# 全局错误日志及 PID 文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

# 工作模式及连接数上限
events {
#epoll 是多路复用 IO(I/O Multiplexing) 中的一种方式,
#仅用于 linux2.6 以上内核, 可以大大提高 nginx 的性能
use epoll;

#单个后台 worker process 进程的最大并发链接数
worker_connections 1024;

# 并发总数是 worker_processes 和 worker_connections 的乘积
# 即 max_clients = worker_processes * worker_connections
# 在设置了反向代理的情况下,max_clients = worker_processes * worker_connections / 4 为什么
# 为什么上面反向代理要除以 4,应该说是一个经验值
# 根据以上条件,正常情况下的 Nginx Server 可以应付的最大连接数为:4 * 8000 = 32000
# worker_connections 值的设置跟物理内存大小有关
# 因为并发受 IO 约束,max_clients 的值须小于系统可以打开的最大文件数
# 而系统可以打开的最大文件数和内存大小成正比,一般 1GB 内存的机器上可以打开的文件数大约是 10 万左右
# 我们来看看 360M 内存的 VPS 可以打开的文件句柄数是多少:
# $ cat /proc/sys/fs/file-max
# 输出 34336
# 32000 < 34336,即并发连接总数小于系统可以打开的文件句柄总数,这样就在操作系统可以承受的范围之内
# 所以,worker_connections 的值需根据 worker_processes 进程数目和系统可以打开的最大文件总数进行适当地进行设置
# 使得并发总数小于操作系统可以打开的最大文件数目
# 其实质也就是根据主机的物理 CPU 和内存进行配置
# 当然,理论上的并发总数可能会和实际有所偏差,因为主机还有其他的工作进程需要消耗系统资源。
# ulimit -SHn 65535

}

http {
#设定 mime 类型, 类型由 mime.type 文件定义
include mime.types;
default_type application/octet-stream;
#设定日志格式
log_format main ‘$remote_addr – $remote_user [$time_local] “$request” ‘
‘$status $body_bytes_sent “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

access_log logs/access.log main;

#sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,
#对于普通应用,必须设为 on,
#如果用来进行下载等应用磁盘 IO 重负载应用,可设置为 off,
#以平衡磁盘与网络 I / O 处理速度,降低系统的 uptime.
sendfile on;
#tcp_nopush on;

#连接超时时间
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;

#开启 gzip 压缩
gzip on;
gzip_disable “MSIE [1-6].”;

#设定请求缓冲
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;

#设定虚拟主机配置
server {
#侦听 80 端口
listen 80;
#定义使用 www.nginx.cn 访问
server_name www.nginx.cn;

#定义服务器的默认网站根目录位置
root html;

#设定本虚拟主机的访问日志
access_log logs/nginx.access.log main;

#默认请求
location / {

#定义首页索引文件的名称
index index.html index.htm;

}

# 定义错误提示页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}

#静态文件,nginx 自己处理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {

#过期 30 天,静态文件不怎么更新,过期可以设大一点,
#如果频繁更新,则可以设置得小一点。
expires 30d;
}
#禁止访问 .htxxx 文件
location ~ /.ht {
deny all;
}

}
}

日志类型

error.log 格式:error_log log_file level; 错误日志,保存 nginx 运行中的错误信息
access.log 格式:access_log log_file log_format 定义名称 (固定输出格式); 访问日志,保存访问信息

变量
http 请求变量:
arg_PARAMETER
http_HEADER(如:http_user_agent)
sent_http_HEADER

内置变量
自定义变量

常用模块
http_sub_status_module nginx 客户端的状态 (可以配置在 server 和 location 下)
stub_status;
具体使用结果:https://blog.csdn.net/echizao…http_sub_module HTTP 内容替换(http|server|location)
sub_filter string(要替换的内容) replacement(替换的内容);
sub_filter_once off; 默认为 on 默认匹配所有内容,on 只匹配一次
sub_filter_last_modified on; 默认为 off 判断服务器文件是否发生过变更,不变更则不重新加载

http_random_index_module 目录中选择一个随机主页(配置在 location 中)
random_index on;默认为 off

limit_conn_module 连接频率限制
limit_conn_zone key zone=name:size;(http)
limit_conn zone number;(http|server|location)
limit_req_modele 请求频率限制
limit_req_zone key zone=name:size rate=rate;(http)
如:limit_req_zone $binary_remote_addr zone=req_zone:1m rate=1r/s;
limit_req zone=name [burst=number](延迟数) [nodelay];(http\server\location)

http_access_module 基于 ip 的控制访问 (通过 $remote_addr 实现,代理模式就失去了作用,x-forword-for,geo 模块,自定义变量解决)
allow address | CIDR(网段) | unix: | all;(http\server\location\limit_except)
deny address | CIDR | unix: | all;(http\server\location\limit_except)
http_auth_basic_module 基于用户的信任登录(弊端解决:1 结合 lua 实现高效验证,2 利用 nginx-auth-ldap 模块,实现 nginx 和 ldap 打通)
auth_basic string | off; 默认 off(http\server\location\limit_except);
auth_basic_user_file file;(http\server\location\limit_except);file 为一个用户名密码文件
file 生成方式:htpasswd -c 文件名 用户名 然后就会让输入两次密码 命令在 httpd-tools 中

正文完
 0