Answer the question
In order to leave comments, you need to log in
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;
}
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;
}
}
Answer the question
In order to leave comments, you need to log in
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
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 ...
....
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question