无法使用带 rails 的 dnsmasq 连接到本地服务器

Can not connect to local server using dnsmasq with rails

我有一个 rails 应用程序,我正尝试在 dnsmasq 的帮助下在本地环境中 运行。当我启动 rails 服务器时,它启动时没有显示任何错误:

[18973] Puma starting in cluster mode...
[18973] * Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
[18973] * Min threads: 10, max threads: 10
[18973] * Environment: development
[18973] * Process workers: 1
[18973] * Phased restart available
[18973] * Listening on tcp:0.0.0.0:3001
[18973] * Listening on ssl:0.0.0.0:3000 cert=config/ssl.crt&key=config/ssl.key&verify_mode=none
[18973] Use Ctrl-C to stop
[18973] * Starting control server on unix:///tmp/puma-status-1580448227758-18973
[18973] - Worker 0 (pid: 19010) booted, phase: 0

我确实启动了 dnsmasq 服务,这是它的状态

● dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
   Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2020-01-31 11:05:18 IST; 2s ago
  Process: 19529 ExecStop=/etc/init.d/dnsmasq systemd-stop-resolvconf (code=exited, status=0/SUCCESS)
  Process: 19605 ExecStartPost=/etc/init.d/dnsmasq systemd-start-resolvconf (code=exited, status=0/SUCCESS)
  Process: 19596 ExecStart=/etc/init.d/dnsmasq systemd-exec (code=exited, status=0/SUCCESS)
  Process: 19595 ExecStartPre=/usr/sbin/dnsmasq --test (code=exited, status=0/SUCCESS)
 Main PID: 19604 (dnsmasq)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/dnsmasq.service
           └─19604 /usr/sbin/dnsmasq -x /run/dnsmasq/dnsmasq.pid -u dnsmasq -r /run/dnsmasq/resolv.conf -7 /etc/dnsmasq.d,.dpkg-dist,.dpkg-old

Jan 31 11:05:18 BEAST dnsmasq[19595]: dnsmasq: syntax check OK.
Jan 31 11:05:18 BEAST dnsmasq[19604]: started, version 2.79 cachesize 150
Jan 31 11:05:18 BEAST dnsmasq[19604]: DNS service limited to local subnets
Jan 31 11:05:18 BEAST dnsmasq[19604]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC
Jan 31 11:05:18 BEAST dnsmasq[19604]: no servers found in /run/dnsmasq/resolv.conf, will retry
Jan 31 11:05:18 BEAST dnsmasq[19604]: read /etc/hosts - 7 addresses
Jan 31 11:05:18 BEAST dnsmasq[19604]: reading /run/dnsmasq/resolv.conf
Jan 31 11:05:18 BEAST dnsmasq[19604]: using nameserver 127.0.0.53#53
Jan 31 11:05:18 BEAST dnsmasq[19605]: /etc/resolvconf/update.d/libc: Warning: /etc/resolv.conf is not a symbolic link to /run/resolvconf/resol
Jan 31 11:05:18 BEAST systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.

我在位置 /etc/resolver/ 添加了开发文件,其内容为

file: /etc/resolver/dev
nameserver 127.0.0.1

我还在 /etc/dnsmasq.conf

位置的配置文件中添加了一行
address=/.dev/127.0.0.1

但是我无法从浏览器连接到服务器。

这是我的 /etc/resolv.conf 文件,而 dnsmasq 是 运行ning

nameserver 172.16.10.1
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 127.0.0.1

这是我在 /etc/dnsmasq.conf 文件中所做的更改,其余部分是默认设置

address=/.dev/127.0.0.1

netsatat -na | grep LISTEN 的输出,而我的 rails 服务器正在 运行ning 以及 dnsmasq

tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3000            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:3001            0.0.0.0:*               LISTEN     
tcp6       0      0 :::53                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN  

挖掘命令更新:

dig www.example.dev

的输出
; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> www.example.dev
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 18574
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;www.example.dev.       IN  A

;; AUTHORITY SECTION:
dev.            300 IN  SOA ns-tld1.charlestonroadregistry.com. cloud-dns-hostmaster.google.com. 1 21600 3600 259200 300

;; Query time: 321 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Feb 03 13:28:54 IST 2020
;; MSG SIZE  rcvd: 146

dig @127.0.0.1 www.example.dev

的输出
; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> @127.0.0.1 www.example.dev
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48569
;; flags: qr aa rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;www.example.dev.       IN  A

;; ANSWER SECTION:
www.example.dev.    0   IN  A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Mon Feb 03 13:29:50 IST 2020
;; MSG SIZE  rcvd: 53

dig example.dev

的输出
; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> example.dev
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 27586
;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;example.dev.       IN  A

;; AUTHORITY SECTION:
dev.            211 IN  SOA ns-tld1.charlestonroadregistry.com. cloud-dns-hostmaster.google.com. 1 21600 3600 259200 300

;; Query time: 271 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: Mon Feb 03 13:38:03 IST 2020
;; MSG SIZE  rcvd: 142

dig @127.0.0.1 example.dev

的输出
; <<>> DiG 9.11.3-1ubuntu1.11-Ubuntu <<>> @127.0.0.1 example.dev
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 36026
;; flags: qr aa rd ra ad; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;example.dev.       IN  A

;; ANSWER SECTION:
example.dev.    0   IN  A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.1#53(127.0.0.1)
;; WHEN: Mon Feb 03 13:38:09 IST 2020
;; MSG SIZE  rcvd: 49

如果我理解正确的话,您只有一台计算机(或虚拟机)带有开发环境 运行 Rails 应用程序和您的浏览器 window。所以你根本不需要dnsmasq,只需添加到你的/etc/hosts文件

127.0.0.1 www.example.dev

然后将浏览器指向 http://www.example.dev:3001https://www.example.dev:3000,因为您配置了两个端口(3000 用于 ssl,3001 用于非 ssl 访问)- 查看 puma 启动消息.

更新

如你所愿dnsmasq:

  1. 文件 /etc/resolver/dev 应该只有一行(那里没有冒号!)
nameserver 127.0.0.1
  1. 似乎 dnsmasq 绑定到 127.0.0.53(请参阅 dnsmasq 状态消息底部的第三行),而不是 127.0.0.1