What is the process of Nginx dynamic domain name resolution?

10-25-2023

Nginx will resolve the domain name when performing reverse proxy, which will be cached locally after resolving the domain name into a specific IP. If the IP corresponding to the domain name changes, it will lead to the invalidation of the Nginx proxy. The following uses the resolver instruction of Nginx to realize the dynamic resolution of the domain name.

Dynamic analysis of intranet DNS server based on custom DNS server. I use dnsmasq

Default.conf configures server {listen 80; root /usr/share/nginx/html/; resolver 192.168.137.110 valid=5s; set $proxy_url huzhihui.local; include /etc/nginx/default.d/*.conf; location / { index index.html index.htm ; try_files $uri $uri/ /index.html; client_max_body_size 100m; add_header tenantId $arg_tenantId; add_header appId $arg_appId; } location /api/ { proxy_set_header Host $http_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; rewrite /api/(.*) /$1 break; proxy_pass http://$proxy_url:8070; }}

The main configuration point resolver: configure the DNS server and resolve the validity of the address,

Set: used to cooperate with resolver to resolve the IP address of the domain name.

Rewrite: modify the access path

Proxy_pass: The proxy service you actually jump to, don't take the path parameter, because it has been rewritten with rewrite.

Start access to CoreDns dynamic domain name resolution default.confserver {listen 80; root /usr/share/nginx/html/; resolver kube-dns.kube-system.svc.cluster.local valid=10s; set $a_part_host a-part-http-service.huzhihui.svc.cluster.local; include /etc/nginx/default.d/*.conf; location / { index index.html index.htm ; try_files $uri $uri/ /index.html; client_max_body_size 100m; add_header tenantId $arg_tenantId; add_header appId $arg_appId; } location /api/ { proxy_set_header Host $http_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_pass http://127.0.0.1:5031/; } location /a-part/ { client_max_body_size 100m; proxy_set_header Host $http_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; rewrite /a-part/(.*) /api/$1 break; proxy_pass http://$a_part_host; } location ~ .*.(html)$ { #html disabled cache add_header Cache-Control no-store; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { }}

Main configuration point kube-DNS. kube-system.svc.cluster.local: This domain name is the domain name of K8S DNS.

A-part-http-service.huzhihui.svc.cluster.local: A-part-http-service is the svc name defined by K8S, and huzhihui is the namespaces defined by me.

After the above configuration, the ip change after svc restart will not cause the reverse proxy to fail.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us