391

I have worked with Apache before, so I am aware that the default public web root is typically /var/www/.

I recently started working with nginx, but I can't seem to find the default public web root.

Where can I find the default public web root for nginx?

user456584
  • 86,427
  • 15
  • 75
  • 107

33 Answers33

535

If installing on Ubuntu using apt-get, try /usr/share/nginx/www.

EDIT:

On more recent versions the path has changed to: /usr/share/nginx/html

2019 EDIT:

Might try in /var/www/html/index.nginx-debian.html too.

t56k
  • 6,769
  • 9
  • 52
  • 115
Lufix
  • 5,359
  • 2
  • 12
  • 2
193

If your configuration does not include a root /some/absolute/path; statement, or it includes one that uses a relative path like root some/relative/path;, then the resulting path depends on compile-time options.

Probably the only case that would allow you to make an educated guess as to what this means for you would be, if you downloaded and compiled the source yourself. In that case, the paths would be relative to whatever --prefix was used. If you didn't change it, it defaults to /usr/local/nginx. You can find the parameters nginx was compiled with via nginx -V, it lists --prefix as the first one.

Since the root directive defaults to html, this would, of course, result in /usr/local/nginx/html being the answer to your question.

However, if you installed nginx in any other way, all bets are off. Your distribution might use entirely different default paths. Learning to figure out what kind of defaults your distribution of choice uses for things is another task entirely.

Gnarfoz
  • 3,146
  • 1
  • 17
  • 15
  • 45
    You could maybe give a hint that you can use "nginx -V" (uppercase V) to discover the configuration arguments used to compile nginx. – kaufholdr Feb 14 '17 at 12:30
  • 1
    @kaufholdr is correct you will find the --prefix @ Gnarfoz mentioned looks similar to this --prefix=/usr/share/nginx – Dung May 08 '17 at 17:27
  • 7
    This also depends on which OS the installation is in, for Amazon Linux, the directory is: /usr/share/nginx/html – jeffmcneill Jun 12 '18 at 07:08
  • 3
    The --prefix path is not always correct. On my debian 9 nginx install the prefix path was "/var/www/html" but as it turns out the acutal default path was "/usr/share/nginx/html". Looking at default configuration file in /etc/nginx/sites-available/default showed the path to be the same as the --prefix path. But the configuration file located at /etc/nginx/conf.d/default.conf listed the root as /usr/share/nginx/html. The default.conf file takes precedent over the normal default file. Be sure to check both configuration files or you might be working with the wrong root. – Caveman Dec 24 '18 at 21:33
  • 3
    `nginx -V 2>&1 | grep --color -o -e '--prefix=[^[:space:]]\+'` gives you the nginx HOME. – sjas May 22 '19 at 11:47
87

The default Nginx directory on Debian is /var/www/nginx-default.

You can check the file: /etc/nginx/sites-enabled/default

and find

