Forum: NGINX Wonderful one-liners in configuration

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
sbwoodside (Guest)
on 2009-04-30 09:27
(Received via mailing list)
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;
      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:
http://forum.nginx.org/read.php?2,1579,1579#msg-1579
Igor S. (Guest)
on 2009-04-30 09:44
(Received via mailing list)
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.
sbwoodside (Guest)
on 2009-05-16 23:40
(Received via mailing list)
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;
  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;
    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:
http://forum.nginx.org/read.php?2,1579,2084#msg-2084
Michael S. (Guest)
on 2009-05-16 23:50
(Received via mailing list)
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...
Igor S. (Guest)
on 2009-05-17 02:00
(Received via mailing list)
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;
   }
usen68 (Guest)
on 2009-05-19 15:56
(Received via mailing list)
Elder brother, this is not a line!

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,1579,2179#msg-2179
nickychen (Guest)
on 2010-08-14 04:25
(Received via mailing list)
哥是来看楼上的回复的

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,1579,119687#msg-119687
This topic is locked and can not be replied to.