Nginx location 问题

匹配以aa、bb开头的的请求全部代理到test.com,配置如下:

location ^~ /aa/ {
    proxy_pass http://test.com/;    
}

location ^~ /bb/ {
    proxy_pass http://test.com/;    
}

请问aa、bb能否写到一个location里?

阅读 3.4k
2 个回答
location ~ ^/(aa|bb)/ {
    proxy_pass http://test.com/;   
}

本地测试:

server {
    server_name test.dev;

    location ~ ^/(aa|bb)/ {
        return https://www.baidu.com/;
    }
}

访问:

~ ❯❯❯ curl -I 'http://test.dev/aa/'
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.0
Date: Thu, 06 Jul 2017 06:43:50 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://www.baidu.com/
~ ❯❯❯ curl -I 'http://test.dev/bb/123'
HTTP/1.1 302 Moved Temporarily
Server: nginx/1.12.0
Date: Thu, 06 Jul 2017 06:44:24 GMT
Content-Type: text/html
Content-Length: 161
Connection: keep-alive
Location: https://www.baidu.com/

~ 支持正则表达式匹配的,你的正则能匹配 aa bb 就可以了

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题