[关闭]
@adonia 2018-05-25T06:12:15.000000Z 字数 20767 阅读 248

Nginx配置

nginx


Nginx配置文件

Nginx配置文件默认是${NGINX_HOME}/conf/nginx.conf.default,重命名为nginx.conf。主要内容如下:

  1. main
  2. events {
  3. ....
  4. }
  5. http {
  6. ....
  7. upstream myproject {
  8. .....
  9. }
  10. server {
  11. ....
  12. location {
  13. ....
  14. }
  15. }
  16. server {
  17. ....
  18. location {
  19. ....
  20. }
  21. }
  22. ....
  23. }
  • main为全局配置

  • events为Nginx的工作模式

  • http为请求服务配置

  • upstream为负载均衡器配置

  • server为主机配置

  • location为请求路由配置

main 模块

Nginx的默认全局配置如下:

  1. #user nobody;
  2. worker_processes 1;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. #worker_rlimit_nofile 1024;
  • user是用来指定Nginx Worker进程用户和属组,默认是nobody,也可以在使用源码编译时,通过--user=nginx --group=users命令指定。

  • worker_processes指明了Nginx Worker子进程的个数。可以设置跟CPU的核数一致,每个Nginx进程平均耗费10~12M内存。

  • error_log用来定义全局错误日志文件。日志输出级别有debug、info、notice、warn、error、crit。也可以在使用源码编译时,通过--error-log-path=/opt/nginx/log/error.log指定。

  • pid指明了Nginx进程id存储的文件,也可以在使用源码编译时,通过--pid-path=/opt/nginx/conf/nginx.pid指定。

  • worker_rlimit_nofile指定了Nginx可打开的最大文件句柄树,需要和ulimit -n同步考虑。

如下是worker_processes设置为2时,查询到的进程情况:

  1. nginx@NKG1000009441:~/conf> pgrep nginx
  2. 32847
  3. 58111
  4. 58112
  5. nginx@NKG1000009441:~/conf> ps -ef | grep 32847
  6. nginx 32847 1 0 May05 ? 00:00:00 nginx: master process nginx
  7. nginx 58111 32847 0 09:30 ? 00:00:00 nginx: worker process
  8. nginx 58112 32847 0 09:30 ? 00:00:00 nginx: worker process
  9. nginx 58878 58020 0 09:42 pts/1 00:00:00 grep 32847

events 模块

events模块用来设置Nginx的工作模式和连接上限,如下:

  1. events {
  2. use epoll;
  3. worker_connections 1024;
  4. }
  • use指明了Nginx的工作模式,即处理客户端连接的方式,包括select、poll、kqueue、epoll、rtsig和/dev/poll。其中,epollkqueue为高效的处理方式,不同的是epoll支持Linux系统,而kqueue支持BSD系统。详见Nginx Connection processing methods

  • worker_connections定义了每个Worker进程的最大连接数,即能够处理的最大客户端请求数。那么,Nginx能够处理的最大请求数即为worker_connections * worker_processes。同样,进程的最大连接数与ulimit -n的设置有关。

http 模块

http模块负责了服务器的相关属性配置,包括主机配置和负载均衡。如下:

  1. http{
  2. server_tokens off;
  3. include mime.types;
  4. default_type application/octet-stream;
  5. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  6. '$status $body_bytes_sent "$http_referer" '
  7. '"$http_user_agent" "$http_x_forwarded_for"';
  8. access_log /usr/local/var/log/nginx/access.log main;
  9. sendfile on;
  10. tcp_nopush on;
  11. tcp_nodelay on;
  12. keepalive_timeout 10;
  13. #gzip on;
  14. upstream myproject {
  15. .....
  16. }
  17. server {
  18. ....
  19. }
  20. }
  • server_tokens off可以在错误页面隐藏nginx的版本号,提高安全性。(开源软件都会有每个版本的bug list和release notes)

  • include引用了文件的mime类型的定义,mime.types即指$NGINX_HOME/conf/mime.types配置文件。

  • default_type指明了如果需要解析的文件类型不在上述的mime.types列表的话,将采用默认的解析方式,此处的application/octet-stream表示二进制流。

  • log_format定义了日志格式,标识为main,跟access_log中的main对应。即access_log的日志格式将采用log_format的定义。

  • sendfile用于开启高效的文件传输模式,将tcp_nopushtcp_nodelay设置为on用于防止网络阻塞。

