# Wonderful one-liners in configuration

**URL:** <https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853>\
**Category:** NGINX\
**Created:** [April 30, 2009, 7:27am UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853 "2009-04-30T07:27:56Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [April 30, 2009, 7:27am UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/1 "2009-04-30T07:27:56Z")

</div>

I just love nginx, just adopted it today, and wanted to show my  
appreciation by sharing something I really like but haven’t seen in any  
of the configuration examples: collapsing multiple-line config blocks  
into one line. For example here is my config for one of my sites. Look  
how nice and short it is 🙂

upstream mongrel-hub { server 127.0.0.1:11000; server 127.0.0.1:11001;  
server 127.0.0.1:11002; }  
server {  
listen 80;  
server\_name xxx;  
root /var/www/deployed-hub/current/public;  
error\_page 500 502 503 504 /50x.html; location = /50x.html { root  
/var/www/nginx-default; }  
if (-f $document\_root/system/maintenance.html) { rewrite ^(._)$  
/system/maintenance.html last; break; }  
location / {  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header -Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
proxy\_redirect false;  
if (-f $request\_filename) { rewrite (._) $1 break; }  
if (-f $request\_filename/index.html) { rewrite (._) $1/index.html  
break; }  
if (-f $request\_filename.html) { rewrite (._) $1.html break; }  
if (!-f $request\_filename) {  
proxy\_pass [http://mongrel-hub](http://mongrel-hub);  
break;  
}  
}  
}

I’ve compressed blocks that I’ll never need to think about again (and  
stole from other rails sites anyway) and keep things nice and clear.  
Thanks!

Posted at Nginx Forum:

> **[Wonderful one-liners in configuration](https://forum.nginx.org/read.php?2,1579,1579#msg-1579)**
>
> I just love nginx, just adopted it today, and wanted to show my appreciation by sharing something I really like but haven't seen in any of the configuration examples: collapsing multiple-line config blocks into one line. For example here is my config...

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [April 30, 2009, 7:44am UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/2 "2009-04-30T07:44:19Z")

</div>

On Thu, Apr 30, 2009 at 01:19:23AM -0400, sbwoodside wrote:

> I just love nginx, just adopted it today, and wanted to show my appreciation by sharing something I really like but haven’t seen in any of the configuration examples: collapsing multiple-line config blocks into one line. For example here is my config for one of my sites. Look how nice and short it is 🙂
> 
> upstream mongrel-hub { server 127.0.0.1:11000; server 127.0.0.1:11001; server 127.0.0.1:11002; }

I prefer to not use one-liner in blocks as above since it does not allow  
to  
comment out some servers.

> ```
> proxy_set_header Host $http_host;
> proxy_redirect false;
> 
> ```

It should be proxy\_redirect “off”.

> ```
> if (-f $request_filename) { rewrite (.*) $1 break; }
> if (-f $request_filename/index.html) { rewrite (.*) $1/index.html break; }
> if (-f $request_filename.html) { rewrite (.*) $1.html break; }
> if (!-f $request_filename) {
> proxy_pass http://mongrel-hub;
> break;
> }
> 
> ```

There is more nice one-liner for the above “if/rewrite” ugly stuff:

```
  location / {
      try_files $uri $uri/index.html $uri.html @mongrel;
  }

  location @mongrel {
       proxy_pass http://mongrel-hub;
       proxy_set_header ...
  }

```

> }  
> }
> 
> I’ve compressed blocks that I’ll never need to think about again (and stole from other rails sites anyway) and keep things nice and clear. Thanks!

This is called write only configuration. Everyone prefers to think that  
he will never need to think about something again. It’s almost always  
wrong.

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [May 16, 2009, 9:40pm UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/3 "2009-05-16T21:40:06Z")

</div>

I prefer to not use one-liner in blocks as above since it does not allow  
to  
comment out some servers.

Yes… in this case I’ll never be commenting out just one that I can  
think of, because it’s a cluster started by cap.

location / {  
try\_files $uri $uri/index.html $uri.html @mongrel;  
}

Nice! I’ve installed nginx from source and switched to this. Here’s my  
config now (for my redmine instalation)

upstream mongrel-redmine { server 127.0.0.1:9500; server 127.0.0.1:9501;  
server 127.0.0.1:9502; }  
server {  
listen 443;  
server\_name [example.com](http://example.com);  
ssl on;  
ssl\_certificate /etc/nginx/certs/myssl.crt; ssl\_certificate\_key  
/etc/nginx/certs/myssl.key;  
root /var/www/deployed-redmine/current/public;  
error\_page 500 502 503 504 /50x.html; location = /50x.html { root  
/var/www/nginx-default; }  
if (-f $document\_root/system/maintenance.html) { rewrite ^(.\*)$  
/system/maintenance.html last; break; }  
location / {  
try\_files $uri $uri/index.html $uri.html @mongrel;  
}  
location @mongrel {  
proxy\_pass [http://mongrel-redmine](http://mongrel-redmine);  
proxy\_set\_header X-FORWARDED\_PROTO https;  
proxy\_set\_header X-Real-IP $remote\_addr;  
proxy\_set\_header X-Forwarded-For $proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Host $http\_host;  
proxy\_redirect off;  
}  
}

Posted at Nginx Forum:

> **[Re: Wonderful one-liners in configuration](https://forum.nginx.org/read.php?2,1579,2084#msg-2084)**
>
> I just love nginx, just adopted it today, and wanted to show my appreciation by sharing something I really like but haven't seen in any of the configuration examples: collapsing multiple-line config blocks into one line. For example here is my config...

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [May 16, 2009, 9:50pm UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/4 "2009-05-16T21:50:40Z")

</div>

cool, we’ll be running redmine soon too, hopefully.

however we have to run a perl script inside of nginx before passing  
off to redmine (it decrypts our authentication cookie) which hopefully  
will be supportable…

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [May 17, 2009, 12:00am UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/5 "2009-05-17T00:00:43Z")

</div>

On Fri, May 15, 2009 at 10:27:02PM -0400, sbwoodside wrote:

> location / {  
> try\_files $uri $uri/index.html $uri.html @mongrel;  
> }

This

if (-f $document\_root/system/maintenance.html) {  
rewrite ^(.\*)$ /system/maintenance.html last; break;  
}  
location / {  
try\_files $uri $uri/index.html $uri.html @mongrel;  
}

should be replaced by

location / {  
try\_files /system/maintenance.html $uri $uri/index.html $uri.html  
@mongrel;  
}

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [May 19, 2009, 1:56pm UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/6 "2009-05-19T13:56:07Z")

</div>

Elder brother, this is not a line!

Posted at Nginx Forum:

> **[Re: Wonderful one-liners in configuration](https://forum.nginx.org/read.php?2,1579,2179#msg-2179)**
>
> I just love nginx, just adopted it today, and wanted to show my appreciation by sharing something I really like but haven't seen in any of the configuration examples: collapsing multiple-line config blocks into one line. For example here is my config...

---

<div class="post-metadata">

**Author:** ![sbwoodside](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ac91a4/32.png) [@sbwoodside](https://www.ruby-forum.com/u/sbwoodside)\
**Post date:** [August 14, 2010, 2:25am UTC](https://www.ruby-forum.com/t/wonderful-one-liners-in-configuration/166853/7 "2010-08-14T02:25:19Z")

</div>

å“¥æ˜¯æ¥çœ‹æ¥¼ä¸Šçš„å›žå¤çš„

Posted at Nginx Forum:

> **[Re: Wonderful one-liners in configuration](https://forum.nginx.org/read.php?2,1579,119687#msg-119687)**
>
> I just love nginx, just adopted it today, and wanted to show my appreciation by sharing something I really like but haven't seen in any of the configuration examples: collapsing multiple-line config blocks into one line. For example here is my config...
