Nginx http相关常用配置总结

简介: Nginx http相关常用配置总结

Nginx http相关常用配置总结

 


 

测试环境

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

 

 

目录
相关文章
|
21天前
|
缓存 前端开发 JavaScript
终极 Nginx 配置指南(全网最详细)
本文详细介绍了Nginx配置文件`nginx.conf`的基本结构及其优化方法。首先通过删除注释简化了原始配置,使其更易理解。接着,文章将`nginx.conf`分为全局块、events块和http块三部分进行详细解析,帮助读者更好地掌握其功能与配置。此外,还介绍了如何通过简单修改实现网站上线,并提供了Nginx的优化技巧,包括解决前端History模式下的404问题、配置反向代理、开启gzip压缩、设置维护页面、在同一IP上部署多个网站以及实现动静分离等。最后,附上了Nginx的基础命令,如安装、启动、重启和关闭等操作,方便读者实践应用。
234 84
终极 Nginx 配置指南(全网最详细)
|
10天前
|
JavaScript 应用服务中间件 开发工具
vue尚品汇商城项目-day07【53.nginx反向代理配置】
vue尚品汇商城项目-day07【53.nginx反向代理配置】
22 4
|
10天前
|
缓存 应用服务中间件 nginx
nginx如何配置?配置项都是什么意思?
nginx如何配置?配置项都是什么意思?
27 1
|
13天前
|
应用服务中间件 nginx Docker
docker应用部署---nginx部署的配置
这篇文章介绍了如何使用Docker部署Nginx服务器,包括搜索和拉取Nginx镜像、创建容器并设置端口映射和目录映射,以及如何创建一个测试页面并使用外部机器访问Nginx服务器。
|
2月前
|
应用服务中间件 nginx Docker
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
这篇文章介绍了如何通过域名在本地访问虚拟机上的nginx服务,包括创建nginx容器、修改配置文件、修改本地host文件以及进行访问测试的详细步骤。文章提供了具体的Docker命令来创建并配置nginx容器,展示了配置文件的修改示例,说明了如何在本地系统的hosts文件中添加虚拟机IP和自定义域名,以及如何通过浏览器进行测试访问。
本地通过域名访问虚拟机上nginx的服务、搭建域名访问环境一(反向代理配置)
|
1月前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
74 3
|
1月前
|
Ubuntu 应用服务中间件 数据库
Nginx配置:阻止非国内IP地址访问的设置方法
此外,出于用户隐私和法律合规性的考虑,应慎重考虑阻止特定国家或地区IP地址的决策。在某些情况下,这可能被视为歧视性或违反当地法律。
65 2
|
13天前
|
应用服务中间件 nginx 索引
7-15|Nginx配置
7-15|Nginx配置
|
2月前
|
Ubuntu 应用服务中间件 Linux
在Linux中,如何配置Web服务器(如Apache或Nginx)?
在Linux中,如何配置Web服务器(如Apache或Nginx)?
|
2月前
|
缓存 负载均衡 应用服务中间件
【揭秘】nginx代理配置全攻略:从零到精通,一文带你玩转高效网络代理的秘密武器!
【8月更文挑战第22天】nginx是一款高性能的HTTP与反向代理服务器,支持代理服务、负载均衡及缓存等功能,有助于提升网站响应速度和安全性。首先需确保已安装nginx,可通过包管理器进行安装。安装后启动并确认nginx运行状态。接着编辑配置文件(通常位于`/etc/nginx/nginx.conf`),设置代理转发规则,例如指定目标服务器地址和请求头信息。配置完成后测试有效性并重新加载nginx以应用更改。可以通过部署简易HTTP服务器验证代理功能是否正常工作。此外,还可以通过扩展配置文件实现更复杂的代理需求,如基于路径的代理和SSL加密等。
328 2