Skip to content

Instantly share code, notes, and snippets.

@cerico
Last active November 19, 2019 20:41
Show Gist options
  • Save cerico/72322b2177b60b9f063b7086a6b76344 to your computer and use it in GitHub Desktop.
Save cerico/72322b2177b60b9f063b7086a6b76344 to your computer and use it in GitHub Desktop.
Set up Nginx/Dnsmasq to run your sites when ip changes
server {
listen 80;
server_name www.hello.test hello.test;
location / {
add_header Content-Type text/plain;
return 200 "
╭────────────────────────────────────────────────────────────╮
│ │
│ 🌊🌊🌊 nginx reporting for duty 🌊🌊🌊 │
│ │
╰────────────────────────────────────────────────────────────╯
";
}
}
1) Install nginx and dnsmasq
```
brew install nginx dnsmasq
```
2) Make sure following lines are in /usr/local/etc/dnsmasq.conf
```# Add alternate DNS servers
server=208.67.222.222
server=208.67.220.220
conf-dir=/usr/local/etc/dnsmasq.d,*.conf
```
3) Create the test file in /etc/resolver and change the permissions
```sudo mkdir /etc/resolver
sudo touch /etc/resolver/test
sudo chmod a+w /etc/resolver/test
```
4) Create the hello-test.conf file above
```
mkdir /usr/local/etc/nginx/servers
cat /usr/local/etc/nginx/servers/hello-test.conf
```
5) Start the services
```
sudo brew services start nginx
sudo brew services start dnsmasq
```
6) Run the setup-sites.sh above
```
chmod a+x ~/dotfiles/setup-sites.sh
~/dotfiles/setup-sites.sh
```
7) Test it out
```
curl http://hello.test
```
8) Add start up script to System Preferences / Users & Groups / Login items
```
osascript -e 'tell application "System Events" to make login item at end with properties {path:"/Users/gareth/zfiles/scripts/lol.sh", hidden:false}'
```
#!/bin/bash
#add this to your startup scripts via System Preferences / Users & Groups / Login items
ip=$(ifconfig | grep en0 -A99 | grep -w inet -m1 | awk -F' ' '{print $2}')
echo "ip is now $ip"
echo "nameserver $ip" > /etc/resolver/test
echo "address=/.test/$ip" > /usr/local/etc/dnsmasq.d/development.conf
sudo brew services restart dnsmasq
curl http://local.test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment