大网数据广告活动
服务器运维问答

Avoiding duplication of directives for different locations?

· 52CE 我爱测

The following rules duplicate the proxy directives and I would prefer to only mention them once: location /view/ { # Duplicated! proxy_pass http://127.0.0.1:9292; proxy_set_header Host $host; } location /api/ { # Duplicated! prox…

问题原文

原帖链接 · Marcus Riemer · CC BY-SA 3.0

The following rules duplicate the proxy directives and I would prefer to only mention them once:

location /view/ {
    # Duplicated!
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;
}

location /api/ {
    # Duplicated!
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;

    auth_basic "esqulino Testbetrieb";
    auth_basic_user_file /srv/svn/users.conf;
}

There are more locations then the ones that I have mentioned here, so specifying the proxy directives globally is not an option.

My naive approach was to simply extend the first location with a regex, mention the proxy there and then only specifying the auth section for the API:

location ~ /(api|view)/ {
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;
}

location /api/ {
    auth_basic "esqulino Testbetrieb";
    auth_basic_user_file /srv/svn/users.conf;
}

But for some reason this does not trigger the authentication at all.

How could I mention the proxy (or actually any) directives only once for a set of locations and still "overload" some more specific locations with additional directives like auth_basic?

已采纳解答

原帖链接 · Phillip -Zyan K Lee- Stockmann · CC BY-SA 3.0

your problem is explained in the nginx documentation. only one of those locations is used, they are not merged together:

To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used. (http://nginx.org/en/docs/http/ngx_http_core_module.html#location)

as an alternative you could use an include and move the duplicated config lines to another file - for example below /etc/nginx/snippets/

EDIT The idea with nested locations from our comments:

Though I am not sure this is 100% correct as I did not test it, yet, try something like this:

location ~ /(view|api) {
    proxy_pass http://127.0.0.1:9292;
    proxy_set_header Host $host;

    location /api/ {
        auth_basic "esqulino Testbetrieb";
        auth_basic_user_file /srv/svn/users.conf;
    }
}

Another option might be to use if ($uri ~ ^/api) within the regex-location

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-07-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。