Mac下使用dnsmasq+nginx進行本地開發
- 前言
- 安裝dnsmasq
- 配置dnsmasq
- 配置macOS
- 測試
- nginx
前言
最近剛剛開始使用Mac進行開發,有點不熟練,借着部署環境分享下使用Dnsmasq進行域名管理。大多數web開發者可能都是使用hosts 或者 端口号進行項目通路,這就有以下問題:
- 需要你在添加項目或者删除項目時每次對配置檔案進行修改
- 需要管理者權限
DNSmasq是一個小巧且友善地用于配置DNS和DHCP的工具,适用于小型網絡,它提供了DNS功能和可選擇的DHCP功能。它服務那些隻在本地适用的域名,這些域名是不會在全球的DNS伺服器中出現的。DHCP伺服器和DNS伺服器結合,并且允許DHCP配置設定的位址能在DNS中正常解析,而這些DHCP配置設定的位址和相關指令可以配置到每台主機中,也可以配置到一台核心裝置中(比如路由器),DNSmasq支援靜态和動态兩種DHCP配置方式。(來自百度百科)
安裝dnsmasq
我的開發環境是PHP7.2 mysql5.7 nginx, 這些基礎環境的部署這裡就不進行說明了。
Mac下的dnsmasq還是很有很多安裝方式的,因為剛剛接觸我目前熟練的隻有HomeBrew。
安裝方法:
#安裝
brew install dnsmasq
建議使用brew services管理服務
官網:https://github.com/Homebrew/homebrew-services
配置dnsmasq
使用brew安裝的軟體預設路徑在/etc/local 目錄下,其中配置檔案在/etc/local/etc目錄中:

Dnsmasq 可以做的很多事情之一是将 DNS 請求與模式資料庫進行比較,并以此來确定正确的應答。我使用這個功能來比對以 .devel 結尾的任何請求,并發送 127.0.0.1 作為應答。Dnsmasq 配置指令非常容易,打開dnsmasq.conf配置,修改:
address=/devel/127.0.0.1
詳細配置可參考:https://cloud.tencent.com/developer/article/1174717
啟動dnsmasq
brew services start dnsmasq
可檢視服務:
配置macOS
現在你已經有了一個可以工作的 DNS 伺服器,你可以在自己的作業系統上配置來使用它。有使用兩種方法:
- 發送所有 DNS 請求到 Dnsmasq
- 隻發送 .devel 的請求到 Dnsmasq
第一種方法非常簡單,隻要在系統偏好中改變你的 DNS 設定——但是可能在 Dnsmasq 配置檔案不添加額外的修改的時候并不會生效。
第二種方法顯得有點微妙,但并沒有非常。大多數類 Unix 的作業系統有叫做 /etc/resolv.conf 的配置檔案,用以控制 DNS 查詢的執行方式,包括用于 DNS 查詢的預設伺服器(這是連接配接到網絡或者在系統偏好中修改 DNS 伺服器時自動設定的)。
macOS 也允許你通過在 /etc/resolver 檔案夾中建立新的配置檔案來配置額外的解析器。這個目錄可能還不存在于你的系統中,是以你的第一步應該是建立它:
sudo mkdir /etc/resolver
在此目錄建立devel檔案,并寫人nameserver 127.0.0.1
在這裡,devel 是我配置 Dnsmasq 來響應的頂級域名,127.0.0.1 是要使用的伺服器的 IP 位址。
一旦你建立了這個檔案,macOS 将會自動讀取并完成。
ps: 目前現在隻發現配置在/etc/resolver下可以,沒搞懂配置在/etc/resolv.conf為什麼沒生效?
測試
至此,你ping任何以.devel結尾的域名就會解析到本地,無論位址是否存在:
nginx
當然直到目前為止感覺沒有太大作用,dnsmasq隻是作為dns解析,其他就需要借助nginx來完成了。
nginx.conf配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include servers/*;
}
servers/dynamic.conf配置
server {
set $basepath "/Users/dev/Sites"; #網站目錄
set $rootpath "";
set $domain $host;
set $servername "localhost";
## check one name domain for simple application
if ($domain ~ "^(.[^.]*)\.(devel)$") { #一級域名解析
set $domain $1;
set $cname $2;
set $rootpath "${domain}";
set $servername "${domain}.${cname}";
}
##二級域名解析 略
listen 80;
server_name $servername;
root $basepath/$rootpath;
index index.html index.htm index.php;
access_log "/usr/local/var/log/nginx/access-${servername}.log";
error_log "/usr/local/var/log/nginx/error-${servername}.log";
include cors;
location / {
try_files $uri $uri/ /index.html /index.php?$args;
autoindex on;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_buffers 4 64k;
fastcgi_buffer_size 64k;
fastcgi_connect_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;
}
location = /favicon.ico {
try_files $uri =204;
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location ~ /\.(ht|git) {
deny all;
}
}
至此你隻需在你的網站根目錄建立項目檔案夾,如demo,然後在浏覽器上通路http://demo.devel 即可通路該項目。
ps:
- 因為頂級域名devel 不存在,是以通路記得加上http:// 。
- 使用brew啟動dnsmasq時,一定要使用root權限啟動
pps: 第一次寫文章,寫的很爛,見諒!!!