server 模块

  1. server {
  2. listen 28888;
  3. server_name 10.179.161.233;
  4. root html;
  5. index index.html;
  6. #error_page 404 /404.html;
  7. # redirect server error pages to the static page /50x.html
  8. #
  9. error_page 500 502 503 504 /50x.html;
  10. #charset utf-8;
  11. #access_log logs/host.access.log main;
  12. }
  • listen指明服务端的监听端口。

  • server_name用来指明IP或者域名,多个IP或者域名要使用空格分隔。

示例

假设现在有一系统,提供了两个服务模块---APIDOCS,分别提供API查询和在线资料帮助。服务域名为www.adonia.org。那么正常情况下,访问两个服务的URI会分别为http://www.adonia.org/apihttp://www.adonia.org/docs

但是,为了方便区分,想要通过http://api.adonia.org访问API系统,通过http://docs.adonia.org访问DOCS系统。怎么处理呢?

先来建立模拟服务,在Nginx的安装目录下创建server目录,作为模拟服务的根目录,其下包括apidocs目录,分别表示上述两个服务。如下:

  1. /opt/nginx
  2. |--server
  3. |--api
  4. |--index.html
  5. \--docs
  6. |--index.html

api/index.html代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>API</title>
  6. </head>
  7. <body>
  8. <h2>API Page</h2>
  9. </body>
  10. </html>

docs/index.html代码如下:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>DOCS</title>
  6. </head>
  7. <body>
  8. <h2>DOCS Page</h2>
  9. </body>
  10. </html>

server目录下启动http-server,即可通过http://10.179.161.233:8080/apihttp://10.179.161.233:8080/docs访问两个服务了(10.179.161.233为测试环境IP,8080为http-server的默认端口)。

当然,这个结果只是为了测试模拟服务是可用了,那下面看下如何通过Nginx来达到上述的目的。(http-server可以关闭了)

先看下未加入任何主机的nginx.conf的配置,如下:

  1. #user nobody;
  2. worker_processes 2;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. use epoll;
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. }

为了方便管理主机,将新增的两个主机配置放到单独的文件中,分别为api.nginx.confdocs.nginx.conf,两者类似。api.nginx.conf的配置如下:

  1. server {
  2. listen 8080; # 服务端监听端口,由于Nginx不是root用户执行的,无法使用80端口
  3. server_name api.adonia.org; # 服务端监听地址,即预期的服务访问地址
  4. index index.html;
  5. access_log log/api.access.log main; # 访问日志,'main'为日志格式,在'nginx.conf'中定义的
  6. error_log log/api.error.log error; # 错误日志,注意此处的'error'为日志级别
  7. location / {
  8. root server/api; # 请求资源所在的根目录,如果是相对路径,则是相对于Nginx的安装目录
  9. index index.html; # 默认页面
  10. }
  11. }

要使上述配置生效,还需将其引入至nginx.conf中。将api.nginx.conf保存在$NGINX_HOME/conf目录,即与nginx.conf同目录。在nginx.confhttp模块配置中增加如下配置:

  1. include api.nginx.conf;

重启Nginx服务---nginx -s reload

由于api.adonia.org域名是自定的,需要手动增加映射,在Nginx所在机器的/etc/hosts中增加如下配置:

  1. 10.179.161.233 api.adonia.org

10.179.161.233为Nginx服务器IP。

此处,通过curl api.adonia.org:8080应该就可以访问到server/api/index.html资源文件了。

那在Windows端如何测试呢?同样也需要增加域名映射,在C:\Windows\System32\drivers\etc\hosts中增加:

  1. 10.179.161.233 api.adonia.org

