NGinx默认的公共www位置?

我之前和Apache一起工作过,所以我知道默认的公共Web根目录通常是/var/www/

我最近开始使用nginx,但我似乎无法find默认的公共Web根。

我在哪里可以findNginx的默认公共Web根?

如果你的configuration不包含root /some/absolute/path; 声明,或者它包含一个使用像root some/relative/path;这样的root some/relative/path; ,那么得到的path依赖于编译时选项。

如果你自己下载并编译源代码,可能是唯一一个能够让你猜测这对你意味着什么的唯一情况。 在这种情况下,path将与相关的--prefix被使用。 如果您没有更改它,则默认为/usr/local/nginx 。 你可以find参数nginx是通过nginx -V编译的,它列出了--prefix作为第一个。

由于root指令默认为html ,所以这当然会导致/usr/local/nginx/html成为您的问题的答案。

但是,如果您以其他方式安装nginx,则所有投注都将closures。 您的分配可能使用完全不同的默认path。 学习弄清楚什么样的违约是你的select分配的东西是完全的另一个任务。

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

编辑:

在更新的版本中,path已经改为: /usr/share/nginx/html

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

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

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

根是默认位置。

“default public web root”可以从nginx -V输出中find:

 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

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

 /usr/local/var/www 

所以:

 root html 

手段

 root /usr/local/var/www/html 

没有html目录,所以必须手动创build。

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

 nano /etc/nginx/sites-enabled/default 

默认文件里面,find服务器标签中的根目录并且改变你的网站的默认文件夹,例如我的网站在/ var / www

 server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /var/www; <-- Here! ... 

当我评估nginx,apache2和lighttpd时,我把它们全部映射到我的网站,位于/ var / www 。 我发现这是评估效率最好的方法。

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

例如

 service apache2 stop service nginx start 

顺便说一句,Nginx其实是非常快!

正如这里的大多数用户所说,正是在这样的道路上:

 /usr/share/nginx/html 

这是默认的path,但你可以让你的。

您只需要在Web服务器根目录树中创build一个,并为其授予一些“不是0777”的权限,并且只为一个用户提供,并且仅对该用户可见,但path的末尾自path的末尾开始对每个人都是可见的是您的文件和文件夹将被公众查看。

例如,你可以这样做:

 home_web/site1/public_html/www/ 

每当你在Nginx中创build一个虚拟主机,你可以自定义你自己的根path,只需在你的服务器块中添加如下内容:

  server { listen 80; server_name yoursite.com; root /home_web/site1/public_html/www/; } 

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

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

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

转储configuration:

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

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

参考文献:

  • -T选项在man页上

  • 帮助信息中的-T选项

更新:在-T选项被添加到nginx时,会出现一些混淆的问题。 它在2015年6月16日由vl-homutov在手册中logging,成为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 

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

在我的情况是在/usr/share/nginx/html

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

 find / -name html 

如果您在Ubuntu 14.04上,您可以在以下pathfindnginx www目录:

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

只需要注意,nginx服务器的默认索引页面也将显示根位置。 从Amazon Linux AMI上的nginx(1.4.3)开始,您将获得以下内容:

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

你现在应该把你的内容放在你select的位置,并在nginxconfiguration文件/etc/nginx/nginx.conf中编辑根configuration指令

通过这个命令请求信息:

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

我收到: root /usr/share/nginx/www;

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

Ubuntu的信息是:没有LSB模块可用。 分销商编号:Ubuntu说明:Ubuntu 16.04 LTS发行:16.04代号:xenial

其实,如果你在Ubuntu上只安装了nginx,那么你可以到“/ etc / nginx / sites-available”并检查默认文件,有一个像“root / web / root / path / goes / here”的configuration。 这就是你正在寻找的。

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

对于AWS EC2 Linux,您可以在这里find:/ usr / share / nginx

对于nginx / 1.4.6(Ubuntu)

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

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

  1. 打开nginx.conf
  2. findlog_format指令
  3. log_format的值是一个模板string,用于将信息写入access.log文件。 您可以将$ document_rootvariables添加到此模板string以将默认www根位置logging到文件中。

下面是一个来自nginx.conf的http部分的例子,修改了log_format指令,在string的开头添加了$ 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. ....... 
  1. 然后备份conf.d目录下的所有configuration文件* .conf,并用以下命令创buildconfiguration文件test.conf

     server{ listen 80; server_name localhost; } 
    1. 将以下行添加到/ etc / hosts文件: 127.0.0.1 localhost

    2. 重新加载nginxconfiguration: nginx -s reload

    3. 发送GET请求到http:// localhost : curl http://localhost

    4. 检查access.log的最后一个string: 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" "-" 

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指令的文档 ),正如可以通过从简单的configuration文件中查看$document_rootvariables来validation的那样:

 # 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

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