nginx的HTTP配置、版本号设置、定义错误页面、文件检查、favicon.ico、客户端连接、请求报文配置、客户端限制、文件操作优化、文件缓存、文件下载服务、gzip压缩、IP访问控制、输出信息

nginx的HTTP配置

http配置:

http {
   server {
   listen address[:PORT]|PORT;
   server_name  SERVER_NAME;
   root /PATH/TO/DOCUMENT_ROOT;
   }	
}

listen
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; 指令指向不同的主机名

server_name
server_name name …;
虚拟主机的主机名称后可跟多个由空白字符分隔的字符串

支持*通配任意长度的任意字符
server_name .test.com www.test.

支持~起始的字符做正则表达式模式匹配,性能原因慎用
server_name ~^www\d+.test.com$
说明: \d 表示 [0-9]
匹配优先级机制从高到低
1.首先是字符串精确匹配 如:www.test.com
2.左侧通配符 如:.test.com
3.右侧通配符 如:www.test.
4.正则表达式 如: ~^.*.test.com$
5.default_server

定义路径相关的配置
root
设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用
于http, server, location, if in location
server {

root /data/www/vhost1;
}

示例
http://www.test.com/images/logo.jpg
–> /data/www/vhosts/images/logo.jpg

location
location [ = | ~ | ~* | ^~ ] uri { … }
location @name { … }
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路
径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最
佳匹配,而后应用其配置

示例:

server {
	server_name www.test.com;
	location /images/ {
		root /data/imgs/;
	}
} 
http://www.test.com/images/logo.jpg
--> /data/imgs/images/logo.jpg

= 对URI做精确匹配;
location = / {

}
http://www.test.com/ 匹配
http://www.test.com/index.html 不匹配
^~ 对URI的最左边部分做匹配检查,不区分字符大小写
~ 对URI做正则表达式模式匹配,区分字符大小写
~* 对URI做正则表达式模式匹配,不区分字符大小写
不带符号 匹配起始于此uri的所有的uri
\ 转义符,可将 . * ?等转义为普通符号
匹配优先级从高到低:
=, ^~, ~/~*, 不带符号
注意:虽然nginx上不区分大小写,但是linux文件系统依旧是敏感的

示例:

root /vhosts/www/htdocs/;
http://www.test.com/index.html
--> /vhosts/www/htdocs/index.html

server {
	root /vhosts/www/htdocs/ ;
	location /admin/ {
		root /webapps/app1/data/;
	}
}
http://www.test.com/admin/index.html
--> /webapps/app1/data/admin/index.html

匹配示例

location = / {
	[ configuration A ]
}
location / {
	[ configuration B ]
}
location /documents/ {
	[ configuration C ]
}
location ^~ /images/ {
	[ configuration D ]
}
location ~* \.(gif|jpg|jpeg)$ {
	[ configuration E ]
}

http://www.test.com/ 
访问根,能匹配A和B,但是因为A带了=,优先级最高,所以最终匹配A

http://www.test.com/index.html
A匹配只有根,这里多了index.html所以匹配B

http://www.test.com/documents/a.txt
C匹配的路径比A B都多,所以匹配C

http://www.test.com/images/a.jpg
匹配了路径和文件名,但是因为优先级高,所以匹配D

http://www.test.com/documents/b.jpeg
因为E带符号,优先级比C高,所以匹配文件名,最终匹配E

root
root指定虚拟主机根目录,在定义location时,文件的绝对路径等于 root+location

示例:

server {
	listen 80;
	server_name www.test.com;
	location / {
		root /data/nginx/html/a;
	}
	location /test {
		root /opt/nginx/html/a;
		#必须要在html目录中创建一个test目录才可以访问,否则报错
		index index.html;
	}
}

静态资源配置示例
location ^~ /static/ {
	root /data/nginx/static;
}

或者
location ~* \.(gif|jpg|jpeg|png|bmp|tiff|tif|css|js|ico)$ {
	root /data/nginx/static;
}

alias
alias可以定义路径别名,文档映射的另一种机制;仅能用于location上下文

http://www.test.com/a/index.html
location /a {  注意: /a 后不要加 /
alias /b/test/;
} --> /b/testf/index.html

location /a/ {
root /b/test/;
} --> /b/test/a/index.html

注意:location中使用root指令和alias指令的意义不同
root,给定的路径对应于location中的/uri 左侧的/
alias,给定的路径对应于location中的/uri 的完整路径

其他参数

在响应报文中将指定的文件扩展名映射至MIME对应的类型
include /etc/nginx/mime.types;
default_type application/octet-stream; 除上面指定的类型外,就为默认的MIME类型,浏览器一般会提示下载
types {
text/html html;
image/gif gif;
image/jpeg jpg;
}

