CentOS 7 安装 dnsmasq 解决nginx无法指定dns的问题

CentOS 7 安装 dnsmasq 解决nginx无法指定dns的问题

1. 安装 dnsmasq

bash 复制代码
yum install dnsmasq -y

如果无法安装没有网络可以下载本地的包

wget http://www.thekelleys.org.uk/dnsmasq/dnsmasq-2.91.tar.xz

tar -xvf dnsmasq-2.91.tar.xz

cd dnsmasq-2.91

make

make install

cp dnsmasq.conf.example /etc/dnsmasq.conf

启动为

/usr/local/sbin/dnsmasq -C /etc/dnsmasq.conf

2. 配置 dnsmasq

编辑配置文件:

bash 复制代码
nano /etc/dnsmasq.conf

添加/修改以下内容(取消注释或新增):

conf 复制代码
listen-address=127.0.0.1
port=53
no-resolv
no-poll


# 上游(步骤1+2)
all-servers
server=114.114.114.114#53
server=223.5.5.5#53
server=119.29.29.29#53
server=8.8.8.8#53
server=1.1.1.1#53

# 缓存(步骤3)
cache-size=20000
min-cache-ttl=600
max-cache-ttl=3600
dns-forward-max=1500

# 安全/杂项
domain-needed
bogus-priv
filter-AAAA
log-async=50

3. 启动并设置开机自启

bash 复制代码
systemctl start dnsmasq
systemctl enable dnsmasq

4. 配置 Nginx 使用本地 DNS

编辑 Nginx 配置文件(通常在 /etc/nginx/nginx.conf):

nginx 复制代码
resolver 127.0.0.1 valid=300s;
resolver_timeout 5s;

可手动安装 下载源码

官网地址https://thekelleys.org.uk/

传输dnsmasq-2.91.tar.xz到服务器 执行解压

tar -xvf dnsmasq-2.91.tar.xz

cd到解压的目录

cd dnsmasq-2.91

执行安装

make

执行添加

make install

然后添加配置

vi /etc/dnsmasq.conf

下面的为配置 不想手动cp可以从我发的包里面直接粘过去

复制代码
listen-address=127.0.0.1
port=53
no-resolv
no-poll

# 国内域名白名单(步骤1)
conf-dir=/etc/dnsmasq.d

# 上游(步骤2+3)
all-servers
server=114.114.114.114#53
server=223.5.5.5#53
server=119.29.29.29#53
server=8.8.8.8#53
server=1.1.1.1#53

# 缓存(步骤4)
cache-size=20000
min-cache-ttl=600
max-cache-ttl=3600
dns-forward-max=1500

# 安全/杂项
domain-needed
bogus-priv
filter-AAAA
log-async=50

然后添加到systemctl 里面

复制代码
echo -e "[Unit]
Description=dnsmasq servier
After=network.target

[Service]
ExecStart=/usr/local/sbin/dnsmasq -C /etc/dnsmasq.conf --keep-in-foreground
Restart=always

[Install]
WantedBy=multi-user.target" > /etc/systemd/system/dnsmasq.service

执行刷新

复制代码
systemctl daemon-reload

启动服务:

复制代码
systemctl start dnsmasq

关闭

复制代码
systemctl stop dnsmasq

设置开机自动启动:

复制代码
systemctl enable dnsmasq

5. 验证配置

  1. 测试 dnsmasq 是否正常工作

    bash 复制代码
    dig @127.0.0.1 www.baidu.com

    若能正常解析,说明 dnsmasq 已生效。

  2. 检查 dnsmasq 状态

    bash 复制代码
    systemctl status dnsmasq

    确保服务处于 active (running) 状态。

6. 故障排查(可选)

若遇到问题,可尝试:

  • 查看日志

    bash 复制代码
    journalctl -u dnsmasq
  • 检查端口占用

    bash 复制代码
    netstat -tulpn | grep :53

    确保 dnsmasq 在监听 53 端口。

配置效果

配置完成后,Nginx 将通过本地 dnsmasq 进行域名解析,而 dnsmasq 会:

  1. 自动选择响应最快的上游 DNS 服务器;
  2. 缓存解析结果以提高性能;
  3. 当某个服务器不可用时,自动切换到其他可用服务器。
相关推荐
ZFB00015 分钟前
【麒麟桌面系统】V10-SP1 2503 系统知识——添加用户
linux·运维·kylin
ZFB000122 分钟前
【麒麟桌面系统】V10-SP1 2503 系统知识——进入救援模式
linux·运维·kylin
RisunJan24 分钟前
Linux命令-iptables-restore命令(从文件批量恢复 iptables 防火墙规则)
linux·运维·网络
oMcLin27 分钟前
如何在 RHEL 7 上优化 Nginx 与 PHP‑FPM 配置,确保高并发 Web 应用的稳定性与响应速度?
前端·nginx·php
鲨莎分不晴1 小时前
Nginx 部署前端项目实战指南
运维·前端·nginx
知南x1 小时前
【STM32MP157 视频监控项目】(2) 移植 Nginx
stm32·nginx·音视频
hugerat2 小时前
在AI的帮助下,用C++构造微型http server
linux·c++·人工智能·http·嵌入式·嵌入式linux
ha20428941943 小时前
Linux操作系统学习记录之----自定义协议(网络计算器)
linux·网络·学习
想唱rap3 小时前
MYSQL在ubuntu下的安装
linux·数据库·mysql·ubuntu
糖~醋排骨3 小时前
DHCP服务的搭建
linux·服务器·网络