server {
        listen   80 default;
        server_name  localhost;

        access_log  /var/log/nginx/localhost.access.log;

        location / {
                root   /var/www/nginx-default;
                index  index.html index.htm;
        }

The root is the default location.

Maroun
  • 94,125
  • 30
  • 188
  • 241
52

'default public web root' can be found from nginx -V output:

nginx -V
nginx version: nginx/1.10.1
built with OpenSSL 1.0.2h  3 May 2016
TLS SNI support enabled
configure arguments: --prefix=/var/lib/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --pid-path=/run/nginx/nginx.pid --lock-path=/run/nginx/nginx.lock --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --user=nginx --group=nginx --with-ipv6 --with-file-aio --with-pcre-jit --with-http_dav_module --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-http_v2_module --with-http_auth_request_module --with-mail --with-mail_ssl_module

the --prefix value is the answer to the question. for the sample above the root is /var/lib/nginx

Serge S
  • 749
  • 7
  • 3
  • 1
    Yep, the best, and cross-platform. – Jesse Adelman Feb 03 '17 at 20:39
  • This is not always correct "--prefix" might be used on some systems but not all. I do agree nginx -V gives you a lot of info that will help track where the files are, but this depends on OS. I.E. on FreeBSD does are not in whatever "--prefix" is, but "/usr/local/www" and in some linux distros it might also deffer. Its best to look into the config. – Mikec Apr 28 '17 at 13:11
  • "--prefix" is the variable defined by GNU standard - see https://www.gnu.org/prep/standards/html_node/Directory-Variables.html. – Serge S May 10 '17 at 20:33
  • "--prefix" is the variable defined by GNU standard - see https://www.gnu.org/prep/standards/html_node/Directory-Variables.html. nginx follows the standards. If you don't define root or location in the configuration file(depends on --conf-path) the prefix will show where 'default public web root' located. And that does not depend on OS, though a package supplied with an OS distribution can also supply a configuration which redefine 'default public web root' in turn. – Serge S May 10 '17 at 20:55
  • so the algorithm: look for **--conf-path** in **nginx -V** output. if **location /** or **root** is not redefined in the config - use **--prefix** for 'default public web root' location. – Serge S May 10 '17 at 21:09
38

For Ubuntu and docker images:

 /usr/share/nginx/html/
DimiDak
  • 4,820
  • 2
  • 26
  • 32
  • Just curious , say I didn't know that , how would I found what is the right location ? There are many different answers to this out there – Royi Namir Oct 20 '18 at 10:52
  • As far as I remember I searched for "nginx" (something like find / -name nginx ) and found inside this directory(or in its subdirectory) the document that appeared in my browser. – DimiDak Feb 13 '19 at 13:42
25

On Mac OS X installing nginx with brew makes the default directory:

/usr/local/var/www

So:

root html

means

root /usr/local/var/www/html

There is no html directory so it would have to be created manually.

freegnu
  • 793
  • 7
  • 11
  • 1
    +1 for mentioning macOS and homebrew. However, I found that though `root html`, `index.html` and `50x.html` is put directly in `/usr/local/var/www`. Therefore, I doubt the exist of `html` folder. Could you give me some help with that? – iplus26 Aug 11 '16 at 18:10
  • 4
    "brew info nginx" Gives the docroot listed in the output. But specifying "root html" in a nginx config changes the docroot for that nginx config. – freegnu Aug 13 '16 at 19:09
  • 1
    For me ( MAC OS X El Capitan 10.11.5 ) and brew (1.1.5) , the directory was `/usr/local/Cellar/nginx/1.10.2_1/html` where 1.10.2_1 is nginx version. – Yogesh Yadav Dec 22 '16 at 21:33
  • 2
    On my brew install the Cellar directory is a symlink: `/usr/local/Cellar/nginx/1.12.0_1/html -> ../../../var/www/` and the `nginx -V` shows: `--prefix=/usr/local/Cellar/nginx/1.12.0_1` so `root html` is a redirected via that link. – Mark Edington Jul 24 '17 at 22:22
  • @iplus26 html directory already available /usr/local/Cellar/nginx/1.12.2_1/html, i think /usr/local/var/www just a link – Kanagavelu Sugumar Nov 30 '17 at 05:39
23

as most users here said, it is under this path:

/usr/share/nginx/html

This is the default path, but you can make yours though.

all you need is to create one in the web server root tree and give it some permissions "not 0777" and only for one user and visible to that user only, but the end of the path is visible to everyone since the end of the path is what your files and folders will be viewed by public.

for example, you can make one like this:

home_web/site1/public_html/www/

whenever you make a virtual host in Nginx you can customize your own root path, just add something like this in your server block:

 server {
    listen  80;
        server_name  yoursite.com;

root /home_web/site1/public_html/www/;
}
030
  • 10,842
  • 12
  • 78
  • 123
Digital site
  • 4,431
  • 12
  • 48
  • 72
  • Yep you are right. I just installed Nginx on my centos7 and I noticed that the default path is `/usr/share/nginx/html/` folder. Is there a reason to change that path at all (for security or any other reasons)? Is it okay to leave it in the default folder if I am going to be the only user and if I am only going to host 1 website in it? – Neel Oct 03 '14 at 12:37
  • Never mind my last question. I ended up creating separate folders for it since I thought its better this way just incase I do want to add different vhosts to this set-up in the future. Its easier this way. Cheers :) – Neel Oct 03 '14 at 17:08
  • this is not working for me, with abs path `/home/ubuntu/some/foo/bar/folder`, the name was copied from `pwd` where the `ls` shows `index.html`. – Pablo LION May 25 '22 at 17:41
22

You can simply map nginx's root folder to the location of your website:

nano /etc/nginx/sites-enabled/default

inside the default file, look for the root in the server tag and change your website's default folder, e.g. my websites are at /var/www

server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www; <-- Here!
...

When I was evaluating nginx, apache2 and lighttpd, I mapped all of them to my website sitting at /var/www. I found this the best way to evaluate efficiently.

Then you can start/stop the server of your choice and see which performs best.

e.g.

service apache2 stop
service nginx start

Btw, nginx actually is very fast!

Waqas
  • 3,763
  • 1
  • 30
  • 18
17

Look into nginx config file to be sure. This command greps for whatever is configured on your Machine:

cat /etc/nginx/sites-enabled/default |grep "root"

on my machine it was :root /usr/share/nginx/www;

Thamme Gowda
  • 11,249
  • 5
  • 50
  • 57
12

The default web folder for nginx depends on how you installed it, but normally it's in these locations:

/usr/local/nginx/html
/usr/nginx/html
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
11

Dump the configuration:

$ nginx -T
...
server {
    ...
    location / {
        root   /usr/share/nginx/html;
        ...
    }
    ...
}

What you get might be different since it depends on how your nginx was configured/installed.

References:

Update: There's some confusion on the issue of if/when the -T option was added to nginx. It was documented in the man page by vl-homutov on 2015 June 16, which became part of the v1.9.2 release. It's even mentioned in the release notes. The -T option has been present in every nginx release since, including the one available on Ubuntu 16.04.1 LTS:

root@23cc8e58640e:/# nginx -h    
nginx version: nginx/1.10.0 (Ubuntu)
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
  -?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -t            : test configuration and exit
  -T            : test configuration, dump it and exit
  -q            : suppress non-error messages during configuration testing
  -s signal     : send signal to a master process: stop, quit, reopen, reload
  -p prefix     : set prefix path (default: /usr/share/nginx/)
  -c filename   : set configuration file (default: /etc/nginx/nginx.conf)
  -g directives : set global directives out of configuration file
rubicks
  • 4,912
  • 1
  • 30
  • 41
11

Run the command nginx -V and look for the --prefix. Use that entry to locate your default paths.

Mark Chackerian
  • 21,866
  • 6
  • 108
  • 99
Jack
  • 111
  • 1
  • 2
9

On Mac install nginx with brew:

/usr/local/etc/nginx/nginx.conf

location / { 
    root   html;  # **means /usr/local/Cellar/nginx/1.8.0/html and it soft linked to /usr/local/var/www**
    index  index.html;
}  
JJD
  • 50,076
  • 60
  • 203
  • 339
user2413075
  • 91
  • 1
  • 3
9

For CentOS, Ubuntu and Fedora, the default directory is /usr/share/nginx/html

MucaP
  • 992
  • 1
  • 10
  • 18
7

In my case it was in /usr/share/nginx/html

you can try to find by performing a search

find / -name html
Eddie Martinez
  • 13,582
  • 13
  • 81
  • 106
7

For AWS EC2 Linux you will find here:

/usr/share/nginx
Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
Vivek Sharma
  • 577
  • 5
  • 9
5

Just to note that the default index page for the nginx server will also display the root location as well. From the nginx (1.4.3) on Amazon Linux AMI, you get the following:

This is the default index.html page that is distributed with nginx on the Amazon Linux AMI. It is located in /usr/share/nginx/html.

You should now put your content in a location of your choice and edit the root configuration directive in the nginx configuration file /etc/nginx/nginx.conf

jboles
  • 111
  • 2
  • 4
5

If you are on Ubuntu 14.04 you can find nginx www directory at following path:

yusuf@yusuf-he:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
yusuf@yusuf-he:/usr/share/nginx/html$
Yusuf Ibrahim
  • 1,591
  • 5
  • 21
  • 46
5

You can search for it, no matter where did they move it (system admin moved or newer version of nginx)

find / -name nginx

Dylan B
  • 796
  • 8
  • 16
5

*default pages web allocated in var/www/html *default configuration server etc/nginx/sites/avaliable/nginx.conf

server {

    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    index index.html index.php;

    server_name _;

    location /data/ {
        autoindex on;
    }

    location /Maxtor {
        root /media/odroid/;
        autoindex on;

    }

        # This option is important for using PHP.
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
    }
}

*default configuracion server etc/nginx/nginx.conf

content..

user www-data;
worker_processes 8;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}


#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
# 
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
#}

default access logs with ip clients var/log/nginx/...

user11793803
  • 59
  • 1
  • 1
4

you can access file config nginx,you can see root /path. in this default of nginx apache at /var/www/html

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
tomnyson
  • 57
  • 1
  • 6
4

If you need to find out nginx public root folder that was defined at compile time you can just check your access.log file.

  1. Open nginx.conf
  2. Find the log_format directive
  3. The value of the log_format is a template string that is used to write information to the access.log file. You can add $document_root variable to this template string to log default www root location to a file.

Here is an example from the http section of nginx.conf with modified log_format directive, $document_root is added at the beginning of the string:

    http {
        include       /etc/nginx/mime.types;
        default_type  application/octet-stream;

                  ## ADD $document_root HERE ##
        log_format  main  '$document_root $remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        etc. .......
  1. Then backup all configuration files *.conf in conf.d directory and create there configuration file, test.conf with the following lines:

    server{
        listen 80;
        server_name localhost;
    }
    
    1. Add following line to /etc/hosts file: 127.0.0.1 localhost

    2. Reload nginx configuration: nginx -s reload

    3. Send GET request to http://localhost: curl http://localhost

    4. Check the last string of access.log:tail -n 1 /var/log/nginx/access.log

Here is the sample output of this command, where /etc/nginx/html is the default document root defined at compile time :

    /etc/nginx/html 127.0.0.1 - - [15/Mar/2017:17:12:25 +0200] "GET / HTTP/1.1" 404 169 "-" "curl/7.35.0" "-"
G.Denis
  • 91
  • 3
  • it seem it works, but did not resolved me, A record for *.knexusgroup.com is on 54.235.222.119, http://w3.knexusgroup.com/ but it did not log for any domain .knexusgroup.com . It works where there is proper definition of root dir, I am not able to figure out what is issue, why I can not find root for any sub domain which is not matched to any ngiinx config. – Ramratan Gupta Oct 28 '17 at 03:38
3

My nginx on Ubuntu is "nginx version: nginx/1.9.12 (Ubuntu)" and root path is /var/www/html/

Ubuntu info is : No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 16.04 LTS Release: 16.04 Codename: xenial

Actually, if you just installed nginx on Ubuntu, then you can go to "/etc/nginx/sites-available" and check the default file, there is a configuration like "root /web/root/path/goes/here". And that is what you are looking for.

3

In Ubuntu, Nginx default root Directory location is /usr/share/nginx/html

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
3

You can find it in /var/www/ that is default directory for nginx and apache but you can change it. step 1 go to the following folder /etc/nginx/sites-available

step 2 edit default file in that you can find a server block under that there will be line named as root that is what defines the location.

gokul kandasamy
  • 193
  • 1
  • 2
  • 14
3

in ubuntu 19.04, we found it on

/usr/share/nginx/html

Arséne
  • 95
  • 4
2

For nginx/1.4.6 (Ubuntu)

/etc/nginx$ cat /etc/nginx/sites-available/default | grep -i root
- root /usr/share/nginx/html;
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
2

I also had this issue on Digital Ocean running a WordPress website with nginx.

My solution was to do the following:

  1. Modify the /etc/nginx/nginx.conf file with the following:
server {
root /var/www/html;
}

I then had to sudo service nginx restart

nginx -V command also shows you where your nginx config file is located as well (mine was pointed at /etc/nginx/nginx.conf)

John C
  • 21
  • 2
1

The default is related to the prefix option of the configure script when nginx is compiled; here's some strange sample from Debian:

% nginx -V | & tr ' ' "\n" | fgrep -e path -e prefix
--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

Subsequently, the default value of root is set to the html directory (as per the documentation of the root directive), which happens to be within prefix , as can be verified by looking at the $document_root variable from a simple configuration file:

# printf 'server{listen 4867;return 200 $document_root\\n;}\n' \
    >/etc/nginx/conf.d/so.10674867.conf

# nginx -s reload && curl localhost:4867
/etc/nginx/html

However, evil distributions like Debian seem to modify it quite a bit, to keep you extra entertained:

% fgrep -e root -e include /etc/nginx/nginx.conf
    include /etc/nginx/mime.types;
    #include /etc/nginx/naxsi_core.rules;
    #passenger_root /usr;
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;

% fgrep -e root -e include \
    /etc/nginx/conf.d/*.conf /etc/nginx/sites-enabled/*
/etc/nginx/conf.d/so.10674867.conf:server{listen 4867;return 200 $document_root\n;}
/etc/nginx/sites-enabled/default:   root /usr/share/nginx/www;
/etc/nginx/sites-enabled/default:       # include /etc/nginx/naxsi.rules
/etc/nginx/sites-enabled/default:   #   root /usr/share/nginx/www;
/etc/nginx/sites-enabled/default:   #   include fastcgi_params;
/etc/nginx/sites-enabled/default:   # deny access to .htaccess files, if Apache's document root
/etc/nginx/sites-enabled/default:#  root html;
/etc/nginx/sites-enabled/default:#  root html;

So, on this instance of Debian, you can see that the root is finally set to /usr/share/nginx/www.

But as you saw with the sample server configuration that would serve its $document_root value over http, configuring nginx is simple enough that you can write your own configuration in a matter of a single line or two, specifying the required root to meet your exact needs.

cnst
  • 25,870
  • 6
  • 90
  • 122
1

Alpine Linux does not have any default location at all. The file /etc/nginx/conf.d/default.conf says:

# Everything is a 404
location / {
    return 404;
}

# You may need this to prevent return 404 recursion.
location = /404.html {
    internal;
}

Replace those with a line like root /var/www/localhost/htdocs to point to the directory you want. Then sudo service nginx restart to restart.

Lassi
  • 3,522
  • 3
  • 22
  • 34
  • 1
    Not true. Just tried the latest `nginx:alpine` image and it serves content from `/usr/share/nginx/html` out of the box. – Jimbali Jul 31 '19 at 22:11
  • 1
    That sounds like a Docker image made by the nginx project that just happens to be using Alpine Linux as the underlying OS. When you install Alpine Linux as your main OS and do `apk add nginx` to manually install nginx from Alpine's own package manager, you get the situation described in my answer (or that was the case when I tried it a couple months ago anyway). – Lassi Aug 01 '19 at 12:52
1

navigate to file $ cat /etc/nginx/nginx.conf

inside the file under section http --> server --> root

here you will see the default location for root directory and all the error page.

ShamSuf
  • 925
  • 6
  • 9
0

In case if you have installed in via homebrew and you are using macOS , you can find it at /opt/homebrew/Cellar/nginx/1.23.2/html , replace 1.23.2 with your version .

Tarun Rawat
  • 244
  • 3
  • 9
0

The default public location for serving files in Nginx is typically set to /var/www/html or /usr/share/nginx/html.

Prem Patil
  • 11
  • 1