在浏览器中输入http://api.adonia.org:8080/api,应该就可以呈现下面的内容:

  1. API Page

docs.nginx.conf的配置如下,操作同api:

  1. server {
  2. listen 8080;
  3. server_name docs.adonia.org; # 服务端监听地址
  4. index index.html;
  5. access_log log/api.access.log main;
  6. error_log log/api.error.log error;
  7. location / {
  8. root server/docs; # 请求资源根路径
  9. index index.html;
  10. }
  11. }

nginx.conf的最终配置如下:

  1. #user nobody;
  2. worker_processes 2;
  3. #error_log logs/error.log;
  4. #error_log logs/error.log notice;
  5. #error_log logs/error.log info;
  6. #pid logs/nginx.pid;
  7. events {
  8. use epoll;
  9. worker_connections 1024;
  10. }
  11. http {
  12. include mime.types;
  13. default_type application/octet-stream;
  14. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  15. '$status $body_bytes_sent "$http_referer" '
  16. '"$http_user_agent" "$http_x_forwarded_for"';
  17. #access_log logs/access.log main;
  18. sendfile on;
  19. #tcp_nopush on;
  20. #keepalive_timeout 0;
  21. keepalive_timeout 65;
  22. #gzip on;
  23. include api.nginx.conf;
  24. include docs.nginx.conf;
  25. }

注意http模块的配置即可。

反向代理

先说下正向代理,即代理,就如同在浏览器中设置的代理服务器。比如客户端(ClientA)要请求服务端(ServerB)的资源,但是ClientA和ServerB的网络不通。这时候,就需要有个代理服务器(ServerC),能够同时与ClientA和ServerB通信。

示例如下:

  1. ClientA ServerB ServerA
  2. | /api | /api |
  3. |--------------->|---------------->|
  4. | Response | Response |
  5. |<---------------|<----------------|
  6. | | |

而反向代理则相反,比如客户端(ClientA)要请求服务端(ServerB)的资源(/api),但是ServerB并没有该资源。于是ServerB就向第三方服务(ServerC)请求,而这一请求动作,ClientA是不感知的。ServerB把从ServerC得到的响应信息转发给ClientA。

示例

基于上面的两个服务(APIDOCS),新增服务(www.adonia.org),访问其下的不同路径,反向代理到上述的两个服务上。如下:

  1. http://www.adonia.org/api ---> http://api.adonia.org
  2. http://www.adonia.org/docs ---> http://docs.adonia.org

$NGINX_HOME/conf下增加主机配置adonia.nginx.conf,内容如下:

  1. server {
  2. listen 8081; # 服务监听端口,为了与上述服务区分,特意设置不一样的端口
  3. server_name www.adonia.org; # 服务监听地址
  4. access_log log/adonia.access.log main;
  5. error_log log/adonia.error.log error;
  6. index index.html;
  7. location /api {
  8. proxy_pass http://api.adonia.org:8080/; # 反向代理服务地址
  9. }
  10. location /docs {
  11. proxy_pass http://docs.adonia.org:8080/;
  12. }
  13. }

nginx.confhttp模块引用上述主机配置:

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. '$status $body_bytes_sent "$http_referer" '
  6. '"$http_user_agent" "$http_x_forwarded_for"';
  7. #access_log logs/access.log main;
  8. sendfile on;
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. #gzip on;
  13. include api.nginx.conf;
  14. include docs.nginx.conf;
  15. include adonia.nginx.conf;
  16. }

重启Nginx---nginx -s reload。访问http://www.adonia.org:8081/api,应该可以得到如下响应:

  1. API Page

注意域名映射。

需要特别说明下反向代理服务地址的配置,如下:

  1. location /api {
  2. proxy_pass http://api.adonia.org:8080/; # 反向代理服务地址
  3. }

