首页 > 解决方案 > 为多个 React 应用程序配置 Nginx

问题描述

我想在同一台服务器上使用 Nginx 提供两个 React 应用程序。在阅读了 Nginx 的文档并研究了多个网站之后,我仍然无法同时提供多个应用程序。我展示的配置在 /etc/nginx/conf.d/myserver.conf 中的一个文件中,我将展示两个示例,一个仅用于一个可以运行的应用程序,另一个用于两个应用程序,但没有吨。

此示例仅使用一个 React 应用程序(我通过http://myserver/foo访问其中)有效:

server {
    listen       80;
    server_name  myserver;
    root         /var/www/html/foo/build;

    location /foo {
        try_files  $uri $uri/ /index.html;
    }
}

这个例子不起作用,我想在http://myserver/foohttp://myserver/bar上提供两个应用程序):

server {
    listen       80;
    server_name  myserver;

    location /foo {
        root       /var/www/html/foo/build;
        try_files  $uri $uri/ /index.html;
    }

    location /bar {
        root       /var/www/html/bar/build;
        try_files  $uri $uri/ /index.html;
    }
}

我尝试过其他配置,但我发布了这个配置,因为我相信它更好地说明了我尝试做的事情。

标签: nginx

解决方案


尝试

server {
    listen       80;
    server_name  myserver;

    location /foo {
        alias       /var/www/html/foo/build;
        try_files  $uri $uri/ /index.html;
    }

    location /bar {
        alias       /var/www/html/bar/build;
        try_files  $uri $uri/ /index.html;
    }
}


推荐阅读