Skip to content

Instantly share code, notes, and snippets.

@Locoxella
Created July 16, 2018 21:26
Show Gist options
  • Save Locoxella/12f43f55a72cf2b59777512e29fa8797 to your computer and use it in GitHub Desktop.
Save Locoxella/12f43f55a72cf2b59777512e29fa8797 to your computer and use it in GitHub Desktop.
Get every server_name from every enabled site on nginx (except default _), and add it to /etc/hosts on 127.0.0.1
#!/usr/bin/env bash
virtualhosts=$(awk 'BEGIN { ORS=" " }; /^\s*server_name/ { for ( i=2; i <= NF; i++ ) { serveralias[$i]++ } } END { for ( server in serveralias) { print server } }' /etc/nginx/sites-enabled/* | sed 's/\;//g' | sed 's/ _//g')
sed -i "/127\.0\.0\.1/ s/.*/127\.0\.0\.1 localhost $virtualhosts/g" /etc/hosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment