Opened 10 years ago

Closed 10 years ago

#532 closed enhancement (wontfix)

Make Nginx configs easy to understand and shorted

Reported by: Andre Fish Owned by:
Priority: major Milestone:
Component: nginx-core Version: 1.2.x
Keywords: Cc:
uname -a: Linux de.domain.net 2.6.32-042stab083.2 #1 SMP Fri Nov 8 18:08:40 MSK 2013 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.2.1
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-log-path=/var/log/nginx/access.log --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --lock-path=/var/lock/nginx.lock --pid-path=/var/run/nginx.pid --with-pcre-jit --with-debug --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-http_xslt_module --with-ipv6 --with-sha1=/usr/include/openssl --with-md5=/usr/include/openssl --with-mail --with-mail_ssl_module --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-auth-pam --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-echo --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-upstream-fair --add-module=/tmp/buildd/nginx-1.2.1/debian/modules/nginx-dav-ext-module

Description

The Nginx way of using config files is very long and means useless things have to be pasted twice. It's also impossible to define a default host.

For example, if i want to add a vhost my defualt site-enabled config would be this, and I would have to add a massive server block for each vhost...

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
        root /var/www/de.domain.net;
        index index.php index.html index.htm;
        server_name _;
        location / {
                try_files $uri $uri/ /index.html;
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
                location ~ /\.ht {
                deny all;
        }
}

server {
        root /var/www/linuxthefish.net;
        server_name domain.net;
        index index.php index.html index.htm;
        location / {
                try_files $uri $uri/ /index.html;
        }
        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }
                location ~ /\.ht {
                deny all;
        }


}

Whereas something like lighttpd you can define where PHP and index files are in the first bit of a lighttpd config, making it a lot easier to manage lots of websites:

fastcgi.server += ( ".php" =>
        ((
                "bin-path" => "/usr/bin/php-cgi",
                "socket" => "/var/run/lighttpd/php.socket",
                "max-procs" => 1,
                "bin-environment" => (
                        "PHP_FCGI_CHILDREN" => "10",
                        "PHP_FCGI_MAX_REQUESTS" => "10000"
                ),
                "bin-copy-environment" => (
                        "PATH", "SHELL", "USER"
                ),
                "broken-scriptfilename" => "enable"
        ))
)


$HTTP["host"] =~ "(^|\.)domain\.net$" {
server.document-root = "/var/www/domain.net"
}

$HTTP["host"] =~ "(^|\.)domain2\.co.uk$" {
server.document-root = "/var/www/domain2.co.uk"
}

Change History (1)

comment:1 by Maxim Dounin, 10 years ago

Resolution: wontfix
Status: newclosed

In nginx, defaults can be defined at http{} level, e.g.:

http {
    ...
    index index.php index.html index.htm;
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    ...
}

What must be explicitly written is location handling. This is done intentionally to make sure each server's configuration is self-consistent and can be edited independently.

But if you really want identical handling for multiple vhosts it is possible to configure it, e.g.., by using the "include" dicrective. Create a "vhost.conf" file with needed content, and use something like this to configure individual virtual hosts:

server { server_name foo.example.com; root /path/to/foo; include vhost.conf; }
server { server_name bar.example.com; root /path/to/bar; include vhost.conf; }
Note: See TracTickets for help on using tickets.