Skip to content

Instantly share code, notes, and snippets.

@gvarela
Last active September 26, 2017 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvarela/aa9f6af7bad8129198f9ec851a60421c to your computer and use it in GitHub Desktop.
Save gvarela/aa9f6af7bad8129198f9ec851a60421c to your computer and use it in GitHub Desktop.
.dev with nginx and dnsmasq
#!/bin/bash
rm .nginx.conf
while [[ $# > 1 ]]
do
key="$1"
case $key in
-p|--port)
PORT="$2"
shift
;;
-n|--name)
NAME="$2"
shift
;;
--public-path)
PUBLICPATH="$2"
shift
;;
*)
# unknown option
;;
esac
shift
done
if [[ -z "$PORT" ]]
then
PORT=5000
fi
if [[ -z "$PUBLICPATH" ]]
then
PUBLICPATH=`pwd`
fi
if [[ -z "$NAME" ]]
then
NAME=${PWD##*/}
fi
(
cat <<CONF
# WebSocket proxying
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream $NAME {
server 127.0.0.1:$PORT;
}
server {
listen 80;
server_name $NAME.dev;
root $PUBLICPATH/public;
client_max_body_size 100M;
try_files \$uri/index.html \$uri.html \$uri @app;
location @app {
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
# WebSocket proxying - from http://nginx.org/en/docs/http/websocket.html
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_redirect off;
proxy_pass http://$NAME;
}
}
CONF
) > .nginx.conf
ln -sf `pwd`/.nginx.conf /usr/local/etc/nginx/sites-enabled/$NAME.dev
sudo nginx -s reload
echo "Start your server on port $PORT and go to http://$NAME.dev"
brew install dnsmasq
sudo mkdir -p /etc/resolver
sudo tee /etc/resolver/dev >/dev/null <<EOF
nameserver 127.0.0.1
EOF
sudo tee /usr/local/etc/dnsmasq.conf >/dev/null <<EOF
bind-interfaces
keep-in-foreground
no-resolv
address=/dev/127.0.0.1
listen-address=127.0.0.1
EOF
brew services start dnsmasq
echo "ensure your webserver [nginx] is configured to listen on port 80"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment