I have the following conf file for nginx:
user www www; ## Default: nobody
...
events {
worker_connections 4096; ## Default: 1024
}
http {
include conf/mime.types;
...
server { # php/fastcgi
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025;
}
}
server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
}
# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
proxy_pass http://127.0.0.1:8080;
}
}
}
This is a foo
example that I found on www.nginx.com.
The goal is to extract all the servers' blocks for various treatments. In this particular case, there are two servers' blocks that I am interested in.
Because I work on very limited environments, I can only use sed/grep/awk/unix systems commands. No python, no pearl, ...
The problem with that kind of configuration files is that it is maybe possible that the server
block itself contains some sub-blocks (i.e server { ... directive { ... }}
). Considering that, it is impossible to simply use grep -oP "server {.*?}"
.
Can grep
do the job? I've tried many different regex but didn't find the good one. For now, I worked with the above file without backslash (i.e cat $FILE | tr -d "\n"
). I've tried things like grep -oP "server\s{1,}{.*?({.*?}){0,}}"
but it does not fit my needs...
The reason I want to use grep
is that I think that awk
is not very readable afterward, and to maintain the code should be easier with grep
- but I could make an exception if it is way easier with awk
!
Thanks :)
EDIT:
Output should looks like:
"server { # php/fastcgi
listen 80;
server_name domain1.com www.domain1.com;
access_log logs/domain1.access.log main;
root html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:1025;
}
}",
"server { # simple reverse-proxy
listen 80;
server_name domain2.com www.domain2.com;
access_log logs/domain2.access.log main;
# serve static files
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
root /var/www/virtual/big.server.com/htdocs;
expires 30d;
}
# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
proxy_pass http://127.0.0.1:8080;
}
}"
Something that I can treat using commands like echo $OUTPUT | tr -d ... | grep -v ...
. If possible, I want to have each server block extracted on a single line to loop on the result!