Skip to content

Instantly share code, notes, and snippets.

@Locoxella
Created July 16, 2018 21:26
Show Gist options
  • Save Locoxella/1a8926a5006a6969d1ecddeed94bffa4 to your computer and use it in GitHub Desktop.
Save Locoxella/1a8926a5006a6969d1ecddeed94bffa4 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. Not compatible with regex.
#!/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