可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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.
回答2:
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
回答3:
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.
回答4:
'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
回答5:
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.
回答6:
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!
回答7:
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/;
}
回答8:
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
回答9:
Run the command nginx -V
and look for the --prefix
. Use that entry to locate your default paths.
回答10:
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;
}
回答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
回答12:
For CentOS, Ubuntu and Fedora, the default directory is /usr/share/nginx/html
回答13:
For Ubuntu and docker images:
/usr/share/nginx/html/
回答14:
In my case it was in /usr/share/nginx/html
you can try to find by performing a search
find / -name html
回答15:
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$
回答16:
Demand the info by this command:
cat /etc/nginx/sites-enabled/default |grep "root"
I received :root /usr/share/nginx/www;
回答17:
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
回答18:
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" "-"
回答19:
For AWS EC2 Linux you will find here:
/usr/share/nginx
回答20:
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.
回答21:
you can access file config nginx,you can see root /path. in this
default of nginx apache at /var/www/html
回答22:
For nginx/1.4.6 (Ubuntu)
/etc/nginx$ cat /etc/nginx/sites-available/default | grep -i root
- root /usr/share/nginx/html;
回答23:
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.
回答24:
You can search for it, no matter where did they move it (system admin moved or newer version of nginx)
find / -name nginx
回答25:
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.
回答26:
In Ubuntu,
Nginx default root Directory location is /usr/share/nginx/html