ChatGPT解决这个技术问题 Extra ChatGPT

Nginx 默认公共 www 位置?

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

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

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


t
t56k

如果使用 apt-get 在 Ubuntu 上安装,请尝试 /usr/share/nginx/www

编辑:

在较新的版本中,路径已更改为:/usr/share/nginx/html

2019年编辑:

也可以在 /var/www/html/index.nginx-debian.html 中尝试。


Ubuntu 上的 nginx 1.4.1 位于 /usr/share/nginx/html
我刚刚在树莓上安装了nginx,默认目录和lufix+1写的一样。在从 apt-get 下载的 raspbian 上
刚刚确认在 CentOS 7.0 上,它也是 /usr/share/nginx/html
Debian apt-get,检查 /etc/nginx/available-sites/default,我找到了那里的路径。
ubuntu 16 /var/www/html/index.nginx-debian.html
G
Gnarfoz

如果您的配置不包含 root /some/absolute/path; 语句,或者它包含使用 root some/relative/path; 等相对路径的语句,则生成的路径取决于编译时选项。

如果您 downloaded 并自己编译源代码,那么可能是唯一能让您对这对您意味着什么做出有根据的猜测的情况。在这种情况下,路径将与使用的任何 --prefix 相关。如果您没有更改它,则默认为 /usr/local/nginx。您可以通过 nginx -V 找到 nginx 编译的参数,它将 --prefix 列为第一个。

由于 the root directive defaults to html,这当然会导致 /usr/local/nginx/html 成为您问题的答案。

但是,如果您以任何其他方式安装 nginx,那么所有的赌注都将失败。您的发行版可能使用完全不同的默认路径。学习弄清楚你的选择分布使用什么样的默认值完全是另一项任务。


您可能会提示您可以使用“nginx -V”(大写 V)来发现用于编译 nginx 的配置参数。
@kaufholdr 是正确的,您会发现提到的 --prefix @ Gnarfoz 看起来类似于 --prefix=/usr/share/nginx
这也取决于安装所在的操作系统,对于 Amazon Linux,目录为:/usr/share/nginx/html
--prefix 路径并不总是正确的。在我的 debian 9 nginx 安装上,前缀路径是“/var/www/html”,但事实证明实际默认路径是“/usr/share/nginx/html”。查看 /etc/nginx/sites-available/default 中的默认配置文件显示该路径与 --prefix 路径相同。但是位于 /etc/nginx/conf.d/default.conf 的配置文件将根目录列为 /usr/share/nginx/html。 default.conf 文件优先于正常的默认文件。请务必检查这两个配置文件,否则您可能使用了错误的根目录。
nginx -V 2>&1 | grep --color -o -e '--prefix=[^[:space:]]\+' 为您提供 nginx HOME。
M
Maroun

Debian 上的默认 Nginx 目录是 /var/www/nginx-default

您可以检查文件:/etc/nginx/sites-enabled/default

并找到

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

根是默认位置。


在 CentOS 中,我发现没有 /etc/nginx/sites-enabled/ 文件夹。有 /etc/nginx/conf.d/ 有两个文件 server.conf 和 virtual.conf 有这个服务器的详细信息。你也可以留意一下。
要添加到 Nafis 的评论中,conf.d 目录中的文件名必须以 .conf 结尾,否则将不会被拾取。
这仅适用于启用默认站点并与请求匹配的情况。否则,默认根为 html
@OrangeDog 如何检查 default site 是否启用?
@Timo 这个答案说明了如何
S
Serge S

'default public web root' 可以从 nginx -V 输出中找到:

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

--prefix 值是问题的答案。对于根目录上方的示例是 /var/lib/nginx


是的,最好的,跨平台的。
这并不总是正确的“--prefix”可能在某些系统上使用,但不是全部。我同意 nginx -V 为您提供了很多有助于跟踪文件位置的信息,但这取决于操作系统。 FreeBSD 上的 IE 不在任何“--prefix”中,而是在“/usr/local/www”中,在某些 linux 发行版中它也可能有所不同。最好看看配置。
“--prefix”是 GNU 标准定义的变量 - 参见 gnu.org/prep/standards/html_node/Directory-Variables.html
“--prefix”是 GNU 标准定义的变量 - 参见 gnu.org/prep/standards/html_node/Directory-Variables.html。 nginx 遵循标准。如果您没有在配置文件中定义根目录或位置(取决于 --conf-path),前缀将显示“默认公共 Web 根目录”的位置。这不依赖于操作系统,尽管随操作系统分发提供的软件包也可以提供依次重新定义“默认公共 Web 根”的配置。
所以算法:在 nginx -V 输出中寻找 --conf-path 。如果在配置中未重新定义位置 / 或根 - 使用 --prefix 作为“默认公共 Web 根”位置。
D
DimiDak

对于 Ubuntu 和 docker 映像:

/usr/share/nginx/html/


只是好奇,说我不知道,我怎么会找到正确的位置?对此有很多不同的答案
据我记得,我搜索了“nginx”(类似于 find / -name nginx )并在此目录(或其子目录)中找到了出现在我的浏览器中的文档。
f
freegnu

