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

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

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


当前回答

如果你需要找到编译时定义的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,如下所示: 服务器{ 听80; server_name主机; } 在“/etc/hosts”文件中增加如下一行:127.0.0.1 localhost 重载nginx配置:nginx -s重载 发送GET请求到http://localhost: 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" "-"

其他回答

在Mac上使用brew安装nginx:

/ usr / local / etc / nginx / nginx上。

location / { 
    root   html;  # **means /usr/local/Cellar/nginx/1.8.0/html and it soft linked to /usr/local/var/www**
    index  index.html;
}  

默认值与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非常简单,您可以在一两行代码内编写自己的配置,指定所需的根以满足您的确切需求。

在ubuntu 19.04中,我们发现它在

/ usr / html / nginx分享

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

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

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