dnsmasq+nginx实现地址转发

一、部署环境:

  主机:192.168.1.155   

  部署方式:rancher2.0 手动发布

  部署包:dnsmasq   nginx   (docker镜像)

二、部署配置:

  在这里我们使用的是rancher2.0部署镜像,具体操作很简单,在这里就不再赘述了。

  dnsmasq和nginx部署完成后如下:

  1、dnsmasq

  我们可以进入容器去配置dnsmas.conf文件,也可以使用管理页面进行配置。在这里使用UI页面配置:

  配置详情:

 1 #dnsmasq config, for a complete example, see:
 2 #  http://oss.segetech.com/intra/srv/dnsmasq.conf
 3 #单设置127只为本机使用,加入本机IP为内部全网使用,默认监听主机所有网卡
 4 #如果是docker容器部署的,配置监听需要配置容器的虚拟ip
 5 #listen-address=127.0.0.1,10.42.0.23
 6 #log all dns queries;dnsmasq日志设置
 7 log-queries
 8 log-facility=/var/log/dnsmasq.log
 9 #dont use hosts nameservers
10 #no-resolv
11 resolv-file=/etc/dnsmasq.d/resolv.dnsmasq.conf     //在这里我们自定义了nameserver
12 #不读取系统hosts,读取你设定的
13 no-hosts
14 addn-hosts=/etc/dnsmasq.d/dnsmasq.hosts        //自定义了hosts
15 #重启后清空缓存
16 clear-on-reload
17 # 完整的域名才向上游服务器查找,如果仅仅是主机名仅查找hosts文件
18 #domain-needed
19 #dnsmasq缓存设置
20 cache-size=2048
21 # 严格按照resolv.conf中的顺序进行查找
22 #strict-order
23 # 也不要检测 /etc/resolv.conf 的变化
24 no-poll
25 #定义主机ip映射
26 address=/irms.boco.com/192.168.1.161
27 address=/wesf-shanxi.irms.boco.com/192.168.1.155

    由于我们在上面自定义了resolv和hosts,因此需要到定义的目录下创建对应的文件配置:

    vi  /etc/dnsmasq.d/resolv.dnsmasq.conf
1  nameserver 192.168.1.155    //或者容器的虚拟ip 
    vi /etc/dnsmasq.d/dnsmasq.hosts
1 192.168.1.161    irms.boco.com
2 192.168.1.155    websf-shanxi.irms.boco.com

    配置完成后restart。

  2、nginx

    vi /etc/nginx/nginx.conf

  1 # For more information on configuration, see:
  2 #   * Official English Documentation: http://nginx.org/en/docs/
  3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
  4 
  5 user nginx;
  6 worker_processes auto;
  7 error_log /var/log/nginx/error.log;
  8 pid /var/run/nginx.pid;
  9 
 10 # Load dynamic modules. See /usr/share/nginx/README.dynamic.
 11 include /usr/share/nginx/modules/*.conf;
 12 
 13 events {
 14     worker_connections 1024;
 15 }
 16 
 17 http {
 18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 19                       '$status $body_bytes_sent "$http_referer" '
 20                       '"$http_user_agent" "$http_x_forwarded_for"';
 21 
 22     access_log  /var/log/nginx/access.log  main;
 23 
 24     sendfile            on;
 25     tcp_nopush          on;
 26     tcp_nodelay         on;
 27     keepalive_timeout   65;
 28     types_hash_max_size 2048;
 29 
 30     include             /etc/nginx/mime.types;
 31     default_type        application/octet-stream;
 32 
 33     # Load modular configuration files from the /etc/nginx/conf.d directory.
 34     # See http://nginx.org/en/docs/ngx_core_module.html#include
 35     # for more information.
 36     include /etc/nginx/conf.d/*.conf;
 37 
 38 
 39 
 40     #shanxi
 41     server {
 42         listen       8088 default_server;
 43         listen       [::]:8088 default_server;
 44         server_name  _;
 45         root         /usr/share/nginx/html;
 46 
 47         # Load configuration files for the default server block.
 48         include /etc/nginx/default.d/*.conf;
 49         location /webdm {
 50             proxy_set_header Host $host:8088;
 51             proxy_pass http://irms.boco.com/webdm;
 52         }
 53         location /cad {
 54             proxy_pass http://cad.irms.boco.com/;
 55         }
 56         location /webisa {
 57             proxy_pass http://irms.boco.com/webisa;
 58         }
 59         location /webaddress {
 60             proxy_pass http://webaddress-shanxi.irms.boco.com/webaddress;
 61         }
 62         location /webddm {
 63             proxy_pass http://webddm-shanxi.irms.boco.com/webddm;
 64         }
 65         location /webpon {
 66             proxy_pass http://webpon-shanxi.irms.boco.com/webpon;
 67         }
 68         location /webrofhflow {
 69             proxy_pass http://webrofhflow-shanxi.irms.boco.com/webrofhflow;
 70         }
 71         location /weblifecyc {
 72             proxy_pass http://weblifecyc-shanxi.irms.boco.com/weblifecyc;
 73         }
 74         location /webrms {
 75             proxy_pass http://webrms-shanxi.irms.boco.com/webrms;
 76         }
 77         location /websf {
 78             proxy_set_header Host $host:8088;    
 79             proxy_pass http://websf-shanxi.irms.boco.com/websf;
 80         }
 81         location /dps {
 82             proxy_pass http://irms.boco.com/dps;
 83         }
 84         location /webdps {
 85             proxy_pass http://irms.boco.com/webdps;
 86         }
 87 
 88         location / {
 89             proxy_pass http://irms.boco.com/;
 90         }
 91 
 92         error_page 404 /404.html;
 93             location = /40x.html {
 94         }
 95 
 96         error_page 500 502 503 504 /50x.html;
 97             location = /50x.html {
 98         }
 99     }
100 }

    配置nginx所在主机的resolv.conf文件(或者是nginx所在容器内)为dnsmasq主机(或容器)的ip。

三、验证结果

 

  域名解析正常,验证成功。

  

原文地址:https://www.cnblogs.com/cooper-73/p/10770185.html