tcp_nodelay on | off;
在keepalived模式下的连接是否启用TCP_NODELAY选项,即Nagle算法当为off时,延迟发送,每发送一个包就需要确认ACK,才发送下一个包默认On时,不延迟发送,多个包才确认一次可用于:http, server, location

tcp_nopush on | off ;
在开启sendfile,on时合并响应头和数据体在一个包中一起发送

sendfile on | off;
是否启用sendfile功能,在内核中封装报文直接发送,默认Off

charset charset | off;
是否在响应报文中的Content-Type显示指定的字符集,默认off不显示

版本号设置

server_tokens on | off | build | string;
是否在响应报文的Server首部显示nginx版本

如果想自定义响应报文的nginx版本信息,需要修改源码文件,重新编译
如果server_tokens on,修改 src/core/nginx.h 例如
#define NGINX_VERSION “1.88.88”
#define NGINX_VER “Testnginx/” NGINX_VERSION

如果server_tokens off,则在此文件中
src/http/ngx_http_header_filter_module.c
找到
static char ngx_http_server_string[] = “Server: nginx” CRLF;
把其中的nginx改为自己想要的文字即可,如:Testnginx

定义错误页面

error_page code … [=[response]] uri;
定义错误页,以指定的响应状态码进行响应
可用位置:http, server, location, if in location

示例:

自定义404页面
error_page 404 /404.html;
location = /40x.html {
}

404错误时修改为200,同时返回自定义的页面
error_page 404 =200 /404.html

50x的错误都返回50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
} 

统一返回一个错误页面
server_name www.test.com;
error_page 500 502 503 504 404 /error.html;
location = /error.html {
root /data/nginx/html;
}

文件检查

try_files file … uri;
try_files file … =code;
按顺序检查文件是否存在,返回第一个找到的文件或文件夹(结尾加斜线表示为文件夹),如果所有文件或文件夹都找不到,会进行一个内部重定向到最后一个参数。只有最后一个参数可以引起一个内部重定向,之前的参数只设置内部URI的指向

此时查找用户输入的URI如果没有就返回default.jpg
location /images/ {
try_files $uri /images/default.jpg;
} 

如果全找不到就报404错误
location / {
try_files $uri $uri/index.html $uri.html =404;
}

## favicon.ico favicon.ico 文件是浏览器收藏网址时显示的图标,当使用浏览器访问页面时,浏览器会自己主动发起请求获取页面的favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错

解决方案:服务器不记录访问日志
location = /favicon.ico {
log_not_found off; #文件没发现事件不记录error_log
access_log off; #不记录access_log
}

将图标保存到指定目录访问:
#location ~ ^/favicon.ico$ {
location = /favicon.ico {
root /data/nginx/html/pc/images;
}

客户端连接设置

keepalive_timeout timeout [header_timeout];
设定保持连接超时时长,0表示禁止长连接,默认为75s
示例:
在响应头显示此首部字段
keepalive_timeout 60 60;

keepalive_requests number;
在一次长连接上所允许请求的资源的最大数量,默认为100

keepalive_disable none | browser …;
对哪种浏览器禁用长连接

send_timeout time;
向客户端发送响应报文的超时时长,此处是指两次写操作之间的间隔时长,而非整个响应过程的传输时长

请求报文配置

client_max_body_size size;
指定请求报文中实体的最大值,设为0,则不限制,默认1m,超过报413错误

client_body_buffer_size size;
用于接收每个客户端请求报文的body部分的缓冲区大小;默认为16k;超出此大小时,其将被暂存到磁盘上的由下面client_body_temp_path指令所定义的位置

client_body_temp_path path [level1 [level2 [level3]]];
设定存储客户端请求报文的body部分的临时存储路径及子目录结构和数量目录名为16进制的数字;用hash之后的值从后往前截取第1、2、3级作为文件名
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

示例:

location /upload {
	client_max_body_size 100m;
	client_body_buffer_size 2048k;
	client_body_temp_path /apps/nginx/temp 1 2 2;
}

客户端限制配置

limit_rate rate;
限制响应给客户端的传输速率,单位是bytes/second,默认值0表示无限制

limit_except method … { … },仅用于location
限制客户端使用除了指定的请求方法之外的其它方法
method:GET, HEAD, POST, PUT, DELETE,MKCOL, COPY, MOVE,OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, PATCH

示例:

limit_except GET {
allow 192.168.1.0/24;
deny all;
}
除了GET之外其它方法仅允许192.168.1.0/24网段主机使用
正常拒绝提示403,如果提示405则是页面不支持当前方法

文件操作优化

aio on | off | threads[=pool];
是否启用aio功能,默认off

directio size | off;
当文件大于等于给定大小时,同步(直接)写磁盘,而非写缓存,默认off

示例:
location /video/ {
sendfile on;
aio on;
directio 10m;
}

文件缓存的配置

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指令所指定的次数的缓存项即为非活动项,将被删除

open_file_cache_errors on | off;
是否缓存查找时发生错误的文件一类的信息,默认值为off

open_file_cache_min_uses number;
open_file_cache指令的inactive参数指定的时长内,至少被命中此处指定的次数方可被归类为活动项,默认值为1

open_file_cache_valid time;
缓存项有效性的检查频率,默认值为60s

配置文件下载服务

autoindex on | off;
自动文件索引功能,默为off

autoindex_exact_size on | off;
计算文件确切大小(单位bytes),off 显示大概大小(单位K、M),默认on

autoindex_localtime on | off ;
显示本机时间而非GMT(格林威治)时间,默认off

autoindex_format html | xml | json | jsonp;
显示索引的页面文件风格,默认html

配置示例:
location /download {
autoindex on;
autoindex_exact_size off;
autoindex_localtime on;
autoindex_format json;
limit_rate 100k;
root /data/nginx/test;
index index.html;
}
mkdir /data/nginx/test/download/

gzip压缩模块

用gzip方法压缩响应数据,节约带宽
使用模块ngx_http_gzip_module

gzip on | off;
启用或禁用gzip压缩

gzip_comp_level level;
压缩比由低到高:1 到 9, 默认:1

gzip_disable regex …;
匹配到客户端浏览器不执行压缩
示例:gzip_disable “MSIE[1-6].”;

gzip_min_length length;
启用压缩功能的响应报文大小阈值

gzip_http_version 1.0 | 1.1;
设定启用压缩功能时,协议的最小版本,默认:1.1

gzip_buffers number size;
支持实现压缩功能时缓冲区数量及每个缓存区的大小
默认:32 4k 或 16 8k

gzip_types mime-type …;
指明仅对哪些类型的资源执行压缩操作;即压缩过滤器
默认包含有text/html,不用显示指定,否则出错

gzip_vary on | off;
如果启用压缩,是否在响应报文首部插入“Vary: Accept-Encoding”

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值任何一个,启用压缩功能

示例:
gzip on;
gzip_comp_level 6;
gzip_min_length 64;
gzip_vary on;
gzip_types text/xml text/css application/javascript;

IP访问控制模块

使用ngx_http_access_module模块可实现基于ip的访问控制功能
allow address | CIDR | unix: | all;
deny address | CIDR | unix: | all;
可出现的位置:http, server, location, limit_except
自上而下检查,一旦匹配,将生效,条件严格的置前

示例:
location /about {
root /data/nginx/html/pc;
index index.html;
deny 192.168.1.10;
allow 192.168.1.0/24;
allow 10.1.1.0/16;
allow 2001:0db8::/32;
deny all; #先允许小部分,再拒绝大部分
}

验证用户的模块

使用ngx_http_auth_basic_module模块可实现基于用户的访问控制,使用basic机制进行用户认证
auth_basic string | off;
auth_basic_user_file file;

location /admin/ {
auth_basic “Admin Area”;
auth_basic_user_file /etc/nginx/.ngxpasswd;
}

用户口令文件:
1.明文文本:格式name:password:comment
22.加密文本:由htpasswd命令实现
httpd-tools所提供

输出信息模块

使用ngx_http_stub_status_module模块输出nginx的基本状态信息, 输出信息示例:
Active connections: 291
server accepts handled requests #下面三个数分别对应accepts,handled,requests 16630948 16630948 31070465
Reading: 6 Writing: 179 Waiting: 106
Active connections:当前状态,活动状态的连接数
accepts:统计总值,已经接受的客户端请求的总数
handled:统计总值,已处理完成的客户端请求总数,一般和accepts相同,除非拒绝
requests:统计总值,客户端发来的总的请求数
Reading:当前状态,正在读取客户端请求报文首部的连接的连接数
Writing:当前状态,正在向客户端发送响应报文过程中的连接数
Waiting:当前状态,正在等待客户端发出请求的空闲连接数

示例:
location /nginx_status {
stub_status;
allow 127.0.0.1;
allow 192.168.1.0/24;
deny all;
}

第三方模块

第三模块是对nginx 的功能扩展,第三方模块需要在编译安装nginx 的时候使用参数–add-module=PATH指定路径添加,有的模块是由公司的开发人员针对业务需求定制开发的,有的模块是开源爱好者开发好之后上传到github进行开源的模块,nginx支持第三方模块,需要重新编译源码才能支持。

编译前需要先装git和下载echo模块的代码

yum install git –y
cd /usr/local/src
git clone https://github.com/openresty/echo-nginx-module.git

编译示例:

./configure \
--prefix=/app/nginx \
--user=nginx --group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_stub_status_module \
--with-http_gzip_static_module \
--with-http_perl_module \
--with-pcre \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module \
--add-module=/usr/local/src/echo-nginx-module

make && make install

推荐模块:开源的echo模块,实现输出变量等信息
https://github.com/openresty/echo-nginx-module

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值