【发布时间】:2020-01-30 20:18:30
【问题描述】:
我想在我的 Nginx 服务器上运行两个不同的 js 应用程序。我的问题是我想在端口 3000 上的主 URL / 上启用 React 应用程序,并在端口 1337 上启用不同的 js 应用程序。
当我在主 URL 上设置 React App 时,它可以正常工作,但是在不同 URL 上的任何应用程序(例如 /admin)加载第二个应用程序时都没有正确加载。我已经更改了路径,现在我在主 URL / 上有了我的第二个应用程序,它工作正常,但是当我在 /admin URL 上运行 React 应用程序时,它无法正确加载文件。
如何使用 Nginx 连接不同位置的两个 js 应用?我正在使用 react-react-app 和 create-strapi-app。您可以在 IP 上查看:http://51.178.80.233/ 和客户端:http://51.178.80.233/client
工作示例在每个位置没有两条 root 行。
我在sites-enabled/default 上的配置:
root /var/www/jozefrzadkosz-portfolio.pl;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location /client {
# First attempt to serve request as file, then
#as directory, then fall back to displaying a 404.
root /var/www/jozefrzadkosz-portfolio.pl/client;
proxy_pass http://localhost:3000; #whatever port your app runs on
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location / {
root /var/www/jozefrzadkosz-portfolio.pl/strapi;
proxy_pass http://localhost:1337;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
【问题讨论】:
-
使用子域怎么样?创建一个名为
client的dns A类型记录并指向你的IP。在启用站点的文件夹中创建一个配置文件。