Help to understand nginx configuration principle (general)

I guess I'm used to apache where you can choose to configure the document root globally. I thought I saw this being done with nginx in some example too but can't seem to find it now. Actually it looks like you set the document root multiple times in each server{} directive?

I'm confused. It doesn't make much intuitive sense to set a document root in a non global way (ie: multiple times). What am I missing?

Use case for me is to (hopefully) define the document root once as being at /srv/nginx/sites and to have index.html in /srv/nginx/sites/

With apache I would define the document root as /srv/apache/sites in /etc/apache2/apache2.conf with:

<Directory /srv/apache/sites> Options Indexes FollowSymLinks AllowOverride None Require all granted </Directory>

and then (in the sites directory) in a .conf file in sites-available using the

DocumentRoot directive (to point into the directory within the sites directory)

Then use a DirectoryIndex directive in a Directory block to point into the html directory in example.conf for the index file.

Isn't there a way to do the same with nginx?


PS: Apologies about the formatting. It shows it nice in the preview but once I click 'submit' button it puts it all inline. Can make the formatting stick.

7 Replies

If you're looking to serve multiple sites, each with its own root, based on server name - then in nginx this can be done something like this:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;


    root /srv/sites/;

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;


    root /srv/sites/;

Each server block having its own server_name and its own root.


So nginx eliminates global setting?

@Jake I believe it's possible to put root "globally", inside an http block, but …

… given your declared goal of serving multiple sites from same config, each site with its own root, a single root setting just won't do, isn't that true? Maybe I've missed something in your question, my apologies.


fwiw font decorations, anything in all caps, or with dashes is not meant as rudeness but for clarification (mine really) since I seem to be struggling so hard to word my question in a sensible way. Thanks for understanding; really, really thanks for the help.

2 issues are involved in the question..

  1. The location of the root for-each domain
  2. The location of the index.html within each unique root, for each domain.

I wanted to do something in a particular way but wasn't sure if my intended approach was (a) possible, (b) a very good idea or (c) would create a security issue.

For reference, purposes my directory structure looks like this right now..

$ pwd

$ tree ..
└── sites
    ├── bithouz
    │   ├── css
    │   │   ├── normalize.css
    │   │   └── stylesheet.css
    │   ├── html
    │   │   ├── bithouz.html
    │   │   └── develop.bithouz.html
    │   └── img
    │       └── under-construction-0.jpg
    └── jfines
        ├── css
        │   ├── normalize.css
        │   └── stylesheet.css
        ├── html
        │   ├── develop.jfines.html
        │   └── jfines.html
        └── img
            └── under-construction-0.jpg

On Ubuntu 18.04 server (my Linode's operating system) the nginx configuration files are located thus..

# Global config  

# Non-global config(s) -will be- at  

# And  

Hereafter referred to as global and non-global respectively and identified specifically as bithouz.conf or jfines.conf where needed.

In the global config file some-thing like..

root   /srv/nginx/sites;  
# ^ Intended to set the global root for all domains as the 'sites' directory but nothing more.

And then in the non-global config(s) some-thing like..

root   bithouz/;

# and

root   jfines/;

# ^ Intended to set each unique root for each domain in the 'sites' directory (where jfines and bithouz are meant to be those root(s).

# What (I hoped would be) happening here in the non-global configs is to point the root into each of /srv/nginx/sites/bithouz and /srv/nginx/sites/jfines in one short, last hop.

In regard to issue 2 - the one about the location of index.html

In each one of the non-global configs would be some-thing like

index  html/index.html;  
# ^ meant to look for the index html file in the 'html' directory inside bithouz and in jfines respectively

Hey, thanks if you can understand that and advise on it. I don't know nginx directives well enough to use them in my question so I hope the gist of it is clear enough. Really appreciate it.

Ah so you want the paths in the server blocks to be relative to a global path (which in turn is defined once outside any server blocks).

Nope doesn't work that way (afaik).

The index directive can however be defined once, globally.

# nginx.conf

index  index.html

and then

# bithouz.conf

server {
  root /srv/nginx/sites/bithouz/html;

# jfines.conf

server {
  root /srv/nginx/sites/jfines/html;


Ah so you want the paths in the server blocks to be relative to a global path (which in turn is defined once outside any server blocks).


Nope doesn't work that way (afaik).

Good to know - saves me time and frustration.

The index directive can however be defined once, globally.

Sounds pretty awesome - probably do that.

Reaally appreciate you taking the time to explain that to me man.

Have a great week. I'm sure we'll bump into one another again some time (I ain't goin' nowhere). :>


Please enter an answer

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct