概述
nginx配置http协议的相关配置
- ngx_http_core_module
- tcp_nodelay on | off
- tcp_nopush on | off
- sendfile on | off
- charset charset | off
- server_tokens on | off | build | string
- 自定义nginx版本信息
- 与套接字相关的配置
- listen PORT|address[:port]|unix:/PATH/TO/SOCKET_FILE
- server_name name ...;
- 定义路径相关的配置
- root
- location [ = | ~ | ~* | ^~ ] uri { ... }
- 生产案例:静态资源配置
- alias path
- index file ...;
- try_files file ... uri;
- 定义客户端请求的相关配置
- keepalive_timeout timeout [header_timeout];
- keepalive_requests number;
- keepalive_disable none | browser ...;
- send_timeout time;
- client_max_body_size size;
- client_body_buffer_size size;
- client_body_temp_path path [level1 [level2 [level3]]];
- 对客户端进行限制的相关配置
- limit_rate rate
- limit_except method ... { ... },仅用于location
- 文件操作优化的配置
- aio on | off | threads[=pool]
- directio size | off
- open_file_cache off
- open_file_cache_errors on | off
- open_file_cache_min_uses number
- open_file_cache_valid time
ngx_http_core_module
在响应报文中将指定的文件扩展名映射至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版本信息
如果想自定义响应报文的nginx版本信息,需要修改源码文件,重新编译
如果server_tokens on,修改 src/core/nginx.h 修改第13-14行,如下示例
#define NGINX_VERSION "1.68.9"
#define NGINX_VER "wanginx/" NGINX_VERSION
如果server_tokens off,修改 src/http/ngx_http_header_filter_module.c
第49行,如下示例:
static char ngx_http_server_string[] = “Server: nginx” CRLF;
把其中的nginx改为自己想要的文字即可,如:wanginx
与套接字相关的配置
server { ... }
配置一个虚拟主机
server {
listen address[:PORT]|PORT;
server_name SERVER_NAME;
root /PATH/TO/DOCUMENT_ROOT;
}
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 name …;
虚拟主机的主机名称后可跟多个由空白字符分隔的字符串
支持*通配任意长度的任意字符
server_name *.magedu.com www.magedu.*
支持~起始的字符做正则表达式模式匹配,性能原因慎用
server_name ~^wwwd+.magedu.com$
说明: d 表示 [0-9]
匹配优先级机制从高到低
(1) 首先是字符串精确匹配 如:www.magedu.com
(2) 左侧*通配符 如:*.magedu.com
(3) 右侧*通配符 如:www.magedu.*
(4) 正则表达式 如: ~^.*.magedu.com$
(5) default_server
定义路径相关的配置
root
设置web资源的路径映射;用于指明请求的URL所对应的文档的目录路径,可用于http, server, location, if in location
server {
...
root /data/www/vhost1;
}
示例
http://www.magedu.com/images/logo.jpg
--> /data/www/vhosts/images/logo.jpg
location [ = | ~ | ~* | ^~ ] uri { … }
location @name { ... }
在一个server中location配置段可存在多个,用于实现从uri到文件系统的路径映射;ngnix会根据用户请求的URI来检查定义的所有location,并找出一个最佳匹配,而后应用其配置
示例:
server {...
server_name www.magedu.com;
location /images/ {
root /data/imgs/;
} }
http://www.magedu.com/images/logo.jpg
--> /data/imgs/images/logo.jpg
= 对URI做精确匹配;
location = / {
...
}
http://www.magedu.com/ 匹配
http://www.magedu.com/index.html 不匹配
^~ 对URI的最左边部分做匹配检查,不区分字符大小写
~ 对URI做正则表达式模式匹配,区分字符大小写
~* 对URI做正则表达式模式匹配,不区分字符大小写
不带符号 匹配起始于此uri的所有的uri
转义符,可将 . * ?等转义为普通符号
匹配优先级从高到低:
=, ^~, ~/~*
, 不带符号
示例:
root /vhosts/www/htdocs/;
http://www.magedu.com/index.html
--> /vhosts/www/htdocs/index.html
server {
root /vhosts/www/htdocs/ ;
location /admin/ {
root /webapps/app1/data/;
}
}
http://www.magedu.com/admin/index.html
--> /webapps/app1/data/admin/index.html
location = / {
[ configuration A ] http://www.magedu.com/
}
location / {
[ configuration B ] http://www.magedu.com/index.html
}
location /documents/ {
[ configuration C ] http://www.magedu.com/documents/logo.jpg
}
location ^~ /images/ {
[ configuration D ] http://www.magedu.com/documents/linux.txt
}
location ~* .(gif|jpg|jpeg)$ {
[ configuration E ] http://www.magedu.com/images/logo.jpeg
}
root 指定虚拟主机根目录,在定义location时,文件的绝对路径等于 root+location,
示例:
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}
location /about {
root /opt/nginx/html/pc;
#必须要在html目录中创建一个about目录才可以访问,否则报错
index index.html;
}
}
#mkdir /opt/nginx/html/pc/about
#echo about > /opt/nginx/html/pc/about/index.html
生产案例:静态资源配置
location ^~ /static/ {
root /data/nginx/static;
}
#或者
location ~* .(gif|jpg|jpeg|png|bmp|tiff|tif|css|js|ico)$ {
root /data/nginx/static;
}
alias path
路径别名,文档映射的另一种机制;仅能用于location上下文
示例:
http://www.magedu.com/bbs/index.html
location /bbs { 注意: /bbs 后建议不要加 /
alias /web/forum/;
} --> /web/forum/index.html
location /bbs/ {
root /web/forum/;
} --> /web/forum/bbs/index.html
注意:location中使用root指令和alias指令的意义不同
(a) root,给定的路径对应于location中的/uri 左侧的/
(b) alias,给定的路径对应于location中的/uri 的完整路径
alias定义路径别名,会把访问的路径重新定义到其指定的路径,如下示例:
server {
listen 80;
server_name www.magedu.net;
location / {
root /data/nginx/html/pc;
}#使用alias的时候uri后面如果加了斜杠则下面的路径配置必须加斜杠,否则403
location /about {
#当访问about时,访问alias定义的/opt/nginx/html/pc内容
alias /opt/nginx/html/pc;
index index.html;
}
}
index file …;
指定默认网页文件,此指令由ngx_http_index_module模块提供
error_page code … [=[response]] uri;
定义错误页,以指定的响应状态码进行响应
可用位置:http, server, location, if in location
示例:
error_page 404 /404.html;
location = /40x.html {
}
error_page 404 =200 /404.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
生产案例:
listen 80;
server_name www.magedu.net;
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的 指向
location /images/ {
try_files $uri /images/default.jpg;
} 说明:/images/default.jpg 为 URI
location / {
try_files $uri $uri/index.html $uri.html =404;
}
定义客户端请求的相关配置
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和HEAD 之外其它方法仅允许192.168.1.0/24网段主机使用
文件操作优化的配置
aio on | off | threads[=pool]
是否启用aio功能,默认off
directio size | off
当文件大于等于给定大小时,同步(直接)写磁盘,而非写缓存,默认off
示例:
location /video/ {
sendfile on;
aio on;
directio 8m; }
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
最后
以上就是清新往事为你收集整理的nginx配置http协议的相关配置ngx_http_core_module与套接字相关的配置定义路径相关的配置定义客户端请求的相关配置对客户端进行限制的相关配置文件操作优化的配置的全部内容,希望文章能够帮你解决nginx配置http协议的相关配置ngx_http_core_module与套接字相关的配置定义路径相关的配置定义客户端请求的相关配置对客户端进行限制的相关配置文件操作优化的配置所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复