麒麟V10离线安装nginx,通过源码编译(本地配置了yum源,但没有nginx),并配置dns的负载均衡, 及常用命令

nginx

一、下载nginx和nginx_upstream_check_module-master源码包,拷贝至服务器

  1. 我下载的是nginx1.22.1
# nginx源码
https://nginx.org/download/nginx-1.22.1.tar.gz
# nginx_upstream_check_module-master源码,用来支持DNS的upd负载
https://codeload.github.com/yaoweibin/nginx_upstream_check_module/zip/refs/heads/master

我把源码上传到git了

https://gitee.com/dosedo/nginx-upstream.git
  1. 先解压,再压缩zip格式的

  2. 使用scp拷贝至服务器

scp nginx-1.22.1.zip root@192.168.1.9:~
  1. 进入服务器,unzip解压压缩包
unzip nginx-1.22.1.zip

二、编译安装nginx

1、本地yum源配置

上传一个iso的源文件,然后挂载到/mnt目录下

sudo mount -o loop iso文件路径 /mnt/

新建local.repo文件,把其他的repo文件暂时移走

sudo vi /etc/yum.repos.d/local.repo

local.repo文件内容

###Kylin Linux Advanced Server 10 - os repo###

[local_server]
name = Local Kylin Server
baseurl = file:///mnt
gpgcheck = 0
enabled = 1

2、编译安装nginx

安装依赖

sudo yum groupinstall -y "Development Tools"
sudo yum install -y pcre-devel zlib-devel openssl-devel

cd进入nginx-1.22.1目录,编译安装
这里的nginx_upstream_check_module-master为它的源码目录

./configure --with-stream --add-module=../nginx_upstream_check_module-master
make -j$(nproc)
sudo make install

3、安装完成后,查看版本

# 使用如下命令查看nginx位置
whereis nginx

# 这里的位置在/usr/local/nginx/sbin/目录下
/usr/local/nginx/sbin/nginx -v

版本

4、将nginx加入systemd服务中

创建nginx的服务文件

sudo vi /etc/systemd/system/nginx.service

nginx.service内容如下:注意nginx替换为自己的nginx路径

[Unit]
Description=Nginx - high performance web server
After=network.target

[Service]
Type=forking
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

重加载systemd服务

sudo systemctl daemon-reload

就可以正常使用systemctl管理nginx了

#启动
systemctl start nginx
#查看状态
systemctl status nginx
#停止
systemctl stop nginx

状态


三、配置nginx为dns服务器做负载均衡

1、编辑nginx配置文件

这里nginx配置文件在:/usr/local/nginx/conf/nginx.conf
编辑之前,先备份原有的配置文件nginx.conf

mv /usr/local/nginx/conf/nginx.conf  /usr/local/nginx/conf/nginx.conf_bak 

编辑nginx配置文件:

vi /usr/local/nginx/conf/nginx.conf 

填写dns负载内容:
nginx本机ip为:192.168.0.8
DNS两台机器为:192.168.0.9 和 192.168.0.2

# /etc/nginx/nginx.conf 或 /usr/local/nginx/conf/nginx.conf

worker_processes auto;

# 设置 Nginx 的 worker 进程数量
#worker_processes auto;

events {
worker_connections 1024;
}