在 Mac OS X 上,使用 brew 安装 nginx 会生成默认目录:

/usr/local/var/www

所以:

root html

方法

root /usr/local/var/www/html

没有 html 目录,因此必须手动创建。


+1 用于提及 macOS 和自制软件。但是,我发现虽然 root htmlindex.html50x.html 直接放在 /usr/local/var/www 中。因此,我怀疑 html 文件夹的存在。你能给我一些帮助吗?
“brew info nginx”给出输出中列出的 docroot。但是在 nginx 配置中指定“root html”会更改该 nginx 配置的 docroot。
对我(MAC OS X El Capitan 10.11.5)和 brew (1.1.5) 来说,目录是 /usr/local/Cellar/nginx/1.10.2_1/html ,其中 1.10.2_1 是 nginx 版本。
在我的 brew install 中,地窖目录是一个符号链接:/usr/local/Cellar/nginx/1.12.0_1/html -> ../../../var/www/ 并且 nginx -V 显示:--prefix=/usr/local/Cellar/nginx/1.12.0_1 所以 root html 是通过该链接重定向的。
@iplus26 html 目录已经可用 /usr/local/Cellar/nginx/1.12.2_1/html,我认为 /usr/local/var/www 只是一个链接
0
030

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

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

是的,你是对的。我刚刚在我的 centos7 上安装了 Nginx,我注意到默认路径是 /usr/share/nginx/html/ 文件夹。是否有理由改变这条路径(出于安全或任何其他原因)?如果我将成为唯一的用户并且我只想在其中托管 1 个网站,是否可以将其保留在默认文件夹中?
别介意我的最后一个问题。我最终为它创建了单独的文件夹,因为我认为这样更好,以防我将来确实想在这个设置中添加不同的虚拟主机。这种方式更容易。干杯:)
这对我不起作用,使用绝对路径 /home/ubuntu/some/foo/bar/folder,名称是从 pwd 复制的,其中 ls 显示 index.html
W
Waqas

您可以简单地将 nginx 的根文件夹映射到您网站的位置:

nano /etc/nginx/sites-enabled/default

在默认文件中,在服务器标签中查找根目录并更改您网站的默认文件夹,例如我的网站位于 /var/www

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

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

当我评估 nginx、apache2 和 lighttpd 时,我将它们全部映射到位于 /var/www 的网站。我发现这是有效评估的最佳方式。

然后您可以启动/停止您选择的服务器,看看哪个性能最好。

例如

service apache2 stop
service nginx start

顺便说一句,nginx 实际上非常快!


T
Thamme Gowda

查看 nginx 配置文件以确定。此命令对您机器上配置的任何内容进行 greps:

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

在我的机器上是:root /usr/share/nginx/www;


J
Ja͢ck

nginx 的默认 web 文件夹取决于您的安装方式,但通常位于以下位置:

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

M
Mark Chackerian

运行命令 nginx -V 并查找 --prefix。使用该条目来定位您的默认路径。


r
rubicks

转储配置:

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

您得到的结果可能会有所不同,因为这取决于您的 nginx 的配置/安装方式。

参考:

手册页上的 -T 选项

-T 帮助信息中的选项

更新:如果/何时将 -T 选项添加到 nginx 的问题存在一些混淆。它于 2015 年 6 月 16 日由 vl-homutov 记录在手册页中,成为 v1.9.2 release 的一部分。 the release notes 中甚至提到了它。 -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

我认为此标志不存在,您可能需要更新答案
@ifender6445,我对您的评论感到非常困惑。我有两个对源代码行的引用,每个都显示 -T 选项。您的特定安装中是否缺少 -T 选项?你能出示任何东西来证实你的主张吗?
是的,可能是不同版本的nginx? gist.github.com/lfender6445/ed8c5e26db64eba595ae4aaf4d15ee0d
我知道了。看来我使用的是旧版本的 nginx,抱歉!
J
JJD

在 Mac 上使用 brew 安装 nginx:

/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

对于 CentOS、Ubuntu 和 Fedora,默认目录是 /usr/share/nginx/html


我想要来自 var/www/html 的 nginx 根。
R
Roman Marusyk

对于 AWS EC2 Linux,您可以在此处找到:

/usr/share/nginx

j
jboles

请注意,nginx 服务器的默认索引页面也将显示根位置。从 Amazon Linux AMI 上的 nginx (1.4.3),您将获得以下信息:

这是在 Amazon Linux AMI 上随 nginx 分发的默认 index.html 页面。它位于 /usr/share/nginx/html。

您现在应该将您的内容放在您选择的位置,并在 nginx 配置文件 /etc/nginx/nginx.conf 中编辑根配置指令


E
Eduardo Dennis

就我而言,它在 /usr/share/nginx/html

您可以尝试通过执行搜索来查找

find / -name html

Y
Yusuf Ibrahim

如果您使用的是 Ubuntu 14.04,您可以在以下路径找到 nginx www 目录:

yusuf@yusuf-he:/usr/share/nginx/html$ pwd
/usr/share/nginx/html
yusuf@yusuf-he:/usr/share/nginx/html$

谢谢,简洁准确。有时最简单的解释就是最好的解释。
我想要来自 var/www/html 的 nginx 根。
D
Dylan B

您可以搜索它,无论他们将它移动到哪里(系统管理员移动或更新版本的 nginx)

查找 / -name nginx


G
G.Denis

如果您需要找出在编译时定义的 nginx 公共根文件夹,您可以检查您的 access.log 文件。

打开 nginx.conf 找到 log_format 指令 log_format 的值是一个模板字符串,用于将信息写入 access.log 文件。您可以将 $document_root 变量添加到此模板字符串,以将默认 www 根位置记录到文件中。

这是 nginx.conf 的 http 部分中的一个示例,其中修改了 log_format 指令,在字符串的开头添加了 $document_root:

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

然后备份 conf.d 目录中的所有配置文件 *.conf 并在那里创建配置文件 test.conf ,其中包含以下行: server{ listen 80; server_name 本地主机;在 /etc/hosts 文件中添加以下行: 127.0.0.1 localhost 重新加载 nginx 配置: nginx -s reload 向 http://localhost 发送 GET 请求: curl http://localhost 检查 access.log:tail 的最后一个字符串 - n 1 /var/log/nginx/access.log

这是此命令的示例输出,其中 /etc/nginx/html 是在编译时定义的默认文档根目录:

    /etc/nginx/html 127.0.0.1 - - [15/Mar/2017:17:12:25 +0200] "GET / HTTP/1.1" 404 169 "-" "curl/7.35.0" "-"

它似乎有效,但没有解决我的问题,*.knexusgroup.com 的记录在 54.235.222.119, w3.knexusgroup.com 上,但它没有记录任何域 <anything>.knexusgroup.com 。它适用于有正确定义根目录的地方,我无法弄清楚是什么问题,为什么我找不到与任何 ngiinx 配置不匹配的任何子域的根目录。
R
Roman Marusyk

你可以访问文件配置 nginx,你可以看到 root /path。在这个默认的 nginx apache 中 /var/www/html


/var/www/html 是 apache 的默认 Web 根目录,而不是 nginx。
u
user11793803

*默认页面 web 分配在 var/www/html *默认配置服务器 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;
    }
}

*默认配置服务器 etc/nginx/nginx.conf

内容..

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

带有 ip 客户端的默认访问日志 var/log/nginx/...


D
Daniel Dong

我在 Ubuntu 上的 nginx 是“nginx 版本:nginx/1.9.12 (Ubuntu)”,根路径是 /var/www/html/

Ubuntu 信息是:没有可用的 LSB 模块。发行商 ID:Ubuntu 描述:Ubuntu 16.04 LTS 版本:16.04 代号:xenial

实际上,如果你刚刚在 Ubuntu 上安装了 nginx,那么你可以去“/etc/nginx/sites-available”查看默认文件,有一个类似“root /web/root/path/goes/here”的配置。这就是你要找的。


R
Roman Marusyk

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


g
gokul kandasamy

您可以在 /var/www/ 中找到它,这是 nginx 和 apache 的默认目录,但您可以更改它。步骤 1 转到以下文件夹 /etc/nginx/sites-available

第 2 步编辑默认文件,您可以在其中找到一个服务器块,在该块下将有一个名为 root 的行,它定义了该位置。


A
Arséne

在 ubuntu 19.04 中,我们在

/usr/share/nginx/html


l
lfender6445

对于 nginx/1.4.6 (Ubuntu)

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

J
John C

我在使用 nginx 运行 WordPress 网站的 Digital Ocean 上也遇到了这个问题。

我的解决方案是执行以下操作:

使用以下内容修改 /etc/nginx/nginx.conf 文件:

server {
root /var/www/html;
}

然后我不得不sudo service nginx restart

nginx -V 命令还显示您的 nginx 配置文件所在的位置(我的指向 /etc/nginx/nginx.conf


c
cnst

默认与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 的默认值为 set to the html directory(根据 documentation of the root directive),恰好在 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 实例上,您可以看到 root 最终设置为 /usr/share/nginx/www

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


root html;是什么意思,我的意思是什么是真正的路径?
@RamratanGupta,您没有阅读答案吗?第一行概述了路径是相对于编译时前缀设置的。
L
Lassi

Alpine Linux 根本没有任何默认位置。文件 /etc/nginx/conf.d/default.conf 说:

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

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

root /var/www/localhost/htdocs 之类的行替换它们以指向您想要的目录。然后 sudo service nginx restart 重新启动。


不对。刚刚试用了最新的 nginx:alpine 图像,它开箱即用地提供来自 /usr/share/nginx/html 的内容。
这听起来像是 nginx 项目制作的 Docker 镜像,恰好使用 Alpine Linux 作为底层操作系统。当您将 Alpine Linux 安装为您的主要操作系统并执行 apk add nginx 以从 Alpine 自己的包管理器手动安装 nginx 时,您会得到我的回答中描述的情况(或者几个月前我尝试过的情况就是这种情况)。