基于nginx路径的路由



我想将基于路径的请求路由到两个不同的Angular应用程序。所以当我提出要求时http://example.com/admin是到一个应用程序的路由http://example.com/client路由到第二个应用程序。我有以下配置,但所有请求都会发送到nginx默认页面。配置如下:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
location /admin {
root /home/ubuntu/apps/admin/;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
location /client {
root /home/ubuntu/apps/client;
index index.html;
try_files $uri $uri/ /index.html?$args;
}
}

/etc/nginx/sites-enabled中没有其他conf,nginx.conf是Ubuntu上安装后的默认配置。感谢您的帮助。

您在root指令中使用了错误的值。在这两个位置中,root指令的正确值都是/home/ubuntu/apps,这意味着您可以通过将一个root指令移动到server块中来简化配置。

当然,您可以使用alias指令,但如手册所述:

当位置与指令值的最后一部分匹配时。。。最好使用root指令。

另一个问题是try_files语句指向错误的index.html文件。

例如:

server {
listen 80 default_server;
listen [::]:80 default_server;
root /home/ubuntu/apps;
location /admin {
try_files $uri $uri/ /admin/index.html;
}
location /client {
try_files $uri $uri/ /client/index.html;
}
}

请注意,server_name _;不是必需的—请参阅服务器名称文档。

此外,index index.html;不必是index指令的默认值。

您似乎不能使用多个root指令,而是需要使用alias(将nginx配置为具有子域上不同根文件夹的多个位置(。这样,我仍然可以得到404秒,直到我从index.html上取下$args。在那之后,一切都很好(不要问花了多长时间才弄清楚(。工作配置:

server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
index index.html;
location /admin {
alias /home/ubuntu/apps/admin;
try_files $uri /index.html =404;
}
location /client {
alias /home/ubuntu/apps/client;
try_files $uri /index.html =404;
}
}

最新更新