如何在 Ubuntu 16.04 上更改 Nginx Web 文档位置
在本文中,我们将学习如何移动或更改 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: # deny access to .htaccess files, if Apache's document root /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 https:///
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 服务器以应用更改。首先,我们将检查配置文件并重新启动 Niginx 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 默认文档根文件夹位置更改为挂载在其他卷上的新位置,我们希望在单个服务器上管理站点和批量站点数据。
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP