我以前使用过Apache,所以我知道默认的公共web根目录通常是/var/www/。

我最近开始使用nginx,但我似乎找不到默认的公共web根。

我在哪里可以找到nginx的默认公共web根?


当前回答

在Ubuntu中, Nginx默认根目录位置为/usr/share/nginx/html

其他回答

运行nginx -V命令,查找——前缀。使用该条目定位默认路径。

正如这里的大多数用户所说,它是在这个路径下:

/usr/share/nginx/html

这是默认路径,但您可以创建自己的路径。

你所需要的是在web服务器根树中创建一个,并赋予它一些权限“不是0777”,只对一个用户和该用户可见,但路径的末尾对每个人都可见,因为路径的末尾是你的文件和文件夹将被公众查看的。

例如,你可以这样做:

home_web/site1/public_html/www/

无论何时你在Nginx中创建一个虚拟主机,你都可以自定义自己的根路径,只需在服务器块中添加如下内容:

 server {
    listen  80;
        server_name  yoursite.com;

root /home_web/site1/public_html/www/;
}

默认值与nginx编译时configure脚本的prefix选项有关;下面是一些来自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

随后,root的默认值被设置为html目录(根据root指令的文档),它恰好在prefix中,可以通过查看简单配置文件中的$document_root变量来验证:

# 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

然而,像Debian这样的邪恶发行版似乎对它进行了相当大的修改,以让你获得额外的娱乐:

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

所以,在这个Debian实例中,你可以看到根目录最终被设置为/usr/share/nginx/www.

但是,正如您在示例服务器配置中看到的,它将通过http提供$document_root值,配置nginx非常简单,您可以在一两行代码内编写自己的配置,指定所需的根以满足您的确切需求。

转储配置:

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

你得到的可能是不同的,因为它取决于你的nginx是如何配置/安装的。

引用:

-T选项 -T选项

更新:关于是否/何时将-T选项添加到nginx的问题有一些混淆。vl-homutov于2015年6月16日在手册页中记录了它,这成为v1.9.2发行版的一部分。甚至在发行说明中也提到了。自那以后,t选项出现在每个nginx版本中,包括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

nginx的默认web文件夹取决于你如何安装它,但通常它在这些位置:

/usr/local/nginx/html
/usr/nginx/html