Is there a command that will list all vhosts or servers running under nginx on CentOS? I would like to pipe the results to a text file for reporting purposes.
I'm looking for something similar to this command that I use for Apache:
apachectl -S 2>&1 | grep 'port 80'
The answers so far will work, except if you have
server_name
directives running over multiple lines, then it'll silently fail. They also seem to be written for human consumption (picking up extra lines likeserver_name_in_redirect off;
) so you can't include them in a script.I have lots of virtual hosts, and wanted to use the output in a script (sigh), so here's something which is a lot longer, but should be robust enough for that purpose:
Since it's long and
\
-y, I'll include a quick explanation of each line.\n
without a preceding;
,{
or}
with a space. This needs to use sed's weirdo:a;N;$!ba;
grab the whole file trick, and some grouping so that we can put the last character back with\1
, plus a bunch of extra backslashes for luck.server_name
line, with some extra checks to remove nginx variables ($foo
) and only include valid domains (ie notlocalhost
and_
).server_name
part.xargs -L1
to remove the single blank line at the top.Note that there are some bits in here which are technically doubling up, but I like to be as clear and robust as possible. Suggestions for improvement welcome, though.
@anthony-briggs is correct to point out that most other answers given here won't work if your
server_name ... ;
declarations are spread out across multiple lines.However, if you can afford the luxury of having a
perl
interpreter at your disposal, you can shorten his solution to this oneliner:In chunks:
-l
: Make theprint
command terminate its output with a newline;It also causes trailing newlines to be stripped from each input record as its read, but that doesn't bother us because we:
-n -0777
: Slurp all input into$_
in one go (one record);-e
: Simply executes the next argument as Perl source;Make the
print
command also output a newline between each of its arguments;Remove all comments;
Disregarding leading whitespace, comments are assumed to start and run through to the end of a line (
.
does not match a newline here);Produce everything between all
server_name ... ;
declarations (.
does match newlines here);... which are all
map
-ped to:... that will leave only the consecutive non-whitespace chunks;
... which are then
grep
-ped for uniqueness and not being the special "_
" server name,... and
print
-ed (unsorted).You can list the domains one line at a time using the following
Update: Thanks to @Putnik for pointing out an easier way (but I prefer only listing sites-enabled):
Old Post:
Try something like this:
Hope that helps!
grep server_name /etc/nginx/* -RiI
Imho much faster to type than @Haubix's answer. Add
|grep -v "#"
optionallyBeside all the other great solution, if you just need a simple list of all names one per line, like:
Parse the config. Get all lines with
server_name
. Removeserver_name
. Finally put one domain per line.