Nginx + React app with router+ Chrome = 子文件夹不工作

Nginx + React app with router+ Chrome = subfolders do not work

我在 nginx 下有一个 React 应用 运行ning。应用 运行 很好,没有问题。

现在,我在同一台服务器上安装了 kibana 和 portainer 运行ning,我将 nginx 配置为 运行 它们作为子文件夹。服务器有安全证书,我无法真正创建新的子域。所以我不得不使用子文件夹。

server {
listen       80;
listen       443 ssl;
server_name  api.nec.private.systems;

ssl_certificate     /etc/ssl/api.nec.private.systems.crt;
ssl_certificate_key /etc/ssl/api.nec.private.systems.key;
ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers         HIGH:!aNULL:!MD5;

root /usr/share/nginx/html;

location / {
    # Set path
    try_files $uri /index.html;
}

# Do not cache sw.js, required for offline-first updates.
location /sw.js {
    add_header Cache-Control "no-cache";
    proxy_cache_bypass $http_pragma;
    proxy_cache_revalidate on;
    expires off;
    access_log off;
}


location /control/ {
    proxy_pass http://portainer:9000/;
    add_header Cache-Control "no-cache";
    proxy_cache_bypass $http_pragma;
    proxy_cache_revalidate on;
    expires off;
    access_log off;
}
    location /kibana/ {
    proxy_pass http://kibana:5601/;
    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;
    add_header Cache-Control "no-cache";
    proxy_cache_bypass $http_pragma;
    proxy_cache_revalidate on;
    expires off;
    access_log off;
}
}

如您所见,前两个位置描述了 React 应用程序,后两个位置都是关于 kibana 和 poratiner 的。

现在,问题来了:

  1. 我会打开 google chrome 并转到 api.nec.private。systems/control - 它会毫无问题地拉起 portainer。
  2. 我会打开 api.nec.private.systems/kibana 并按预期获得 kibana。
  3. 我会打开 api.nec.private.systems/,然后会打开带有 React 路由器的 React 应用程序。
  4. 现在,完成第 3 步后,我将打开 api.nec.private.systems/kibana,它不会再打开 kibana,而是会尝试在我的文件中输入 /kibana路由器。它根本不会打开 kibana。不管我怎么努力。

  5. 步骤编号 X - 清除 google chrome 的缓存并重试 - kibana 和 poirtainer 工作正常。直到我打开 React 应用程序。

有什么想法吗?

好的,所以我在这里找到了自己的问题。这都是因为create-react-app自带的service worker。基本上,service worker 试图将所有请求从应用程序重定向到本地存储缓存。

终止该服务并开始正常工作。