D
D
Daniel Newman2013-01-23 00:14:02
Nginx
Daniel Newman, 2013-01-23 00:14:02

Where to look for beautiful nginx configs or how to make mine beautiful?

Dear, how to solve the problems of logging and beautiful organization of nginx configs, when you want to log addresses both with the www/ftp/mysql… prefix and without it into one file in one folder for a given set of hosts?

upstream vserver_pool {
    server 100.100.100.100:80;
}

root              /var/log/nginx/;
access_log        /var/log/nginx/$host-access.log;
error_log         /var/log/nginx/error.log;

server {
    listen        80;
    server_name   www.domain1.tld;

    return        301 http://domain1.tld$request_uri;

    root          /var/log/nginx/$host/;
    access_log    /var/log/nginx/$host/$host-access.log;
}

..........................................

server {
    listen        80;
    server_name   www.domain100500.tld;

    return        301 http://domain100500.tld$request_uri;

    root          /var/log/nginx/$host/;
    access_log    /var/log/nginx/$host/$host-access.log;
}


The problem of a beautiful organization of configs pops up when there is a need to add a set of identical proxy rules to each host. 10 times add an identical piece:
server {
    listen        80 default_server;

    location ~ /\.ht {
        deny all;
    }

    location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location / {
        proxy_pass  http://vserver_pool;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_buffering off;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}


And 5 times - not identical. There is a problem with the hierarchy. I figured that a special case of the config would take precedence, however, all I achieve is duplication of logs, in case there is a www prefix in the hostname.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
VBart, 2013-01-24
@VBart

nginx configs are not a programming language. It is not necessary to apply programming dogmas to it, that duplication is bad, etc. In fact, a static config without variables will work much faster than yours. Just adding the $host variable to the access_log already adds two extra system calls per request. Variables in nginx are needed not for “templating”, but for solving really dynamic configuration tasks.
ps
mailman.nginx.org/pipermail/nginx-ru/2011-November/044461.html

L
la0, 2013-01-23
@la0

I don't remember exactly, but logically it should work:
I'm not sure if it's possible to set in the server context, but try it.
But: you need to use named locations, then the config will definitely become more beautiful.

set $h site1.ru;
include defaults-server
set $h site2.ru;
include defaults-server

----
defaults-server
server {
  server_name $h www.$h
  include locations;
  location / {return 406;}

}
---
locations

error_page 406 @bckend;
location @bckend{
  proxy_pass ...
 ....
}

P
Pavel Zagrebelin, 2013-01-23
@Zagrebelion

nginx can use wildcards and regexps in server_name.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question