我们在定义api服务时,访问的请求是http://api.adonia.org:8080,但是配置反向代理时,一定不能遗漏了最后的/,因为这个表示访问的是API服务的根路径资源,再根据api.nginx.conf的配置,请求将加载/api/index.html。如果不加上/,Nginx会自动拼接上此处定义的location信息,最终访问的请求就变成了http://api.adonia.org:8080/api,将加载的资源则是server/api/api,就会报404 Not Found的错误了。

  1. 2016/05/18 16:39:11 [error] 83404#0: *39 open() "/opt/nginx/server/api/api" failed (2: No such file or directory), client: 10.179.161.233, server: api.adonia.org, request: "GET /api HTTP/1.0", host: "api.adonia.org:8080"

负载均衡

在集群场景下,需要根据使用的场景来配置不同的请求分发策略,就涉及到负载均衡的配置。

Nginx中的使用upstream模块来实现负载均衡的配置的,配置样例如下:

  1. upstream servers{
  2. ip_hash;
  3. server 192.168.12.1:80;
  4. server 192.168.12.2:80 down;
  5. server 192.168.12.3:8080 max_fails=3 fail_timeout=20s;
  6. server 192.168.12.4:8080;
  7. }

Note:

  • servers为定义的负载均衡器的名称,可以任意指定。

  • ip_hash是一种负载均衡算法。

  • server则为后端服务器信息。

Nginx的负载均衡模块目前支持4种调度算法:

在HTTP Upstream模块中,可以通过server指令指定后端服务器的IP地址和端口,同时还可以设定每个后端服务器在负载均衡调度中的状态。常用的状态有:

注意 当负载调度算法为ip_hash时,后端服务器在负载均衡调度中的状态不能是weight和backup。

示例

现在模拟集群场景,包括两个集群节点---10.179.161.23310.135.13.1,分别使用http-server加载之前模拟服务的apidocs,不过这里要稍作下修改,以便区分是哪个集群节点上的资源。如下:

10.179.161.233/api/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>API</title>
  6. </head>
  7. <body>
  8. <h2>API Page In 10.179.161.233</h2>
  9. </body>
  10. </html>

10.135.13.1/api/index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>API</title>
  6. </head>
  7. <body>
  8. <h2>API Page In 10.135.13.1</h2>
  9. </body>
  10. </html>

分别在两台机器上加载服务,http-server -p 8082。此处,由于Nginx也运行在10.179.161.233上,为了防止两者的端口冲突,将模拟服务的端口设置为8082。此时,访问http://10.179.161.233:8082/apihttp://10.135.13.1:8082/api,应该都是能够得到对应的响应信息的。

$NGINX_HOME/conf下增加负载均衡配置---upstream.nginx.conf,内容如下:

  1. upstream api{
  2. server 10.135.13.1:8082 weight=1; # 集群节点1
  3. server 10.179.161.233:8082 weight=2; # 集群节点2
  4. }
  5. server{
  6. listen 8080; # 服务监听端口
  7. server_name 10.179.161.233; # 服务监听地址
  8. access_log log/upstream.access.log main;
  9. error_log log/upstream.error.log error;
  10. location / {
  11. proxy_pass http://api; # 请求反向代理至集群列表
  12. proxy_set_header X-Real-IP $remote_addr;
  13. }
  14. }

将负载均衡配置引入至Nginx的http模块,如下:

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. '$status $body_bytes_sent "$http_referer" '
  6. '"$http_user_agent" "$http_x_forwarded_for"';
  7. #access_log logs/access.log main;
  8. sendfile on;
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. #gzip on;
  13. include upstream.nginx.conf;
  14. #include api.nginx.conf;
  15. #include docs.nginx.conf;
  16. #include adonia.nginx.conf;
  17. }

重启Nginx服务---nginx -s reload。访问http://10.179.161.233:8080/api,持续刷新,响应结果应该会一直在下面两个中切换:

  1. API Page In 10.179.161.233
  1. API Page In 10.135.13.1

而且,API Page In 10.179.161.233出现的概率为 2/3

