Nginx http相关常用配置总结

 

by:授客  QQ:1033553122

 

测试环境

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

Nginx http相关常用配置总结的更多相关文章

  1. 12: nginx原理及常用配置

    1.1 nginx基本介绍 1.nginx高并发原理( 多进程+epoll实现高并发 ) 1. Nginx 在启动后,会有一个 master 进程和多个相互独立的 worker 进程. 2. 每个子进 ...

  2. nginx安装以及常用配置

    nginx的源码安装 0 安装相关软件:yum -y install pcre-devel zlib-devel openssl-devel 1 下载 nginx-1.14.0.tar.gz 2 安装 ...

  3. nginx mime.types 常用配置

    常见mime type类型 nginx的mime.types 配置如下 types { text/html html htm shtml; text/css css; text/xml xml pli ...

  4. 最全面 Nginx 入门教程 + 常用配置解析

    转自 http://blog.csdn.net/shootyou/article/details/6093562 Nginx介绍和安装 一个简单的配置文件 模块介绍 常用场景配置 进阶内容 参考资料 ...

  5. 【转】【Nginx】Nginx 入门教程 + 常用配置解析

    == Nginx介绍和安装 == Nginx是一个自由.开源.高性能及轻量级的HTTP服务器及反转代理服务器, 其性能与IMAP/POP3代理服务器相当.Nginx以其高性能.稳定.功能丰富.配置简单 ...

  6. Nginx的一些常用配置

    #定义Nginx运行的用户和用户组 #user nobody; #nginx进程数,建议设置为等于CPU总核心数. worker_processes 1; #全局错误日志定义类型,[ debug | ...

  7. 快速掌握Nginx(四) —— Nginx日志切片和常用配置总结

    1.Nginx日志管理 1.日志简单介绍 Nginx提供了日志记录的功能,日志文件在对我们管理网站十分有用,通过访问日志(access_log)我们可以获取请求来源.客户端信息.请求的资源等信息:通过 ...

  8. 【第六课】Nginx常用配置下详解

    目录 Nginx常用配置下详解 1.Nginx虚拟主机 2.部署wordpress开源博客 3.部署discuz开源论坛 4.域名重定向 5.Nginx用户认证 6.Nginx访问日志配置 7.Ngi ...

  9. Nginx入门篇(四)之常用配置解析

    1.Nginx状态信息功能 Nginx的模块当中有一个ngx_http_stub_status_module模块,这个模块主要记录Nginx的基本访问信息,要使用该模块,需要在编译的时候增加http_ ...

随机推荐

  1. txt文本处理---行未添加逗号

    做音频处理过程中,经常遇到需要对文本进行转换,今天就遇到了一个行末加逗号的问题,找到了几种有效的方式,做个记录吧. 以下是几种方法实现: python代码实现: import os with open ...

  2. [干货,阅后进BAT不是梦]面试心得与总结---阿里、小米、腾讯

    之前实习的时候就想着写一篇面经,后来忙就给忘了,现在找完工作了,也是该静下心总结一下走过的路程了,我全盘托出,奉上这篇诚意之作,希望能给未来找工作的人一点指引和总结, 也希望能使大家少走点弯路 , 如 ...

  3. Oracle的条件in包含NULL时的处理

    一.创建一个含表,表中只有一列为id,该列中含有值为NULL的记录 我们在写SQL时经常会用到in条件,如果in包含的值都是非NULL值,那么没有特殊的,但是如果in中的值包含null值(比如in后面 ...

  4. Java核心技术及面试指南 键值对方面的面试题总结以及答案

    3.3.5.1如何遍历HashMap对象?尤其请说明通过Iterator遍历HashMap对象的方法. 建议用这种方式: Set<Entry<String,String>>en ...

  5. 解决app安装成功后,直接点击“打开”再按home返回,再次打开app会重新启动的问题

    在主activity的onCreate中加入以下代码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCr ...

  6. web进修之—Hibernate HQL(7)

    概述 HQL是Hibernate封装成为面向对象的数据库查询语言,具有如下特点: 面向对象,包括继承.多态和关联之类的概念,SQL操作的数据库的表,HQL更像是操作对象 大小写敏感,只对对象和属性敏感 ...

  7. jdk 8 的内存参数修改

    jdk内存实际是jvm内存,jvm有一个运行时数据区,其实就是对这一部分的大小分配.运行时数据区通常包括这几个部分:程序计数器(Program Counter Register).Java栈(VM S ...

  8. Angular2入门:TypeScript的接口

  9. 举例分析 Makefile 中的 filter 与 filter-out 函数

    $(filter pattern-,text) Returns all whitespace-separated words in text that do match any of the patt ...

  10. Runtime详解(上)

    这篇关于Runtime讲解参考https://juejin.im/post/593f77085c497d006ba389f0以及https://www.jianshu.com/p/6ebda3cd80 ...