小知识:nginx反向代理进行yum配置的步骤详解

part.0 使用背景

公司内网服务器不能直接通过Internet上网,但为了与外网通信和同步时间等,会指定那么几台服务器可以访问Internet。这里就是通过能上网的机器作为代理,制作内网使用的yum仓库。

part.1 环境

内网dns(推荐,非必须,因为可使用IP代替)

一台能上Internet的服务器A

不能上Internet的服务器能与A服务器通信

part.2 nginx安装

在可连接外网的A中安装nginx

?
1
yum install nginx

part.3 nginx配置

在主机A中添加nginx配置

?
1
2
$ cd /etc/nginx/conf.d
$ vim proxy.conf
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
server {
listen 80;
#listen [::]:80;
server_name mirrors.yourdomain.com;
index index.html index.htm index.php default.html default.htm default.php;
root /home/wwwroot/html;
location /ubuntu/ {
proxy_pass http://mirrors.aliyun.com/ubuntu/ ;
}
location /centos/ {
proxy_pass http://mirrors.aliyun.com/centos/ ;
}
location /epel/ {
proxy_pass http://mirrors.aliyun.com/epel/ ;
}
}

part.4 配置yum repo 源

修改无法连接外网的主机B 的repo文件。

?
1
$ cat /etc/yum.repos.d/CentOS-7.repo
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[base]
name=CentOS-$releasever – Base – mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/os/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/os/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever – Updates – mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/updates/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/updates/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever – Extras – mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/extras/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/extras/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras
gpgcheck=1
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever – Plus – mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/centosplus/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/centosplus/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus
gpgcheck=1
enabled=0
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7
#contrib – packages by Centos Users
[contrib]
name=CentOS-$releasever – Contrib – mirrors.yourdomain.com
failovermethod=priority
baseurl=http://mirrors.yourdomain.com/centos/$releasever/contrib/$basearch/
http://mirrors.yourdomain.com/centos/$releasever/contrib/$basearch/
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=contrib
gpgcheck=1
enabled=0
gpgkey=http://mirrors.yourdomain.com/centos/RPM-GPG-KEY-CentOS-7

part.5 配置hosts

?
1
2
3
4
5
6
$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1   localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.193 mirrors.yourdomain.com
# 确保A 主机IP 和后面的反向代理地址

part.6 配置iptables

?
1
2
ping mirrors.yourdomain.com
#报错 没有到主机的路由

此时查看B主机中的iptables信息,发现无法访问80,可以在最前添加一条规则。

?
1
2
3
4
5
6
7
8
9
$ iptables -nvL
8155 28M ACCEPT  all — *  *  0.0.0.0/0   0.0.0.0/0   ctstate RELATED,ESTABLISHED
0  0 ACCEPT  all — lo  *  0.0.0.0/0   0.0.0.0/0  
11761 985K INPUT_direct all — *  *  0.0.0.0/0   0.0.0.0/0  
11761 985K INPUT_ZONES_SOURCE all — *  *  0.0.0.0/0   0.0.0.0/0  
11761 985K INPUT_ZONES all — *  *  0.0.0.0/0   0.0.0.0/0  
0  0 DROP  all — *  *  0.0.0.0/0   0.0.0.0/0   ctstate INVALID
11756 985K REJECT  all — *  *  0.0.0.0/0   0.0.0.0/0   reject-with icmp-host-prohibited
?
1
$ iptables -I INPUT -p tcp –dport 80 -j ACCEPT

part.7 测试是否成功

在B主机中进行,yum makecache操作。来判断是否能进行yum操作。

?
1
2
$ yum clean all
$ yum makecache

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/143945dd811d

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:Nginx实现动静分离的示例代码

2023-4-11 1:14:07

建站知识

小知识:负载均衡的基本知识以及使用nginx进行负载均衡的简单例子

2023-4-11 1:32:42

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索