如果把上述的负载均衡算法设置为ip_hash的话,配置如下:

  1. upstream api{
  2. ip_hash;
  3. server 10.135.13.1:8082;
  4. server 10.179.161.233:8082;
  5. }
  6. server{
  7. listen 8080;
  8. server_name 10.179.161.233;
  9. access_log log/upstream.access.log main;
  10. error_log log/upstream.error.log error;
  11. location / {
  12. proxy_pass http://api;
  13. proxy_set_header X-Real-IP $remote_addr;
  14. }
  15. }

访问http://10.179.161.233:8080/api/,持续刷新,会发现响应结果是不变的。注意: 使用ip_hash算法,就不建议再配置weight了。

rewrite 模块

Nginx Rewrite模块负责URL重写,主要用于同一服务器内的资源重定向。语法格式如下:

  1. rewrite regex replacement flag
  • regex为资源的正则匹配

  • replacement为重定向后的资源路径

  • flag为重定向标记

flag包括:

  • last: url重写后,会发起新的请求,重新进入'server'模块,进行location匹配,如果匹配超过10次扔不成功,则报500错误,地址栏url不变。

  • break: url重写后,直接使用当前资源,不再执行location余下语句,完成本次请求,地址栏url不变。

  • redirect: 返回302临时重定向,url会跳转,爬虫不会更新url。

  • permanent: 返回301永久重定向。url会跳转。爬虫会更新url

  • 空值: URL 不会变,但是内容已经变化,也是永久性的重定向。

示例

$NGINX_HOME/html目录下增加文本文件,如下:

  1. /opt/nginx
  2. |--html
  3. |--index.html
  4. \--50x.html
  5. \--file
  6. |--a.txt

html/file/a.txt为简单文本文件,内容如下:

  1. text a

按照Nginx默认的主机配置的话,访问http://10.179.161.233:8080/file/a.txt应该就可以显示上述文本内容。

Note: 10.179.161.233为Nginx测试机器的IP。

现在更改下规则,想要在访问根路径下的资源文件时,能够被重定向至file目录下。比如访问http://10.179.161.233:8080/a,实际加载的是html/file/a.txt文件。

如何配置呢?
$NGINX_HOME/conf下新增rewrite.nginx.conf,内容如下:

  1. server{
  2. listen 8080;
  3. server_name 10.179.161.233;
  4. location / {
  5. root html;
  6. index index.html;
  7. rewrite '^(.+)$' /file/$1.txt break;
  8. }
  9. }

nginx.conf的http模块中引入rewrite.nginx.conf配置,如下:

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. '$status $body_bytes_sent "$http_referer" '
  6. '"$http_user_agent" "$http_x_forwarded_for"';
  7. #access_log logs/access.log main;
  8. sendfile on;
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. #gzip on;
  13. include rewrite.nginx.conf;
  14. }

重启Nginx---nginx -s reload,访问http://10.179.161.233:8080/a,应该就能得到如下的响应结果:

  1. text a

一个问题

按照上述的配置,所有以"/"开头的请求就将被重写,包括Nginx的默认首页index.html,访问http://10.179.161.233:8080,得到的是404 Not Found。查看错误日志:

  1. 2016/05/19 14:15:49 [error] 44662#0: *33 open() "/opt/nginx/html/file//.txt" failed (2: No such file or directory), client: 10.135.13.1, server: 10.179.161.233, request: "GET / HTTP/1.1", host: "10.179.161.233:8080"

那像这种情况,如果针对指定的uri,不想让请求被重写,而是按照原先的逻辑路由,怎么配置呢?

serverlocation模块,支持if判断指令。语法为if(condition){...},对给定的条件condition进行判断。如果为真,大括号内的rewrite指令将被执行,if条件(conditon)可以是如下任何内容:

-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行

