You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotnet-learn-vs/WebMVC/WebMVCApi/md/Nginx反向代理域名但域名IP变化后还解析的是变化前...

47 lines
1.8 KiB
Markdown

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

---
icon: edit
date: 2023-03-26
category:
- 系统配置
tag:
- nginx
headerDepth: 5
---
# Nginx反向代理域名但域名IP变化后还解析的是变化前的IP解决方法
之前博客的代理 proxy_pass 时直接指定域名是可以用的,比如
```
location / {
proxy_pass http://local.wuanwanghao.top:30550;
}
```
#### 遇到一个问题是:
如果路由器因为断电或者掉线之类的原因重新拨号后ip发生变化此处nginx就无法反向代理了必须重启一次nginx才行。
#### 解决方法:
```
server {
listen 8080;
server_name localhost;
ssl_prefer_server_ciphers on;
#配置域名解析 validDNS缓存时间也可以不指定缓存时间会默认根据域名的TTL时间 ipv6on或off指定该域名解析为ipv6地址。
resolver 202.102.134.68 114.114.114.114 valid=30 ipv6=off;
#指定解析域名时DNS服务器的超时时间也可以不指定
resolver_timeout 30s;
#设置变量在使用resolver之后必须使用set设置变量来代替域名否则会报错
set $true_url "local.wuanwanghao.top";
location / {
proxy_pass http://$true_url:30550;
}
}
```
resolver后接指定的DNS服务器多个用空格隔开。resolver可以在http里面全局设定也可以在server里面设定。
validDNS缓存时间也可以不指定缓存时间会默认根据域名的TTL时间。
ipv6on或off指定该域名解析为ipv6地址。
resolver_timeout指定解析域名时DNS服务器的超时时间也可以不指定。
set 设置变量在使用resolver之后必须使用set设置变量来代替域名否则会报错。另外set不能写到 location里面否则不会生效。注意set变量在server中可以设置成功http中语法不允许。
这样才解决动态dns解析访问问题。