Nginx介绍
-
Nginx:engine X ,2002年,开源,商业版
-
NGINX是免费,开源,高性能的HTTP和反向代理服务器,邮件代理服务器,通用TCP/UDP代理服务器
-
解决C10K问题(10K Connections)
-
二次开发版:
Tengine, OpenResty(章亦春) -
七层调度就叫代理服务器
-
特性:
- 模块化设计,较好的扩展性
- 高可靠性
- 支持热部署:不停机更新配置文件,升级版本,更换日志文件
- 低内存消耗:10000个keep-alive连接模式下的非活动连接,仅需2.5M内存
- event-driven,aio,mmap,sendfile (epool,apache用的select功能)
-
基本功能:
- 静态资源的web服务器
- http协议反向代理服务器
- pop3/imap4协议反向代理服务器 (用的不多)
- FastCGI(LNMP),uWSGI(python)等协议 ()
- 模块化(非DSO),如zip,SSL模块
nginx的程序架构
-
web服务相关的功能:
- 虚拟主机(server)
- 支持 keep-alive 和管道连接
- 访问日志(支持基于日志缓冲提高其性能)
- url rewirte
- 路径别名
- 基于IP及用户的访问控制
- 支持速率限制及并发数限制
- 重新配置和在线升级而无须中断客户的工作进程
- Memcached 的 GET 接口
-
nginx的程序架构:
master/worker结构- 一个master进程:
负载加载和分析配置文件、管理worker进程、平滑升级 - 一个或多个worker进程
处理并响应用户请求 - 缓存相关的进程:
cache loader:载入缓存对象
cache manager:管理缓存对象
- 一个master进程:
-
nginx模块
- nginx高度模块化,但其模块早期不支持DSO机制;1.9.11版本支持动态装载和卸载
- 模块分类:
- 核心模块:core module
- 标准模块:
- HTTP 模块: ngx_http_*
HTTP Core modules 默认功能
HTTP Optional modules 需编译时指定_ - Mail 模块 ngx_mail_*_
- Stream 模块 ngx_stream_*
- HTTP 模块: ngx_http_*
- 第三方模块
-
nginx的功用:
- 静态的web资源服务器
html,图片,js,css,txt等静态资源 - 结合FastCGI/uWSGI/SCGI等协议反向代理动态资源请求
- http/https协议的反向代理
- imap4/pop3协议的反向代理
- tcp/udp协议的请求转发(反向代理)
- 静态的web资源服务器
nginx的安装
-
Fedora-EPEL:
https://mirrors.aliyun.com/epel/7/x86_64/ -
编译安装:
•yum install pcre-devel openssl-devel zlib-devel •useradd -r nginx •./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_dav_module --with-http_stub_status_module --with-threads --with-file-aio •make && make install
-
编译安装nginx选项:
--prefix=/etc/nginx 安装路径 --sbin-path=/usr/sbin/nginx 指明nginx程序文件安装路径 --conf-path=/etc/nginx/nginx.conf 主配置文件安装位置 --error-log-path=/var/log/nginx/error.log 错误日志文件安装位置 --http-log-path=/var/log/nginx/access.log 访问日志文件安装位置 --pid-path=/var/run/nginx.pid 指明pid文件安装位置 --lock-path=/var/run/nginx.lock 锁文件安装位置 --http-client-body-temp-path=/var/cache/nginx/client_temp 客户端body部分的临时文件存放路径,服务器允许客户端使用put方法提交大数据时,临时存放的磁盘路径 --http-proxy-temp-path=/var/cache/nginx/proxy_temp 作为代理服务器,服务器响应报文的临时文件存放路径 --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 作为fastcgi代理服务器,服务器响应报文的临时文件存放路径 --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 作为uwsgi代理服务器,服务器响应报文的临时文件存放路径 --http-scgi-temp-path=/var/cache/nginx/scgi_temp 作为scgi反代服务器,服务器响应报文的临时文件存放路径 --user=nginx 指明以那个身份运行worker进程,主控master进程一般由root运行 --group=nginx --with-http_ssl_module 表示把指定模块编译进来
nginx目录结构和命令
-
ls /usr/local/nginx/
html是测试页,sbin是主程序 -
ls /usr/local/nginx/sbin/
nginx 只有一个程序文件 -
ls /usr/local/nginx/html/
50x.html index.html 测试网页 -
Nginx:默认为启动nginx
-h 查看帮助选项 -V 查看版本和配置选项 -t 测试nginx语法错误 -c filename 指定配置文件(default: /etc/nginx/nginx.conf) -s signal 发送信号给master进程,signal:stop, quit, reopen, reload 示例: nginx -s stop 停止nginx nginx -s reload 加载配置文件 -g directives 在命令行中指明全局指令
nginx配置
-
配置文件的组成部分:
-
主配置文件:nginx.conf (实验中修改此文件,先做好备份
cp nginx.conf{,.bak}
) -
子配置文件 include conf.d/*.conf
-
fastcgi, uwsgi,scgi等协议相关的配置文件
-
mime.types:支持的mime类型
-
-
主配置文件的配置指令:
- directive value [value2 …];(例:worker_processes 2;)
-
注意:
(1) 指令必须以分号结尾
(2) 支持使用配置变量
内建变量:由Nginx模块引入,可直接引用
自定义变量:由用户使用set命令定义
set variable_name value;
引用变量:$variable_name
nginx配置文件
-
主配置文件结构:四部
main block:主配置段,即全局配置段,对http,mail都有效
event {
…
} 事件驱动相关的配置http {
…
} http/https 协议相关配置段mail {
…
} mail 协议相关配置段stream {
…
} stream 服务器相关配置段
nginx全局配置
-
Main 全局配置段常见的配置指令分类
-
正常运行必备的配置
-
优化性能相关的配置
-
用于调试及定位问题相关的配置
-
事件驱动相关的配置
-
正常运行必备的配置:
- 1、user (ps aux查看)
Syntax: user user [group];
Default: user nobody nobody;(getent passwd
)
Context: main 指定worker进程的运行身份,如组不指定,默认和用户名同名 - 2、pid /PATH/TO/PID_FILE (
ls /run/nginx.pid;pstree -p
查看)
指定存储nginx主进程PID的文件路径 - 3、include file | mask(
ls /usr/share/nginx/modules
查看加载模块列表)
指明包含进来的其它配置文件片断 - 4、load_module file (
cat /usr/share/nginx/modules/mod-mail.conf
查看)
模块加载配置文件:/usr/share/nginx/modules/*.conf
指明要装载的动态模块路径: /usr/lib64/nginx/modules
性能优化相关的配置:
-
1、worker_processes number | auto
worker进程的数量;通常应该为当前主机的cpu的物理核心数vim /etc/nginx/nginx.conf worker_processes 2;
-
2、worker_cpu_affinity cpumask …
提高缓存命中率CPU MASK: 00000001:0号CPU 00000010:1号CPU 10000000:8号CPU worker_cpu_affinity 0001 0010 0100 1000; worker_cpu_affinity 0101 1010; watch -n1 'ps axo pid,cmd,psr|grep nginx' 查看跑在哪颗CPU ab -c 100 -n2000 http://192.168.32.88 模拟并发 vim /etc/nginx/nginx.conf worker_cpu_affinity 0001 0010 ; 共两颗CPU,一个进程一个CPU
-
3、worker_priority number
指定worker进程的nice值,设定worker进程优先级:[-20,19]watch -n1 'ps axo pid,cmd,nice|grep nginx' 查看优先级 vim /etc/nginx/nginx.conf worker_priority 10;
-
4、worker_rlimit_nofile number
worker进程所能够打开的文件数量上限,如65535vim /etc/nginx/nginx.conf worker_rlimit_nofile 51200 worker最终可以打开51200用户连接
事件驱动相关的配置:
-
1、worker_connections 10240 (不要用centos6测试,有ulimit值限制,ulimit -a可看,ulimit -n可改)
每个worker进程所能够打开的最大并发连接数数量,如10240
总最大并发数:worker_processes * worker_connections -
2、use method
指明并发连接请求的处理方法 ,默认自动选择最优方法use epoll; -
3、accept_mutex on | off 互斥
处理新的连接请求的方法;on指由各个worker轮流处理新请求,Off指每个新请求的到达都会通知(唤醒)所有的worker进程,但只有一个进程可获得连接,造成“惊群”,影响性能vim /etc/nginx/nginx.conf events { worker_connections 1024; use epoll; accept_mutex on }
调试和定位问题:(测试环境使用)
1、daemon on|off (主语句块)
是否以守护进程方式运行nignx,默认是守护进程方式
2、master_process on|off
是否以master/worker模型运行nginx;默认为on
off 将不启动worker
3、error_log file [level]
错误日志文件及其级别;出于调试需要,可设定为debug;但debug仅在编译时使用了“–with-debug”选项时才有效
方式:file /path/logfile;
stderr:发送到标准错误
syslog:server-address[,parameter=values]:发送到syslog memory:size 内存
level:debug|info|notice|warn|error|crit|alter|emerg
http协议相关的配置结构:/etc/nginx/conf.d/
http {
...
... 各server的公共配置
server { 每个server用于定义一个虚拟主机
...
}
server {
...
server_name 虚拟主机名
root 主目录
alias 路径别名
location [OPERATOR] URL { 指定URL的特性
...
if CONDITION {
...
}
}
}
}
ngx_http_core_module
1、配置一个虚拟主机
server {
listen 80 default_server;
server_name www.a.com;
root /data/website;
}
2、listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
listen address[:port][default_server] [ssl][http2 | spdy] [backlog=number][rcvbuf=size] [sndbuf=size]
- default_server 设定为默认虚拟主机
- ssl 限制仅能够通过ssl连接提供服务
- backlog=number 超过并发连接数后,新请求进入后援队列的长度
- rcvbuf=size 接收缓冲区大小
- sndbuf=size 发送缓冲区大小
注意:
(1) 基于port;
listen PORT; 指令监听在不同的端口
(2) 基于ip的虚拟主机
listen IP:PORT; IP 地址不同
(3) 基于hostname
server_name fqdn; 指令指向不同的主机名
3、server_name name …;
- 虚拟主机的主机名称后可跟多个由空白字符分隔的字符串
- 支持*通配任意长度的任意字符
server_name *.dushan.com www.dushan.*
- 支持~起始的字符做正则表达式模式匹配,性能原因慎用
server_name ~^www\d+\.magedu\.com$
说明: \d 表示 [0-9]
- 匹配优先级机制从高到低:
(1) 首先是字符串精确匹配 如:www.dushan.com
(2) 左侧*通配符 如:*.dushan.com
(3) 右侧*通配符 如:www.dushan.*
(4) 正则表达式 如: ~^.*\.dushan\.com$ (所有开头的.dushan.com)
(5) default_server
4、tcp_nodelay on | off;
在keepalived模式下的连接是否启用TCP_NODELAY选项
当为off时,延迟发送,合并多个请求后再发送 关心服务器
默认On时,不延迟发送 关心客户端
可用于:http, server, location
5、sendfile on | off; (提升性能最好on)
是否启用sendfile功能,在内核中封装报文直接发送
默认Off
6、server_tokens on | off | build | string
是否在响应报文的Server首部显示nginx版本
vim /etc/nginx/nginx.conf
server_tokens off
curl -I www.a.com后看不到版本号
7、root
- 设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location
server {
...
root /data/www/vhost1;
}
示例:
http://www.dushan.com/images/logo.jpg--> /data/www/vhosts/images/logo.jpg
ln -s /data/www/vhosts/images bbs (创建软连接,可能就访问的不是原始目录)
http://www.dushan.com/bbs/logo.jpg--> /data/www/vhosts/images/logo.jpg
8、location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置
示例:
server {...
server_name www.dushan.com;
location /images/ {
root /data/imgs/;
}
}
示例:
http://www.dushan.com/images/logo.jpg --> /data/imgs/images/logo.jpg
- 对URI做精确匹配;
location / {
root /data/website
}
^~: 对URI的最左边部分做匹配检查,不区分字符大小写
~: 对URI做正则表达式模式匹配,区分字符大小写
~*: 对URI做正则表达式模式匹配,不区分字符大小写
不带符号:匹配起始于此uri的所有的uri
匹配优先级从高到低:=, ^~, ~/~*, 不带符号
9、alias path;(不带子目录了,直接访问别名时直接是指定路径)
路径别名,文档映射的另一种机制;仅能用于location上下文
示例:
http://www.magedu.com/bbs/index.php
location /bbs {
alias /web/forum/;
}
访问/bbs目录时,将访问到/web/forum/index.html
注意:/bbs后建议不要加/
location /bbs/ {
root /web/forum/;
}
访问/bbs目录时,将访问到/web/forum/bbs/index.html
注意:location中使用root指令和alias指令的意义不同
(a) root,给定的路径对应于location中的/uri/左侧的/
(b) alias,给定的路径对应于location中的/uri/右侧的/
10、index file …;
指定默认网页文件,注意:ngx_http_index_module模块
11、error_page code … [=[response]] uri;
模块:ngx_http_core_module
定义错误页,以指定的响应状态码进行响应
可用位置:http, server, location, if in location
error_page 404 /404.html
error_page 404 =200 /404.html
server {
server_tokens off;
listen 80;
server_name *.dushan.com www.dudou.com;
root /data/website;
error_page 404 /404_error.html;
}
12、try_files file … uri;
try_files file … =code;
按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有的文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向。最后一个参数是回退URI且必须存在,否则会出现内部500错误
location /images/ {
try_files $uri /images/default.gif;
} 说明:/images/default.gif 是uri
location / {
try_files $uri $uri/index.html $uri.html =404;
}
定义客户端请求的相关配置
13、keepalive_timeout timeout [header_timeout];
设定保持连接超时时长,0表示禁止长连接,默认为75s
测试:
telnet www.a.com 80
GET / HTTP1.1
HOST:www.a.com
14、keepalive_requests number;
在一次长连接上所允许请求的资源的最大数量
默认为100
15、keepalive_disable none | browser …
对哪种浏览器禁用长连接
16、send_timeout time;
向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非整个响应过程的传输时长
17、client_body_buffer_size 16k;
用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置
18、client_body_temp_path path [level1 [level2 [level3]]];
设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量
目录名为16进制的数字;
client_body_temp_path /var/tmp/client_body 1 2 2
1 1级目录占1位16进制,即2^4=16个目录 0-f
2 2级目录占2位16进制,即2^8=256个目录 00-ff
2 3级目录占2位16进制,即2^8=256个目录 00-ff
一共16*256*256=1048576 可搜索加速,缓存的常见机制
[root@dushan website]#md5sum index.html
8c85e4eb9eab7c6ff9473df66e3d7444 index.html
取值: 2 2 1
/var/tmp/client_body/4/44/d7/ 页面将来会放到这个目录里
19、limit_rate 10240;
限制响应给客户端的传输速率,单位是bytes/second
默认值0表示无限制
server {
...
limit_rate 10240;
}
测试:wget可看速度,是否为10k左右
wget http://www.a.com/access.log
20、limit_except method … { … },仅用于location
限制客户端使用除了指定的请求方法之外的其它方法
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH
除了GET方法以外可以操作,将来可以限制PUT,上传功能等。
limit_except GET {
allow 192.168.32.7/24;
deny all;
}
文件操作优化的配置
21、aio on | off | threads[=pool];
是否启用aio功能(异步io)
22、directio size | off;
当文件大于等于给定大小时,例如directio 4m,同步(直接)写磁盘,而非写缓存
写入buffer快,但是未写磁盘,断电可能会丢失,所以达到多大时,立即写磁盘
23、open_file_cache off;
open_file_cache max=N [inactive=time];
nginx可以缓存以下三种信息:
(1) 文件元数据:文件的描述符、文件大小和最近一次的修改时间
(2) 打开的目录结构
(3) 没有找到的或者没有权限访问的文件的相关信息
max=N:可缓存的缓存项上限;达到上限后会使用LRU算法实现管理
inactive=time:缓存项的非活动时长,在此处指定的时长内未被命中的或命中的次数少于open_file_cache_min_uses指令所指定的次数的缓存项即为非活动项,将被删除
24、open_file_cache_errors on | off;
是否缓存查找时发生错误的文件一类的信息
默认值为off
25、open_file_cache_min_uses 3; 至少访问3次才算活动项
open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项
默认值为1
26、open_file_cache_valid 60; 60秒淘汰一次
缓存项有效性的检查频率
默认值为60s
ngx_http_access_module
可实现基于ip的访问控制功能
1、allow address | CIDR | unix: | all;
2、deny address | CIDR | unix: | all;
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;
}
ngx_http_auth_basic_module
实现基于用户的访问控制,使用basic机制进行用户认证
1、auth_basic string | off;2、auth_basic_user_file file;
location /admin/ {
auth_basic "Admin Area";
auth_basic_user_file /etc/nginx/.ngxpasswd;
}
用户口令文件:
1、明文文本:格式name:password:comment
2、加密文本:由htpasswd命令实现
httpd-tools所提供
yum install httpd-tools
htpasswd -c /etc/nginx/.nginxpasswd dushan
注意建立第二次账号时不要加-c,只有第一次创建文件时使用-c
htpasswd /etc/nginx/.nginxpasswd liuying
ngx_http_stub_status_module
用于输出nginx的基本状态信息
输出信息示例:
Active connections: 291
server accepts handled requests
16630948 16630948 31070465
上面三个数字分别对应accepts,handled(),requests三个值
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已经处理完成的客户端请求的总数
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数
location /status{
stub_status;
allow 192.168.32.6;
deny all;
}
ngx_http_log_module
指定日志格式记录请求
1、log_format name string …;
string可以使用nginx核心模块及其它模块内嵌的变量
2、access_log path [format [buffer=size] [gzip[=level]][flush=time] [if=condition]];
access_log off;
访问日志文件路径,格式及相关的缓冲的配置
buffer=size
flush=time
vim /etc/nginx/nginx.conf
hppd {
log_format compression '$remote_addr - $remote_user [$time_local] "$request"'
'$status $bytes_sent "$http_referer"'
"$http_user_agent" "$gzip_ratio"';
log_format testlog '$remote_addr - $remote_user [$time_local] "$request"';
}
vim/etc/nginx/conf.d/vhosts.conf 在其他vhost中引用compression设定,分别存放
access_log /var/logs/nginx-access.log compression buffer=32k;
3、open_log_file_cache max=N [inactive=time][min_uses=N] [valid=time];
open_log_file_cache off;
缓存各日志文件相关的元数据信息
max:缓存的最大文件描述符数量
min_uses:在inactive指定的时长内访问大于等于此值方可被当作活动项
inactive:非活动时长
valid:验证缓存中各缓存项是否为活动项的时间间隔
ngx_http_gzip_module
用gzip方法压缩响应数据,节约带宽
1、gzip on | off;
启用或禁用gzip压缩
2、gzip_comp_level level;
压缩比由低到高:1 到 9
默认:1
3、gzip_disable regex …;
匹配到客户端浏览器不执行压缩
4、gzip_min_length length;
启用压缩功能的响应报文大小阈值
5、gzip_http_version 1.0 | 1.1;
设定启用压缩功能时,协议的最小版本
默认:1.1
6、gzip_buffers number size;
支持实现压缩功能时缓冲区数量及每个缓存区的大小
默认:32 4k 或 16 8k
7、gzip_types mime-type …;
指明仅对哪些类型的资源执行压缩操作;即压缩过滤器
默认包含有text/html,不用显示指定,否则出错
8、gzip_vary on | off;
如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”
9、gzip_proxied off | expired | no-cache | no-store | private | no_last_modified | no_etag | auth | any …;
nginx充当代理服务器时,对于后端服务器的响应报文,在何种条件下启用压缩功能
off:不启用压缩
expired,no-cache, no-store,private:对后端服务器的响应报文首部Cache-Control值任何一个,启用压缩功能
示例:
vim /etc/nginx/conf.d/nginx.conf
server{
gzip on;
gzip_comp_level 9;
gzip_min_length 64;
gzip_proxied any;
gzip_vary on
gzip_types text/xml text/css application/javascript text/plain;
...
}
测试:
curl -I --compressd www.a.com/access.log |less 注意curl需带compress才能压缩
ngx_http_ssl_module
1、ssl on | off;
为指定虚拟机启用HTTPS protocol, 建议用listen指令代替
2、ssl_certificate file;
当前虚拟主机使用PEM格式的证书文件
3、ssl_certificate_key file;
当前虚拟主机上与其证书匹配的私钥文件
4、ssl_protocols [SSLv2][SSLv3] [TLSv1][TLSv1.1] [TLSv1.2];
支持ssl协议版本,默认为后三个
5、ssl_session_cache off | none | [builtin[:size]][shared:name:size];
none: 通知客户端支持ssl session cache,但实际不支持
builtin[:size]:使用OpenSSL内建缓存,为每worker进程私有
[shared:name:size]:在各worker之间使用一个共享的缓存
6、ssl_session_timeout time;
客户端连接可以复用ssl session cache中缓存的ssl参数的有效时长,默认5m
示例:
[root@dushan ~]#vim /etc/nginx/conf.d/vhosts.conf
server {
listen 443 ssl;
server_name www.a.com;
root /data/website;
ssl_certificate /etc/nginx/conf.d/a.com.crt; *这个即可
ssl_certificate_key /etc/nginx/conf.d/a.com.key; *这个即可
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
自己建立证书测试
[root@dushan ~]#cd /etc/pki/tls/certs/
[root@dushan certs]#ls
ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert
[root@dushan certs]#vim Makefile
...
%.key:
umask 77 ; \
#/usr/bin/openssl genrsa -aes128 $(KEYLEN) > $@
/usr/bin/openssl genrsa $(KEYLEN) > $@
...
[root@dushan certs]#make a.com.crt
umask 77 ; \
#/usr/bin/openssl genrsa -aes128 2048 > a.com.key
/usr/bin/openssl genrsa 2048 > a.com.key
Generating RSA private key, 2048 bit long modulus
..........+++
..........................+++
e is 65537 (0x10001)
umask 77 ; \
/usr/bin/openssl req -utf8 -new -key a.com.key -x509 -days 365 -out a.com.crt
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:beijing
Organization Name (eg, company) [Default Company Ltd]:dushan
Organizational Unit Name (eg, section) []:ops
Common Name (eg, your name or your server's hostname) []:www.a.com
Email Address []:
[root@dushan certs]#ls
a.com.crt a.com.key ca-bundle.crt ca-bundle.trust.crt make-dummy-cert Makefile renew-dummy-cert
[root@dushan certs]#mv a.com* /etc/nginx/conf.d/
ngx_http_rewrite_module
将用户请求的URI基于PCRE regex所描述的模式进行检查,而后完成重定向替换
示例:
http://www.dushan.com/hn --> http://www.dushan.com/henan
http://www.dushan.com --> https://www.dushan.com/
1、rewrite regex replacement [flag]
将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为replacement指定的新的URI
注意:如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查
隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制
如果replacement是以http://或https://开头,则替换结果会直接以重向返回给客户端, 即永久重定向301
[flag]:
-
last:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后对新的URI启动新一轮重写检查;提前重启新一轮循环,不建议在location中使用
-
break:重写完成后停止对当前URI在当前location中后续的其它重写操作,而后直接跳转至重写规则配置块之后的其它配置;结束循环,建议在location中使用
-
redirect:临时重定向,重写完成后以临时重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求;使用相对路径,或者http://或https://开头,状态码:302
-
permanent:重写完成后以永久重定向方式直接返回重写后生成的新URI给客户端,由客户端重新发起请求,状态码:301
1. location /bbs{ rewrite ^/bbs(.*)$ /forum$1 last; } 注意:()分组后,nginx用$1引用前面的分组 显示代码200,浏览器地址栏不变,只是内部跳转,原目录不存在也可以。 2. location /bbs{ rewrite ^/bbs(.*)$ /forum$1 redirect; } 显示代码302,浏览器地址变为新地址,客户端对新地址发送一遍GET请求。 3. location /{ rewrite / http://www.b.com; permanet } 不添加permanet,更改域名自动是302代码,网站永久变更记得带permanet 4. location ~\.html$ { rewrite (.*)\.html$ $1.php break; } location ~\.php$ { rewrite (.*)\.php$ $1.html break; } 访问www.a.com/index.html 自动跳转到www.a.com/index.php 访问www.a.com/index.php 自动跳转到www.a.com/index.html
2、return
return code [text];
return code URL;
return URL;
停止处理,并返回给客户端指定的响应码,对 301, 302, 303, 307, 308跳转到URL
location /admin {
#return 403 "forbidden";
return http://www.b.com
}
3、rewrite_log on | off;
是否开启重写日志, 发送至error_log(notice level)测试失败
4、set v a r i a b l e v a l u e ; 用 户 自 定 义 变 量 注 意 : 变 量 定 义 和 调 用 都 要 以 variable value; 用户自定义变量 注意:变量定义和调用都要以 variablevalue;用户自定义变量注意:变量定义和调用都要以开头
5、if (condition) { … }
条件满足时,执行配置块中的配置指令;server, location
condition:
比较操作符:
= 相同 != 不同
~:模式匹配,区分字符大小写
~*:模式匹配,不区分字符大小写
!~:模式不匹配,区分字符大小写
!~*:模式不匹配,不区分字符大小写
文件及目录存在性判断:
-e, !-e 存在(包括文件,目录,软链接)
-f, !-f 文件 -d, !-d 目录 -x, !-x 执行
示例:
location / {
if ( $scheme = http){
rewrite / https://www.a.com redirect;
}
}
ngx_http_referer_module
用来阻止Referer首部无有效值的请求访问,可防止盗链
1、valid_referers none|blocked|server_names|string …;
定义referer首部的合法可用值,不能匹配的将是非法值
none:请求报文首部没有referer首部
blocked:请求报文有referer首部,但无有效值
server_names:referer首部中包含本主机名
arbitrary_string:任意字符串,但可使用*作通配符
regular expression:被指定的正则表达式模式匹配到的字符串,要使用~开头,
例如: ~.*\.magedu\.com
示例:只允许dushan 和baidu.google访问,其他都拒绝并跳转到www.dushan.com
valid_referers none block server_names *.dushan.com dushan.* ~\.dushan\. ~\.baidu\. ~\google\. ;
if ($invalid_referer) {
return 403 http://www.dushan.com;
}
ngx_http_proxy_module
转发请求至另一台主机
1、proxy_pass URL;
Context:location, if in location, limit_except
注意:proxy_pass后面路径不带uri时,会将location的uri传递(附加)给后端主机
server {
listen 80 default_server;
server_name www.a.com;
root /data/website;
location /bbs {
proxy_pass http://192.168.32.7/; 注意斜线
}
}
上面示例:访问http://www.a.com/bbs得到http://192.168.32.7页面
如设置内不带斜线,表示访问http://192.168.32.7/bbs的页面
server {
listen 80 default_server;
server_name www.a.com;
root /data/website;
location ~\.txt$ {
proxy_pass http://192.168.32.7; 注意斜线
}
}
上面示例:访问www.a.com/*.txt文件内容则调度到192.168.32.7;利用正则表达式,就不能带斜线了
server{
listen 80;
server_name www.a.com;
location / {
proxy_pass http://192.168.32.7/;
}
}
server{
listen 81;
server_name app.a.com;
location / {
proxy_pass http://192.168.32.7:81/;
}
}
server{
listen 82;
server_name mobi.a.com
location / {
proxy_pass http://192.168.32.7:82/;
}
}
基于端口多转发
2、proxy_set_header field value;
设定发往后端主机的请求报文的请求首部的值
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
请求报文的标准格式如下:
X-Forwarded-For: client1, proxy1, proxy
192.168.32.7-apache:
vim /etc/httpd/conf/httpd.conf
# LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{clientip}\" " combined
httpd -t
systemctl restart httpd
tail -f /var/log/httpd/access_log
192.168.32.88:
server{
listen 80 default_server;
listen 443 ssl;
server_name www.a.com;
root /data/website;
access_log /var/log/nginx/a.com_access.log main;
error_log /var/log/nginx/a/a.com.error.log;
ssl_certificate /etc/nginx/conf.d/a.com.crt;
ssl_certificate_key /etc/nginx/conf.d/a.com.key;
location /{
proxy_pass http://192.168.32.7/;
proxy_set_header clientip $remote_addr;
}
}
nginx -s reload
若后台RS不同网站端口一样,添加头部即可实现访问
server{
listen 80;
server_name www.a.com;
location / {
proxy_pass http://192.168.32.7/;
proxy_set_header clientip $remote_addr;
proxy_set_header host $host;
}
}
server{
listen 80;
server_name app.a.com
location / {
proxy_pass http://192.168.32.7:81/;
proxy_set_header clientip $remote_addr;
proxy_set_header host $host;
}
}
server{
listen 80;
server_name mobi.a.com
location / {
proxy_pass http://192.168.32.7:80/;
proxy_set_header clientip $remote_addr;
proxy_set_header host $host;
}
}
或设置http主配置文件里
3、proxy_cache_path;
定义可用于proxy功能的缓存;Context:http
4、proxy_cache zone | off; 默认off
指明调用的缓存,或关闭缓存机制;Context:http, server, location
5、proxy_cache_key string;
缓存中用于“键”的内容
默认值:proxy_cache_key
s
c
h
e
m
e
scheme
schemeproxy_host$request_uri;
6、proxy_cache_valid [code …] time;
定义对特定响应码的响应内容的缓存时长
定义在http{…}中
示例:
proxy_cache_valid 200 302 10m; (200和302代码的缓存时长10分钟)
proxy_cache_valid 404 1m;(404的缓存时长1分钟)
proxy_cache_path path [levels=levels][use_temp_path=on|off] keys_zone=name:size [inactive=time][max_size=size] [manager_files=number][manager_sleep=time] [manager_threshold=time][loader_files=number] [loader_sleep=time][loader_threshold=time] [purger=on|off][purger_files=number] [purger_sleep=time][purger_threshold=time];
示例:在http配置定义缓存信息
proxy_cache_path /var/cache/nginx/proxycache 缓存路径 目录必须提前建立,子目录可以没有
levels=1:1:1 keys_zone=proxycache:20m 指定目录结构,1级目录放一个16进制,16*16*16;起个名字proxycache:在内存中占用20M空间
inactive=120s max_size=1g; 缓存的网页文件1G放在磁盘,有效期120秒
启动,在子配置文件中启动上面定义的缓存信息
说明:proxycache:20m 指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
max_size=1g 指磁盘存入文件内容的缓存空间最大值
调用缓存功能,需要定义在相应的配置段,如server{...};
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m; 除了定义的都选用1分钟
7、proxy_cache_use_stale;
proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | off …
在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端
8、proxy_cache_methods GET | HEAD | POST …;
对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存(POST客户上传,一般不开启)
9、proxy_hide_header field;
默认nginx在响应报文中不传递后端服务器的首部字段Date, Server, X-Pad, X-Accel-等,用于隐藏后端服务器特定的响应首部
proxy_hide_header Server; 隐藏Server,curl -I 192.168.32.7后看不到web版本
10、proxy_connect_timeout time;
定义与后端服务器建立连接的超时时长,如超时会出现502错误,默认为60s,一般不建议超出75s
11、proxy_send_timeout time;
将请求发送给后端服务器的超时时长;默认为60s
12、proxy_read_timeout time;
等待后端服务器发送响应报文的超时时长,默认为60s
ngx_http_headers_module
向由代理服务器响应给客户端的响应报文添加自定义首部,或修改指定首部的值
1、add_header name value [always];
添加自定义首部
add_header X-Via $server_addr; 不建议加此项,服务器地址
add_header X-Cache $upstream_cache_status; 缓存状态
add_header X-Accel $server_name;服务器名
curl -I 192.168.32.7 后可查看自行添加的选项
2、add_trailer name value [always];
添加自定义响应信息的尾部
RS2:
yum install php
vim /var/www/html
cat > test.php
<?php phpinfo();?>
RS1:
via /var/www/html/index.html
RS1 server
nginx:
server {
listen 80 default_server;
server_name www.a.com;
root /data/website
access_log /var/log/nginx/a.com_access.log main;
error_log /var/log/nginx/a.com_error.log;
proxy_set_header clientip $remote_addr;
proxy_cache proxycache;
proxy_cach_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_hide_header ETag;
proxy_hide_header Server;
proxy_cache_valid any 1m;
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
add_header Server "IIS";
location ~ \.php$ {
proxy_pass http://192.168.32.17/;
}
location / {
proxy_pass http://192.168.32.7/;
}
tree /var/cache/nginx/proxycache/ 查看缓存
ngx_http_fastcgi_module模块,动静分离
转发请求到FastCGI服务器,不支持php模块方式
1、fastcgi_pass address;
address为后端的fastcgi server的地址
可用位置:location, if in location
yum install php-fpm php-mysql 或把mysql装到另外一台机器
vim /etc/php-fpm.d/www.conf
listen = 9000 默认只监听本机9000端口,修改后监听在本机的所有IP上
;listen.allowed_clients =127.0.0.1 默认只允许本机访问,注释掉,即允许任何ip访问
pm.status_path = /status 状态页面
ping.path = /ping ping
ping.response = pong 回应pong
mkdir /data/php
cp test.php /data/php
systemctl start php-fpm
ss -ntl 9000
nginx:
示例1:
1)在后端服务器先配置fpm server和mariadb-server
2)在前端nginx服务上做以下配置:
location ~* \.php$ {
fastcgi_pass 192.168.32.17:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/php$fastcgi_script_name; php页面默认存放路径
include fastcgi_params; 其他配置文件直接调用fastcgi_params,nginx系统已经写好
}
示例2:通过/pm_status和/ping来获取fpm server状态信息
location ~* ^/(pm_status|ping)$ {
include fastcgi_params;
fastcgi_pass 后端fpm服务器IP:9000;
fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
}
2、fastcgi_index name;
fastcgi默认的主页资源
示例:fastcgi_index index.php;
3、fastcgi_param parameter value [if_not_empty];
设置传递给 FastCGI服务器的参数值,可以是文本,变量或组合
4、定义fastcgi的缓存;
fastcgi_cache_path path [levels=levels][use_temp_path=on|off] keys_zone=name:size [inactive=time][max_size=size] [manager_files=number][manager_sleep=time] [manager_threshold=time][loader_files=number] [loader_sleep=time][loader_threshold=time] [purger=on|off][purger_files=number] [purger_sleep=time][purger_threshold=time];
path 缓存位置为磁盘上的文件系统
max_size=size 磁盘path路径中用于缓存数据的缓存空间上限
levels=levels: 缓存目录的层级数量,以及每一级的目录数量
levels=ONE:TWO:THREE
示例:leves=1:2:2
keys_zone=name:size
k/v映射的内存空间的名称及大小
inactive=time 非活动时长
5、fastcgi_cache zone | off;
调用指定的缓存空间来缓存数据
可用位置:http, server, location
6、fastcgi_cache_key string;
定义用作缓存项的key的字符串
示例:fastcgi_cache_key $request_rui;
7、fastcgi_cache_methods GET | HEAD | POST …;
为哪些请求方法使用缓存
8、fastcgi_cache_min_uses number;
缓存空间中的缓存项在inactive定义的非活动时间内至少要被访问到此处所指定的次数方可被认作活动项
9、fastcgi_keep_conn on | off;
收到后端服务器响应后,fastcgi服务器是否关闭连接,建议启用长连接
10、fastcgi_cache_valid [code …] time;
不同的响应码各自的缓存时长
示例:
http {
fastcgi_cache_path /var/cache/nginx/fcgi_cache levels=1:2:1 keys_zone=fcgicache:20m inactive=120s;
...
server {
location ~* \.php$ {
...
fastcgi_cache fcgicache;
fastcgi_cache_key $request_uri;
fastcgi_cache_valid 200 302 10m;
fastcgi_cache_valid 301 1h;
fastcgi_cache_valid any 1m;
...
}
}
ngx_http_upstream_module
ngx_http_upstream_module模块
用于将多个服务器定义成服务器组,而由proxy_pass, fastcgi_pass等指令进行引用
1、upstream name { … }
定义后端服务器组,会引入一个新的上下文
默认调度算法是wrr Context: http
vim /etc/nginx/nginx.conf
http {
upstream httpserver{
server 192.168.32.17;
server 192.168.32.27;
}
vim /etc/nginx/conf.d/vhost.conf
location / {
proxy_pass http://httpserver;
}
2、server address [parameters];
在upstream上下文中server成员,以及相关的参数;Context:upstream
unix:/PATH/TO/SOME_SOCK_FILE
weight=number 权重,默认为1
max_conns 连接后端报务器最大并发活动连接数,1.11.5后支持
max_fails=number 失败尝试最大次数;超出此处指定的次数时,server将被标记为不可用,默认为1
fail_timeout=time 后端服务器标记为不可用状态的连接超时时长,默认10s
backup 将服务器标记为“备用”,即所有服务器均不可用时才启用 (找一个独立的端口号)
down 标记为“不可用”,实现灰度发布
示例:
vim /etc/nginx/conf.c/vhost.conf
server {
listen 8080
root /data/website;
}
server {
listen 80 default_server;
root /data/weibsite;
server_name www.a.com;
}
vim /etc/nginx/nginx.conf
http {
upstream httpserver {
server 192.168.32.17 weight=3;
server 192.168.32.27;
server 127.0.0.1:8000 backup;
}
}
3、ip_hash 源地址hash调度方法 (ip_hash和backup冲突)
vim /etc/nginx/nginx.conf
http {
upstream httpserver {
ip_hash;
server 192.168.32.17 weight=3;
server 192.168.32.27;
#server 127.0.0.1:8000 backup;
}
}
4、least_conn 最少连接调度算法,当server拥有不同的权重时其为wlc,当所有后端主机连接数相同时,则使用wrr,适用于长连接
vim /etc/nginx/nginx.conf
http {
upstream httpserver {
least_conn;
server 192.168.32.17 weight=3;
server 192.168.32.27;
#server 127.0.0.1:8000 backup;
}
}
5、hash key [consistent] 基于指定的key的hash表来实现对请求的调度,此处的key可以直接文本、变量或二者组合
作用:将请求分类,同一类请求将发往同一个upstream server,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务器(如varnish)时使用
vim /etc/nginx/nginx.conf
http {
upstream httpserver {
hash $remote_addr;
server 192.168.32.17 weight=3;
server 192.168.32.27;
#server 127.0.0.1:8000 backup;
}
}
hash $request_uri consistent;目标地址hash
hash $remote_addr; 源地址hash
6、keepalive 连接数N;
为每个worker进程保留的空闲的长连接数量,可节约nginx端口,并减少连接管理的消耗
7、health_check [parameters];(注意:仅对nginx plus有效,收费版)
健康状态检测机制;只能用于location上下文
常用参数:
interval=time检测的频率,默认为5秒
fails=number:判定服务器不可用的失败检测次数;默认为1次
passes=number:判定服务器可用的失败检测次数;默认为1次
uri=uri:做健康状态检测测试的目标uri;默认为/
match=NAME:健康状态检测的结果评估调用此处指定的match配置块
8 match name { … }(注意:仅对nginx plus有效,收费版)
对backend server做健康状态检测时,定义其结果判断机制;只能用于http上下文
常用的参数:
status code[ code …]: 期望的响应状态码
header HEADER[operator value]:期望存在响应首部,也可对期望的响应首部的值基于比较操作符和值进行比较
body:期望响应报文的主体部分应该有的内容
ngx_stream_core_module
- ginx的其它的二次发行版:
Tengine:由淘宝网发起的Web服务器项目。它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性。Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。它的最终目标是打造一个高效、稳定、安全、易用的Web平台。从2011年12月开始,Tengine成为一个开源项目,官网 http://tengine.taobao.org/
OpenResty:基于 Nginx 与 Lua 语言的高性能 Web 平台 - ngx_stream_core_module模块
模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器
1、stream { … }
定义stream相关的服务;Context:main
vim /etc/nginx/nginx.conf 注意:stream语句块和http平级
stream {
upstream mysqlsrvs {
server 192.168.32.17:3306;
server 192.168.22.27:3306;
least_conn;
}
server {
listen 3306;
proxy_pass mysqlsrvs; (与上面upstream名字对上)
}
}
2、listen
listen address:port [ssl][udp] [proxy_protocol][backlog=number] [bind][ipv6only=on|off] [reuseport][so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]];
ngx_stream_proxy_module模块
可实现代理基于TCP,UDP (1.9.13), UNIX-domain sockets的数据流
1 proxy_pass address;
指定后端服务器地址
2 proxy_timeout timeout;
无数据传输时,保持连接状态的超时时长
默认为10m
3 proxy_connect_timeout time;
设置nginx与被代理的服务器尝试建立连接的超时时长
默认为60s
示例
stream {
upstream mysqlsrvs {
server 192.168.0.10:3306;
server 192.168.0.11:3306;
hash $remote_addr consistent;
}
server {
listen 172.16.100.100:3306;
proxy_pass mysqlsrvs;
proxy_timeout 60s;
proxy_connect_timeout 10s;
}
}
实现Nginx高并发Linux内核优化(/etc/sysctl.conf)
-
由于默认的Linux内核参数考虑的是最通用场景,这明显不符合用于支持高并发访问的Web服务器的定义,所以需要修改Linux内核参数,是的Nginx可以拥有更高的性能,根据业务特点来进行调整,当Nginx作为静态web内容服务器、反向代理或者提供压缩服务器的服务器时,期内核参数的调整都是不同的,这里针对最通用的、使Nginx支持更多并发请求的TCP网络参数做简单的配置,修改/etc/sysctl.conf来更改内核参数
-
fs.file-max = 999999
表示单个进程较大可以打开的句柄数 -
net.ipv4.tcp_tw_reuse = 1
参数设置为 1 ,表示允许将TIME_WAIT状态的socket重新用于新的TCP链接,这对于服务器来说意义重大,因为总有大量TIME_WAIT状态的链接存在 -
net.ipv4.tcp_keepalive_time = 600
当keepalive启动时,TCP发送keepalive消息的频度;默认是2小时,将其设置为10分钟,可更快的清理无效链接 -
net.ipv4.tcp_fin_timeout = 30
当服务器主动关闭链接时,socket保持在FIN_WAIT_2状态的较大时间 -
net.ipv4.tcp_max_tw_buckets = 5000
这个参数表示操作系统允许TIME_WAIT套接字数量的较大值,如果超过这个数字,TIME_WAIT套接字将立刻被清除并打印警告信息,默认为8000,过多的TIME_WAIT套接字会使Web服务器变慢 -
net.ipv4.ip_local_port_range = 1024 65000
定义UDP和TCP链接的本地端口的取值范围 -
net.ipv4.tcp_rmem = 10240 87380 12582912
定义了TCP接受缓存的最小值、默认值、较大值 -
net.ipv4.tcp_wmem = 10240 87380 12582912
定义TCP发送缓存的最小值、默认值、较大值 -
net.core.netdev_max_backlog = 8096
当网卡接收数据包的速度大于内核处理速度时,会有一个列队保存这些数据包。这个参数表示该列队的较大值 -
net.core.rmem_default = 6291456
表示内核套接字接受缓存区默认大小 -
net.core.wmem_default = 6291456
表示内核套接字发送缓存区默认大小 -
net.core.rmem_max = 12582912
表示内核套接字接受缓存区较大大小 -
net.core.wmem_max = 12582912
表示内核套接字发送缓存区较大大小注意:以上的四个参数,需要根据业务逻辑和实际的硬件成本来综合考虑
-
net.ipv4.tcp_syncookies = 1
与性能无关。用于解决TCP的SYN攻击 -
net.ipv4.tcp_max_syn_backlog = 8192
这个参数表示TCP三次握手建立阶段接受SYN请求列队的较大长度,默认1024,将其设置的大一些可使出现Nginx繁忙来不及accept新连接时,Linux不至于丢失客户端发起的链接请求 -
net.ipv4.tcp_tw_recycle = 1
这个参数用于设置启用timewait快速回收 -
net.core.somaxconn=262114
选项默认值是128,这个参数用于调节系统同时发起的TCP连接数,在高并发的请求中,默认的值可能会导致链接超时或者重传,因此需要结合高并发请求数来调节此值。 -
net.ipv4.tcp_max_orphans=262114
选项用于设定系统中最多有多少个TCP套接字不被关联到任何一个用户文件句柄上。如果超过这个数字,孤立链接将立即被复位并输出警告信息。这个限制指示为了防止简单的DOS攻击,不用过分依靠这个限制甚至认为的减小这个值,更多的情况是增加这个值
源码编译安装
yum groupinstall "development tools"
yum install pcre-devel openssl-devel zlib-devel
useradd -r -s /sbin/nologin nginx
vim src/core/nginx.h
#define NGINX_VERSION "6.6.19"
#define NGINX_VER "DADDAnginx/" NGINX_VERSION
vim src/http/ngx_http_header_filter_module.c (修改后和server_tokens off;有关)
static u_char ngx_http_server_string[] = "Server:dushanwebsrv" CRLF;
(安装路径,配置文件路径,日志路径,访问日志路径,pid文件路径,锁路径,用户以谁身份运行,组谁身份运行,是否加密,是否使用http2.0版本支持,支持dav功能,支持sub卷,是否指示线程,是否启动文件的异步io)
./configure --prefix=/data/nginx \
--conf-path=/etc/nginx/nginx.conf \ 删除此行的话,默认放到/data/nginx下
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--pid-path=/var/run/nginx.pid \
--lock-path=/var/run/nginx.lock \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_dav_module \
--with-http_stub_status_module \
--with-threads --with-file-aio \
cd /data/nginx/sbin
./nginx
curl -I 192.168.32.17 测试
vim ningx.conf
http{
...
server_tokens off;
}
./nginx -reload
curl -I 192.168.32.17 再次测试