如何在Nginx服务器上的不同位置托管多个HTML文件

时间:2019-01-24 16:14:38

标签: nginx webserver web-hosting

我在2个不同的文件夹中有2个index.html文件。如何通过基于端口和位置的映射来映射我的nginx指向这些不同的文件夹?

我曾尝试在sites-available文件夹中创建单个文件,并将其目录映射到location / {下,但这没有用

我有:

有2个html文件

/var/www/ex1.com/index.html

/var/www/ex2.com/index.html

我想做的是:

ip:8080 ex1/index.html gets rendered

ip:8081 ex2/index.html gets rendered

还有我该如何实现

ip/ex1 goes to ex1/index.html

ip/ex2 goes to ex2/index.html

1 个答案:

答案 0 :(得分:1)

要配置Nginx服务器以侦听特定端口,请使用listen指令。有关详细信息,请参见this document

例如:

server {
    listen 8080;
    root /var/www/ex1.com;
}
server {
    listen 8081;
    root /var/www/ex2.com;
}

URL http://<ip_address>/ex1http://<ip_address>/ex2将由相同的server块处理,侦听端口80。

您将需要使用alias指令而不是root指令,因为无法通过简单地将某些值与URI串联来创建本地文件的路径。

例如:

server {
    listen 80;

    location /ex1 {
        alias /var/www/ex1.com;
    }
    location /ex2 {
        alias /var/www/ex2.com;
    }
}

请注意,location值和alias值都应具有尾随/或都不具有尾随/。有关详细信息,请参见this document

相关问题