title: 配置nginx代理yum源及DNS #标题tags: 配置代理yum源 #标签
date: 2020-05-16
categories: nginx # 分类

需求:内网环境,需要yum安装很多东西,故需要一台可以连接外网的nginx作为代理,内网主机通过nginx代理yum安装包。

代理yum源

环境描述

两台主机,192.168.20.3为内网主机,无法连接外网,需要连接外网进行yum安装。
192.168.20.2为nginx代理,可以连接外网,并可以和内网主机通信。

修改内网主机的yum配置文件

  1. $ tail -1 /etc/yum.conf # 修改yum配置文件,添加该行,指定nginx监听地址
  2. proxy=http://192.168.20.2:1102
  3. # 注,这里仅支持支持端口,而不支持这样的路径匹配,如:http://192.168.20.2:1102/repo
  4. # 备份原有.repo文件
  5. $ mkdir /etc/yum.repos.d/bak
  6. $ mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/
  7. $ ls /etc/yum.repos.d/bak/ # 确认文件已备份
  8. CentOS-Base.repo CentOS-fasttrack.repo CentOS-Vault.repo
  9. CentOS-CR.repo CentOS-Media.repo epel.repo
  10. CentOS-Debuginfo.repo CentOS-Sources.repo
  11. # 编写新的repo文件如下
  12. $ cat /etc/yum.repos.d/Centos-7.repo
  13. # CentOS-Base.repo
  14. #
  15. # The mirror system uses the connecting IP address of the client and the
  16. # update status of each mirror to pick mirrors that are updated to and
  17. # geographically close to the client. You should use this for CentOS updates
  18. # unless you are manually picking other mirrors.
  19. #
  20. # If the mirrorlist= does not work for you, as a fall back you can try the
  21. # remarked out baseurl= line instead.
  22. #
  23. #
  24. [base]
  25. name=CentOS-$releasever - Base - mirrors.aliyun.com
  26. failovermethod=priority
  27. baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
  28. http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
  29. http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
  30. gpgcheck=1
  31. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  32. #released updates
  33. [updates]
  34. name=CentOS-$releasever - Updates - mirrors.aliyun.com
  35. failovermethod=priority
  36. baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
  37. http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
  38. http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
  39. gpgcheck=1
  40. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  41. #additional packages that may be useful
  42. [extras]
  43. name=CentOS-$releasever - Extras - mirrors.aliyun.com
  44. failovermethod=priority
  45. baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
  46. http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
  47. http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
  48. gpgcheck=1
  49. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  50. #additional packages that extend functionality of existing packages
  51. [centosplus]
  52. name=CentOS-$releasever - Plus - mirrors.aliyun.com
  53. failovermethod=priority
  54. baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
  55. http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
  56. http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
  57. gpgcheck=1
  58. enabled=0
  59. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  60. #contrib - packages by Centos Users
  61. [contrib]
  62. name=CentOS-$releasever - Contrib - mirrors.aliyun.com
  63. failovermethod=priority
  64. baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
  65. http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
  66. http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
  67. gpgcheck=1
  68. enabled=0
  69. gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
  70. #[epel]
  71. #name=Extra Packages for Enterprise Linux 7 - $basearch
  72. #baseurl=http://mirrors.aliyun.com/epel/7/$basearch
  73. #failovermethod=priority
  74. #enabled=1
  75. #gpgcheck=0
  76. #gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  77. [epel-debuginfo]
  78. name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
  79. baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
  80. failovermethod=priority
  81. enabled=0
  82. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  83. gpgcheck=0
  84. [epel-source]
  85. name=Extra Packages for Enterprise Linux 7 - $basearch - Source
  86. baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
  87. failovermethod=priority
  88. enabled=0
  89. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
  90. gpgcheck=0

配置nginx代理

修改配置文件

192.168.20.2为nginx代理主机,需修改配置文件。

  1. $ vim /etc/nginx/nginx.conf # 以下只是部分nginx配置文件
  2. server {
  3. listen 1102 default_server; # 修改监听端口和client端指定的一致
  4. location / {
  5. proxy_pass http://mirrors.ustc.edu.cn; # 指定此url
  6. }
  7. }

启动nginx
  1. $ nginx
  2. $ ss -lnpt | grep 1102 # 确定端口在监听
  3. LISTEN 0 128 *:1102 *:* users:(("nginx",pid=2413,fd=6),("nginx",pid=2412,fd=6))

至此,那个纯内网环境的主机即可通过yum来进行安装了。

yum安装测试

  1. $ yum clean all
  2. $ yum makecache
  3. $ yum install nginx
  4. # 如果yum报错,可以尝试下面的指令
  5. $ yum clean all
  6. $ rpm --rebuilddb

代理DNS

nginx配置如下即可充当DNS:

  1. stream {
  2. server {
  3. listen 53 udp reuseport;
  4. proxy_timeout 20s;
  5. proxy_pass 114.114.114.114:53;
  6. }
  7. }

代理mysql等端口

  1. stream {
  2. server {
  3. listen 3306;
  4. proxy_timeout 20s;
  5. proxy_pass 192.168.20.2:3306;
  6. }
  7. }