前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Nginx http相关常用配置总结

Nginx http相关常用配置总结

作者头像
授客
发布2019-09-11 16:41:51
1K0
发布2019-09-11 16:41:51
举报
文章被收录于专栏:授客的专栏授客的专栏

测试环境

nginx-1.10.0

client_max_body_size

Syntax: client_max_body_size size;

Default: client_max_body_size 1m;

Context: http, server, location

设置允许的客户端请求体大小最大值,请求头域Content-Length指明的值。如果请求体大小超过配置设置值,返回413错误给客户端。需要注意的是,浏览器不定义可以正确的展示该错误。设置client_max_body_size 为0,禁用请求体大小检查。

例:设置客户端允许上传文件最大不超过15m

http {

……

client_max_body_size 15m;

……

}

参考链接:

http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size

location

Syntax: location [ = | ~ | ~* | ^~ ] uri { ... }

location @name { ... }

Default: —

Context: server, location

可用前缀字符串、正则表达式定义location,如果是正则表达式,则需要指定修饰符 ~*(大小写不敏感) 或者是 ~(大小写敏感)。为了在请求URI中查找匹配的location,nginx先匹配前缀字符串location,如果有多个匹配则会先记住拥有最长匹配前缀字符串的location(即匹配度最高的那个,和其在配置文件中的顺序无关),然后按location定义在配置文件中出现的顺序,从上到下,匹配正则表达式location,如果找到第一个匹配的location则停止查找,并使用这个location处理该请求,否则使用之前记住的最长匹配前缀字符串location。

location可支持嵌套。

特殊情况,如果最长匹配前缀location携带 ^~,则不会匹配正则表达式location。另外,如果使用 = 修饰符,则定义精确匹配URI location。如果找到精确匹配URI的location,则停止查找,这样在某些情况下,可以加速请求处理速度。这样的location显然不支持包含嵌套location。

例子:

假设nginx服务器地址192.168.1.102,监听端口8080,

location = / {

[ configuration A ]

}

location / {

[ configuration B ]

}

location /documents/ {

[ configuration C ]

}

location ^~ /images/ {

[ configuration D ]

}

location ~* \.(gif|jpg|jpeg)$ {

[ configuration E ]

}

如上配置

请求 http://192.168.1.102:8080/ 执行配置A

请求 http://192.168.1.102:8080/test.html 执行配置B,也就是说 / 匹配字符串,匹配任何URI

请求 http://192.168.1.102:8080/documents/document.html,执行配置C

请求 http://192.168.1.102:8080/images/1.gif,执行配置D

请求 http://192.168.1.102:8080/documents/1.jpg,执行配置E

总结:

= prefix_match_string 表示要求URI和prefix_match_string精确匹配,如果匹配成功,则停止搜索并用当前location处理此请求

~ regular_expression 表示正则表达式regular_expression同URI正则匹配,并且区分大小写

~* regular_expression 表示正则表达式regular_expression同URI正则匹配,但不区分大小写

^~ prefix_match_string 表示要求URI和prefix_match_strin“模糊”匹配找到最匹配location,则使用该location处理此请求,并不再进行正则匹配

参考链接:

http://nginx.org/en/docs/http/ngx_http_core_module.html#location

root

Syntax: root path;

Default:

root html;

Context: http, server, location, if in location

说明:

为请求设置根目录。path值支持变量($document_root 和$realpath_root除外)

例子:

location /i/ {

root /data/w3;

}

假设top.gif文件路径为/data/w3/i/top.gif, 请求URI为/i/top.gif,那么如果location匹配该URI,则服务器将会把/data/w3/i/top.gif返回给客户端,如果/data/w3/i/目录下不存在top.gif文件,那么默认的,nginx将会返回404错误。

通常,我们会这么做,把静态资源放nginx服务器,优先从nginx服务器上获取静态资源返回给前端,如果nginx服务器上找不到该文件,则去后端请求对应资源,如下:

location ~ \.(gif|jpg|png|html|js|css|zip|ico|json)$ {

root /data/Platform/;

//如果找不到请求文件,则转发请求到10.202.95.86:8080

if (!-e $request_filename){

proxy_pass http://10.202.95.86:8080;

break;

}

}

