关于Nginx中路由报错 no resolver defined to resolve localhost

dragon72463399

drake

Posted on August 26, 2023

关于Nginx中路由报错 no resolver defined to resolve localhost
  • 该问题是由动态解析造成的
  • 比如下面的配置: > 该路由规则 proxy_pass 后面跟的是变量,当该位置是变量的时候Nginx会在每个请求中动态解析localhost, 而这就会导致resolver错误
        location ~ ^/k8sapi(/(.*))?$ {
            proxy_set_header    Host  $host;
            proxy_set_header    X-Real-IP  $remote_addr;
            proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
            proxy_pass https://localhost:6443/$2;
        }
Enter fullscreen mode Exit fullscreen mode
  • 而如果是静态解析
  • 比如下面的配置: > proxy_pass 后面跟的是一个具体的值,该值会被硬编码到proxy_pass指令中,只会在Nginx启动的时候解析一次,并且缓存结果;那么这种方式就不会报错
        location ~ ^/k8sapi(/(.*))?$ {
            proxy_set_header    Host  $host;
            proxy_set_header    X-Real-IP  $remote_addr;
            proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_redirect off;
            proxy_pass https:localhost:6443/;
        }

Enter fullscreen mode Exit fullscreen mode
  • How do I fix the problem? Two scenarios:
    • 1. Add a line to the routing rule
    location ~ ^/k8sapi(/(.*))?$ {
        resolver 127.0.0.1;  # 添加这一行
        proxy_set_header    Host  $host;
        proxy_set_header    X-Real-IP  $remote_addr;
        proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
        proxy_pass https://localhost:6443/$2;
    }
Enter fullscreen mode Exit fullscreen mode
  • 2、直接换成回环地址 https://127.0.0.1:6443/$2
💖 💪 🙅 🚩
dragon72463399
drake

Posted on August 26, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related