Nginx配置为同一服务器实例上的多个静态站点

问题描述 投票:3回答:1

我有三个静态网站。我正在使用Vue 2并为每个文件夹运行构建。

我想在同一服务器实例上托管所有三个静态文件。现在我没有域,所以我想托管服务器的IP本身。

我在html / www文件夹中有文件夹

first_folder
second_folder
third_folder

以上三个文件夹中都包含index.html文件。

假设我有一个IP地址3.12.178.229

我想访问像这样的文件夹

http://3.12.178.229     // i.e path for first_folder
http://3.12.178.229/second_path    // i.e path for second_folder
http://3.12.178.229/third_path     // i.e path for third_folder

我能够访问first_folder所具有的index.html文件,但是当我尝试使用IP http://3.12.178.229/second_folder访问second_folder时它没有显示任何内容。

{
   listen 80;
   server_name 3.12.178.229;

   location / {
     root path_to_first_folder/first_folder; // I am able to access this
     index  index.html index.htm;
     try_files $uri $uri/ /index.html;
   }

   location /second_path {
     root path_to_first_folder/second_folder; // I am able to access this

     index  index.html index.htm;
     try_files $uri $uri/ /index.html;
   }

   location /third_path {
     root path_to_first_folder/third_folder; // I am able to access this

     index  index.html index.htm;
     try_files $uri $uri/ /index.html;
   }

}
nginx vue.js vue-router nginx-location nginx-config
1个回答
0
投票

通过将root指令的值与URI连接来构造所请求文件的路径名。因此,如果(例如)rootsecond_path实际上是同一个名称,那么你只能将second_folder与子文件夹一起使用。有关详细信息,请参阅this document

例如:

location /foo {
    root /path/to/root;
}

URI /foo/index.html位于/path/to/root/foo/index.html


如果second_pathsecond_folder是不同的名称,您将需要使用alias指令。有关详细信息,请参阅this document

例如:

location /foo {
    alias /path/to/root/bar;
}

URI /foo/index.html位于/path/to/root/bar/index.html

© www.soinside.com 2019 - 2024. All rights reserved.