注意:if和(之间要有个空格,否则会报类似如下错误:

nginx: [emerg] unknown directive "if(" in /usr/local/ngnix/conf/nginx.conf:81

附文件和目录判断 -f 存在文件

!-f 不存在文件 -d 存在目录

!-d 不存在目录 -e 存在文件或目录

!-e 不存在文件或目录 -x 文件可执行

!-x 文件不可执行

参考链接:

http://nginx.org/en/docs/http/ngx_http_core_module.html#root

index

设置默认展示页面。

例子:

location / {

root html;

index index.html index.htm;

}

假设为配置location = / {}这种配置,那么当用户发起诸如 http://192.168.1.102:8080/、http://192.168.1.102/的请求时,自动匹配该location, nginx会在 root 配置指令指定的文件系统目录(默认html目录)下,按index指令设置,依次寻找 index.html 和index.htm这两个文件。如果 index.html 文件存在,则直接发起“内部跳转”到 /index.html 这个新的地址;如果 index.html 文件不存在,则继续检查 index.htm 是否存在。如果存在,同样发起“内部跳转”到/index.htm;如果 index.htm 文件仍然不存在,则404错误。

注意:假设请求携带非 / 的URI,形如http://192.168.1.102:8080/test.html,且仅匹配location / 则,则只会在html目录下查找该文件,如果找到了则返回,否则返回404,并不会执行index指令。

官网参考链接:无

rewrite

Syntax: rewrite regex replacement [flag];

Default: —

Context: server, location, if

如果指定正则表达式匹配某个请求URI,那么URI被替换为replacement字符串给定的值,然后继续处理这个替换后的请求。如果配置了多个rewrite指令,按rewrite指令在配置文件中出现的先后顺序执行。如果replacement以http://,https开头,则不进行URI替换,直接跳转到replacement给定的连接。

flag可选参数值如下:

last

停止当前指令集,并为改变都的URI开启一轮新的匹配。(stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;)

break

停止后续指令的处理。

redirect

返回临时的302重定向 仅replacement 不以http,https开头(returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;)

permanent

返回301永久重定向(returns a permanent redirect with the 301 code.)

The full redirect URL is formed according to the request scheme ($scheme) and the server_name_in_redirect and port_in_redirect directives.

更多资料(略)……

例子:

location = / {

rewrite / /home.html;

}

假设请求为:http://192.168.1.102/,那么将匹配以上location,并重写请求为:http://192.168.1.102/home.html

需要注意的地方是:

Syntax: rewrite regex replacement [flag];

当regex为 /,形如 rewrite / /index.html;且请求URI不为 /, 形如 http://192.168.1.102/index.html, 不能放在非 = / 定义的location中,否则会出现类似如下的错误:

*50 rewrite or internal redirection cycle while processing "/index.html"

另外,重写URI后,又会按新的URI发起新的请求,且进行location匹配,如下:

location = / {

rewrite / /index22.html;

}

location / {

root html;

rewrite / http://192.168.1.102/index.html;

index index.html index.htm;

}

假设请求为:http://192.168.1.102/,访问结果为:直接重定向到 http://192.168.1.102/index.html了。

参考链接:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

error_page

Syntax: error_page code ... [=[response]] uri;

Default: —

Context: http, server, location, if in location

定义用于显示指定请求错误的请求URI

例子:

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

当出现 500,502等错误时,会修改客户端请求方法GET,内部请求指定URI(/50x.html),即访问http://host:port/50x.html页面并返回给客户端展示。

还可以通过=response语法,改变响应代码。

error_page 404 =200 /empty.gif;

如果内部跳转过程中,不需要修改URI和方法,还可以传递错误处理到某个location

location / {

error_page 404 = @fallback;

}

location @fallback {

proxy_pass http://backend;

}

如果uri处理出错,返回最后产生的状态码给客户端。

也可以使用url重定向

error_page 403 http://example.com/forbidden.html;

error_page 404 =301 http://example.com/notfound.html;

更多资料参考:

http://nginx.org/en/docs/http/ngx_http_core_module.html#error_page

http://nginx.org/en/docs/http/ngx_http_core_module.html#http

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018-03-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 测试环境
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档