在同一个 nginx 服务器上为两个 react 应用程序和一个 express 项目提供服务

时间:2021-01-01 21:51:58

标签: node.js reactjs express nginx

我有一个静态 React 网站和一个连接到 API(即 express 项目)的应用程序。着陆页不包含任何路线,我正在尝试在 domain.com/ 提供它。仪表板有路线,我正在尝试在 domain.com/app/ 下提供服务。最后,应该从 domain.com/api/ 提供 express 应用程序。在 location 块分离之前,react 应用是一个项目,express 项目将构建文件发送给客户端。一切都奏效了。以前的版本是从 location / 使用如下配置提供的:

location / {
    root /var/www/domain.com;

    proxy_set_header   X-Forwarded-For $remote_addr;
    proxy_set_header   Host $http_host;
    proxy_pass         http://000.00.00.000:5000; # my droplet IP is normally here
}

/var/www/ 目录目前的结构如下:

- var
--www
---domain.com
----landing
----dashboard
----server.js
----other files from the express project

我想了解如何在一个服务器块下分离这三个项目。使用下面显示的配置,唯一显示的是从 domain.com/ 提供的着陆。

server {
    server_name domain.com www.domain.com;

    #Configures the publicly served root directory
    #Configures the index file to be served
    #root /var/www/domain.com/landing/build;
    #index index.html index.htm;

    location app/ {
        root /var/www/domain.com/dashboard/build;
        try_files $uri $uri/ =404;
    }

    location api/ {
        root /var/www/domain.com;

        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://000.00.00.000:5000;   # my droplet IP is normally here
    }

    location / {
        root /var/www/domain.com/landing/build;
        try_files $uri $uri/ =404;
    }

    # Certbot stuff

}

如何在同一个域下分别服务这三个项目?提前致谢。

0 个答案:

没有答案