Nginx and DNS

本文介绍如何在Ubuntu上配置Nginx以使用外部DNS服务器8.8.8.8进行域名解析,并通过/etc/hosts文件配置内部域名映射。同时介绍了安装和配置dnsmasq作为本地DNS服务的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Nginx and DNS

On ubuntu-12.04, I need to run this
> sudo dpkg-reconfigure resolvconf

To solve the Error
resolvconf: Error: /etc/resolv.conf isn't a symlink, not doing anything

First of all, let me describe the issue.

I plan to use Nginx to proxy external domain which need 8.8.8.8 to resolve the DNS. Depend on the header value, the request main proxy to and internal IP which the domain name and IP configuration is configured in /etc/hosts.

Install the DNS service on Local
> sudo apt-get install dnsmasq

Start running the service
> sudo service dnsmasq start

The NGINX configuration will be similar to this:
map $http_user_agent $target {
default 'https://ocp.sillycatcloudbeta.com/SillycatServer';
"oldclientsproxy" 'http://local-hosts-external-ip:5080/SillycatServer';
"test" 'http://requestbin.fullcontact.com/1fio7ty1/SillycatServer';
}

location /SillycatServer/useragentapi {
resolver 127.0.0.1;
proxy_pass $target/useragentapi$is_args$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}
location /SillycatServer/provisioning {
resolver 127.0.0.1;
proxy_pass $target/provisioning$is_args$args;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_ssl_server_name on;
}


Then the resolver will first look up /etc/hosts and then it will working similar as 8.8.8.8 to look up AMAZON AWS domain.


References:
https://github.com/arrivu/beacon/wiki/Solution-for--------resolvconf:-Error:--etc-resolv.conf-isn't-a-symlink,-not-doing-anything
https://askubuntu.com/questions/54888/resolvconf-u-gives-the-error-resolvconf-error-etc-resolv-conf-must-be-a-sym
http://www.enkichen.com/2017/05/23/dnsmasq-introduce/
https://stackoverflow.com/questions/8305015/when-using-proxy-pass-can-etc-hosts-be-used-to-resolve-domain-names-instead-of/8559797#8559797
看起来您提到的是“nginx注册到nginx”,这表述有些模糊。如果是想了解如何让 Nginx 参与到服务网格(如 Istio)中的服务发现,或者是想询问关于 Nginx Ingress Controller 的注册流程,那么我们可以进一步澄清并详细解释。不过,通常我们不会直接说将 Nginx “注册” 到另一个 Nginx 实例上。 我猜测您的意图可能是想知道如何配置 Nginx 来作为反向代理服务器,并将其自身或其他后端服务注册到像 Consul、Eureka 或者 Nacos 这样的服务发现组件中,以便能够动态地管理和路由请求给正确的上游服务实例。 以下是针对几种典型场景的具体指导: ### 场景一:Nginx 配合外部服务发现系统 (例如 Nacos) 如果您想要通过 Nacos 对 Nginx 后面的微服务体系进行管理,您可以按照以下步骤操作: 1. **安装和配置 Nacos Server**: 确保已经有一个运行着的 Nacos 服务用于服务注册与发现。 2. **编写 Nginx 配置文件**: 使用 Lua 脚本或者其他插件技术,在每次启动时获取最新的后端服务列表,并更新 Nginx 的 upstream 设置。 3. **设置健康检查机制**: 让 Nginx 定期向上游发送心跳包来检测其状态的变化。 4. **利用 API 更新配置**: 如果需要实时同步变更信息,则可以通过调用 Nacos 提供的 RESTful 接口来进行配置刷新。 ### 场景二:使用 Nginx Plus 自动化功能 如果你使用的是商业版本 NGINX Plus, 它内置了一些高级特性可以直接支持自动化和服务发现: - **Dynamic DNS and IP Resolution with Real-Time Updates**: 支持从第三方来源如 AWS EC2 Metadata Service 获取最新主机名/IP地址。 - **Service Discovery via APIs or Scripting**: 内建脚本引擎允许开发者编写自定义逻辑连接各种后端管理系统。 - **Health Checks for Dynamic Upstream Groups**: 自动生成监控数据报告并且自动移除失效节点。 ### 场景三:采用开源工具链整合方案 当不想额外付费也不愿意过多侵入式改动现有架构的情况下,可以选择一些成熟的开源解决方案组合起来解决问题: - **Consul Template + Nginx**: 通过 consul-template 工具监听来自 Consul 的键值对变动事件,并据此生成新的 Nginx 配置文件片段;然后触发 reload 动作使得更改生效。 - **Registrator + Nginx**: Registrator 是一个小巧轻量级程序它会自动捕捉 Docker 容器生命周期内发生的创建销毁活动并将相关信息汇报给指定的服务总线平台(比如 etcd)。 - **Fleet+Systemd+Nginx**: 在 CoreOS/Fedora 平台上结合 systemd 单元文件可以很容易实现类似效果而不必担心跨宿主通讯难题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值