发布于 1年前

Nginx配置:多个location配置相同的规则

方法一:使用正则来做匹配

location ~ (pattern1|pattern2) {
  ...
}

如有/location1/和/location2/,可以配置如下:

location ~ ^/(location1|location2)/ {
  ...
}

但使用正则表达式,会存在性能问题,特别是在并发量高的情况。

方法二:引入规则

多个路径,每一个都配置一个location,然后相同规则写在配置文件中引入。

server {
    location /location1/ {
        include commom_rule.conf;
    }
    location /location2/ {
        include commom_rule.conf;
    }
}

规则文件内容就是{}里包含的内容,如:

default_type text/plain;
return 200 "http_user_agent:    $http_user_agent
remote_addr:    $remote_addr
remote_port:    $remote_port
scheme:     $scheme
nginx_version:  $nginx_version
";
©2020 edoou.com   京ICP备16001874号-3