下面是可以用作if判断的全局变量:

  • $args: #这个变量等于请求行中的参数,同$query_string

  • $content_length: 请求头中的Content-length字段。

  • $content_type: 请求头中的Content-Type字段。

  • $document_root: 当前请求在root指令中指定的值。

  • $host: 请求主机头字段,否则为服务器名称。

  • $http_user_agent: 客户端agent信息

  • $http_cookie: 客户端cookie信息

  • $limit_rate: 这个变量可以限制连接速率。

  • $request_method: 客户端请求的动作,通常为GET或POST。

  • $remote_addr: 客户端的IP地址。

  • $remote_port: 客户端的端口。

  • $remote_user: 已经经过Auth Basic Module验证的用户名。

  • $request_filename: 当前请求的文件路径,由root或alias指令与URI请求生成。

  • $scheme: HTTP方法(如http,https)。

  • $server_protocol: 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。

  • $server_addr: 服务器地址,在完成一次系统调用后可以确定这个值。

  • $server_name: 服务器名称。

  • $server_port: 请求到达服务器的端口号。

  • $request_uri: 包含请求参数的原始URI,不包含主机名,如:"/foo/bar.php?arg=baz"。

  • $uri: 不带请求参数的当前URI,$uri不包含主机名,如"/foo/bar.html"。

  • $document_uri: 与$uri相同。

那么,针对上述情况,就可以处理为: 如果请求路径是"/",则按照默认的路由规则,被映射到"/index.html",如下:

  1. location =/ {
  2. root html;
  3. index index.html;
  4. }

N.B.: 注意此处的=,这里表示路径的精确匹配。

重载Nginx,再试下访问http://10.179.161.233:8080,发现响应依旧是404 Not Found,查看错误日志:

  1. 2016/05/19 14:17:34 [error] 44789#0: *37 open() "/opt/nginx/html/file//index.html.txt" failed (2: No such file or directory), client: 10.135.13.1, server: 10.179.161.233, request: "GET / HTTP/1.1", host: "10.179.161.233:8080"

原来,请求被location =/匹配后,uri被映射为/index.html,之后请求又被location /匹配,请求被重写为/file/index.html.txt

那就需要在location /加个判断逻辑,过滤*.html的请求,不做任何处理。如下:

  1. server{
  2. listen 8080;
  3. server_name 10.179.161.233;
  4. location =/ {
  5. root html;
  6. index index.html;
  7. }
  8. location / {
  9. root html;
  10. if ($uri ~ "\.html$") {
  11. break; # 如果请求以'html'结尾,则不做处理
  12. }
  13. rewrite '^(.+)$' /file/$1.txt break;
  14. }
  15. }

重载之后,应该就可以正常访问了。

再看 rewrite 标识

前面介绍到rewrite的语法为: rewrite regex replacement flag,其中flag包括lastbreakredirectpermanent。那看下上述场景,分别使用几种标识得到的结果是什么。

  1. 2016/05/19 14:34:13 [error] 45836#0: *43 rewrite or internal redirection cycle while processing "/file//file//file//file//file//file//file//file//file//file//file//a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt", client: 10.135.13.1, server: 10.179.161.233, request: "GET /a HTTP/1.1", host: "10.179.161.233:8080"
  1. 2016/05/19 14:39:06 [error] 46165#0: *1 rewrite or internal redirection cycle while processing "/file//file//file//file//file//file//file//file//file//file//file//a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt", client: 10.135.13.1, server: 10.179.161.233, request: "GET /a HTTP/1.1", host: "10.179.161.233:8080"
  1. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//a.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  2. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//file/a.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  3. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//file/file/a.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  4. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//file/file/file/a.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  5. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//file/file/file/file/a.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  6. 10.135.13.1 - - [19/May/2016:14:40:27 +0800] "GET /file//file/file/file/file/file/a.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  7. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  8. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  9. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  10. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  11. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  12. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  13. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  14. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  15. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  16. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  17. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  18. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  19. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  20. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"
  21. 10.135.13.1 - - [19/May/2016:14:40:28 +0800] "GET /file//file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/file/a.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt.txt HTTP/1.1" 302 161 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"

从上述结果也可以看出,last并没有阻止继续执行location匹配;而break可以防止上述匹配的情况,直接使用rewrite后的资源。

permanentredirect都会改变地址栏中的请求路径,最终导致匹配次数超过10次,服务端报出'500'的错误。浏览器中也显示重定向的次数过多。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注