# 配置负载均衡的 stream 模块
stream {
# 定义日志格式(可自定义字段)
log_format basic '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time';

# 启用日志
access_log /var/log/nginx/stream_access.log basic;
error_log /var/log/nginx/stream_error.log;

# 定义 DNS 服务器集群
upstream dns_cluster {
server 192.168.0.9:53 max_fails=3 fail_timeout=30s; # DNS服务器1
server 192.168.0.2:53 max_fails=3 fail_timeout=30s; # DNS服务器2

# 这里可以根据需要调整健康检查
# check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}

# TCP 协议监听
server {
listen 192.168.0.8:53; # 监听端口 53 (TCP)
proxy_pass dns_cluster; # 将请求转发给 dns_cluster
proxy_connect_timeout 1s; # 与 upstream 服务器连接的超时时间
proxy_timeout 3s; # 代理超时时间
}

# UDP 协议监听
server {
listen 192.168.0.8:53 udp reuseport; # 监听端口 53 (UDP)
proxy_pass dns_cluster; # 将请求转发给 dns_cluster
# proxy_responses 1; # 预期接收的响应数量
proxy_timeout 3s; # 代理超时时间
}
}

http {
server {
listen 192.168.0.9:8080; # 只允许本地访问

location /nginx_status {
# stub_status on;
access_log off; # 关闭访问日志
allow 192.168.0.8; # 只允许本地访问
# deny all; # 拒绝其他主机访问
}
}
}

日志文件路径
access_log /var/log/nginx/stream_access.log basic;
error_log /var/log/nginx/stream_error.log;

2、检查配置文件,并重启nginx

检查nginx配置文件是否正确

/usr/local/nginx/sbin/nginx -t

重启nginx服务

systemctl restart nginx

遇到错误:open xxx stream_access.log” failed

nginx: [emerg] open() "/var/log/nginx/stream_access.log" failed (2: No such file or directory)
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

原因是/var/log/nginx/目录不存在,新建目录

mkdir /var/log/nginx

再次重启nginx就可以了。

3、防火墙放行DNS的53端口

检查防火墙状态

systemctl status firewalld
systemctl status iptables

这里只开启了firewalld的防火墙,所以只用它放行53和8081端口
放行udp53和tcp53

firewall-cmd --permanent --zone=public --add-port=53/udp
firewall-cmd --permanent --zone=public --add-port=53/tcp

重新加载防火墙

firewall-cmd --reload

查看放行的端口

firewall-cmd --zone=public --list-ports

四、验证DNS负载

找一台电脑,将DNS地址配置为Nginx的地址。

如果域名为:www.eg.com

使用nslookup命令,查看解析是否成功

nslookup www.eg.com

或者使用dig命令(ip为nginx服务的地址)

dig @192.168.0.8 www.eg.com

五、问题记录

问题1:nginx与bind dns在同一台机器,53端口冲突问题
解决方案:
让DNS服务,只监听127.0.0.1的53端口

# DNS配置文件部分内容
options {
listen-on port 53 { 127.0.0.1; };
listen-on-v6 port 53 { none; };
xxxx
}

同时DNS服务,在转发时,转发到127.0.0.1的53端口上

# 定义 DNS 服务器集群
upstream dns_cluster {
server 127.0.0.1:53 max_fails=3 fail_timeout=30s; # DNS服务器1
server 192.168.0.2:53 max_fails=3 fail_timeout=30s; # DNS服务器2

# 这里可以根据需要调整健康检查
# check interval=3000 rise=2 fall=3 timeout=1000 type=tcp;
}

六、相关命令备忘

## linux设置DNS地址,在/etc/resolv.conf中添加
[root@i-sq7eZizx7-9 named]# cat /etc/resolv.conf
; Created by cloud-init on instance boot automatically, do not edit.
;
nameserver 192.168.0.2
[root@i-sq7eZizx7-9 named]#

#防火墙放行udp53和tcp53
firewall-cmd --permanent --zone=public --add-port=53/udp
firewall-cmd --permanent --zone=public --add-port=53/tcp


#重新加载防火墙
firewall-cmd --reload

#查看放行的端口
firewall-cmd --zone=public --list-ports

## 查看端口占用情况

lsof -i :53
netstat -tuln | grep :53
ss -tuln | grep :53

## 测试DNS查询
# TCP 查询
dig @nginx-ip example.com +tcp

# UDP 查询
dig @nginx-ip example.com


# 检查nginx配置文件是否正确
/usr/local/nginx/sbin/nginx -t

# 检查udp端口情况
nc -vzu 192.168.0.9 53

# mac清空DNS缓存
sudo killall -HUP mDNSResponder

# windowns清空dns
ipconfig /flushdns

# 查看编译时nginx的参数(大V)
/usr/local/nginx/sbin/nginx -V

## 查看Linux系统版本
[root@i-sq7eZizx7-2 ~]# cat /etc/kylin-release
Kylin Linux Advanced Server release V10 (Sword)
[root@i-sq7eZizx7-2 ~]#

uname -a
uname -v
微信打赏

意外收获认可,内心充满感激;打赏之举,更添无限动力。