ChatGPT解决这个技术问题 Extra ChatGPT

NGinx Default public www location?

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?


t
t56k

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.


nginx 1.4.1 on Ubuntu is in /usr/share/nginx/html
I've just installed nginx on Raspberry, the default directory is the same written by Lufix +1. On raspbian downloaded from apt-get
Just confirmed that on CentOS 7.0, it is /usr/share/nginx/html as well
Debian apt-get, check /etc/nginx/available-sites/default,I found the path there.
ubuntu 16 /var/www/html/index.nginx-debian.html
G
Gnarfoz

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.


You could maybe give a hint that you can use "nginx -V" (uppercase V) to discover the configuration arguments used to compile nginx.
@kaufholdr is correct you will find the --prefix @ Gnarfoz mentioned looks similar to this --prefix=/usr/share/nginx
This also depends on which OS the installation is in, for Amazon Linux, the directory is: /usr/share/nginx/html
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.
nginx -V 2>&1 | grep --color -o -e '--prefix=[^[:space:]]\+' gives you the nginx HOME.
M
Maroun

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.


In CentOS, I found that there is no /etc/nginx/sites-enabled/ folder. there is /etc/nginx/conf.d/ and there two files server.conf and virtual.conf has this server details. You can look out for that also.
To add to Nafis's comment, the filename in the conf.d directory must end in .conf or they will not be picked up.
This only applies if the default site is enabled, and matches the request. Otherwise the default root is html.
@OrangeDog How to check if default site is enabled?
@Timo this answer says how
S
Serge S

'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


Yep, the best, and cross-platform.
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.
"--prefix" is the variable defined by GNU standard - see gnu.org/prep/standards/html_node/Directory-Variables.html.
"--prefix" is the variable defined by GNU standard - see 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.
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.
D
DimiDak

For Ubuntu and docker images:

/usr/share/nginx/html/


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
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.
f
freegnu

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.


+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?
"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.
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.
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.
@iplus26 html directory already available /usr/local/Cellar/nginx/1.12.2_1/html, i think /usr/local/var/www just a link
0
030

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/;
}

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?
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 :)
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.
W
Waqas

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!


T
Thamme Gowda

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;


J
Ja͢ck

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

M
Mark Chackerian

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


r
rubicks

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:

-T option on the man page

-T option in the help message

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

i dont think this flag exists, you might need to update the answer
@ifender6445, I'm thoroughly confused by your comment. I have two references to lines in the source, each showing the -T option. Is the -T option missing from your particular installation? Can you show anything to substantiate your claim?
yes it is, may be a different version of nginx? gist.github.com/lfender6445/ed8c5e26db64eba595ae4aaf4d15ee0d
i see. it looks like i am using an older version of nginx, apologies!
J
JJD

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;
}  

M
MucaP

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


I want nginx root from var/www/html.
R
Roman Marusyk

For AWS EC2 Linux you will find here:

/usr/share/nginx

j
jboles

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


E
Eduardo Dennis

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

you can try to find by performing a search

find / -name html

Y
Yusuf Ibrahim

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$

Thanks, short concise and accurate. Sometimes the simplest explanation is the best one.
I want nginx root from var/www/html.
D
Dylan B

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

find / -name nginx


G
G.Denis

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

Open nginx.conf Find the log_format directive 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. .......

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; } Add following line to /etc/hosts file: 127.0.0.1 localhost Reload nginx configuration: nginx -s reload Send GET request to http://localhost: curl http://localhost 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" "-"

it seem it works, but did not resolved me, A record for *.knexusgroup.com is on 54.235.222.119, w3.knexusgroup.com but it did not log for any domain <anything>.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.
R
Roman Marusyk

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


/var/www/html is the default web root for apache, not nginx.
u
user11793803

*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/...


D
Daniel Dong

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.


R
Roman Marusyk

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


g
gokul kandasamy

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.


A
Arséne

in ubuntu 19.04, we found it on

/usr/share/nginx/html


l
lfender6445

For nginx/1.4.6 (Ubuntu)

/etc/nginx$ cat /etc/nginx/sites-available/default | grep -i root
- root /usr/share/nginx/html;

J
John C

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

My solution was to do the following:

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)


c
cnst

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.


what does it mean root html;, I mean what is real path?
@RamratanGupta, did you not read the answer? The very first lines outline that the path is relative to the compile-time prefix setting.
L
Lassi

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.


Not true. Just tried the latest nginx:alpine image and it serves content from /usr/share/nginx/html out of the box.
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).