如何在 Ubuntu 16.04 上更改 Nginx Web 文档位置

websiteinternetweb services

在本文中,我们将学习如何移动或更改 Nginx Web 服务器文档文件夹的位置。默认情况下,Nginx Web 服务器的默认位置是 /usr/share/nginx/html,位于 Linux 的默认文件系统中。通常,这是根据网站需求或客户需求来完成的。

先决条件

  • 要完成设置,我们需要满足以下要求。
  • 已安装 Ubuntu 16.04,并在计算机上使用 sudo 权限。
  • 已安装 Nginx Web 服务器
  • 已挂载驱动器或新文档,用于我们想要更改默认文档位置的位置。

将文档根文件复制到新位置

由于 Nginx 默认文档根目录位于 /usr/share/nginx/html,如果您已安装 Nginx 并配置了现有服务器,我们需要检查位于 /etc/nginx/sites-enabled 下的 sites-enabled 文件夹。如果现有服务器上的位置已更改,我们可以使用以下命令搜索 sites-enabled 文件夹。

$ grep “root” –R /etc/nginx/sites-enabled
/etc/nginx/sites-enabled/default: root /usr/share/nginx/html;
/etc/nginx/sites-enabled/default: # 如果 Apache 的文档位于根目录,则拒绝访问 .htaccess 文件
/etc/nginx/sites-enabled/default:# root /var/www/demosite;

将站点数据移动到新位置

假设我们将默认站点文件 /var/www/demosite 移动到新创建的卷,该卷位于 /mnt/newdatavolume。

$ sudo rsync –av /usr/share/nginx/html /mnt/newdatavolume

更改 Nginx 的配置文件

Nginx 允许我们全局或特定站点更改站点配置。在本演示中,我们将现有站点从默认位置更改为新位置,我们需要使用 grep 命令找到该位置并更改配置文件。

$ sudo vi /etc/nginx/sites-enabled/000-default

我们需要查找行 ‘root’ 并更新新位置注释该行并添加新行

root /mnt/newdatavolume

以下是默认 nginx 配置的示例文件。

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
root /mnt/newdatavolume
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# Only for nginx-naxsi used with nginx-naxsi-ui : process denied requests
#location /RequestDenied {
# proxy_pass http://127.0.0.1:8080;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html

重启 Nginx Web 服务器

配置更改后,我们需要重启 Nginx Web 服务器以应用更改。首先,我们将检查配置文件并重启 Nginx Web 服务器。

$ sudo nginx –t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

$ sudo systemctl restart nginx

在上面的文章和设置中,我们学习了如何将 Nginx 默认文档根文件夹位置更改为挂载在另一个卷上的新位置,我们希望在该卷上管理站点并在单个服务器上批量更新站点数据。


相关文章