Nginx 无法解析上游 React JS 文件并回退到 index.html

发布于 2025-01-10 21:41:24 字数 4479 浏览 0 评论 0原文

我有一个网站的 docker swarm 设置。主要网站是 Angular 的,网站的特定部分是 React 应用程序。

swarm 集群有 3 个应用程序的 3 个服务。 Angular 和 React 应用程序均通过 Nginx

  1. Angular 应用程序 (https://mywebsite.com)
  2. 提供服务 React 应用程序 (https://mywebsite.com/viewer)
  3. 具有 /api 端点的 Spring Boot 应用程序 (https://mywebsite.com/api)

为网站提供服务的主要 Nginx 配置,以及作为上游的 Spring boot 和 React 应用程序。下面的 Nginx 文件显示了 3 个应用程序的 3 个位置块。

nginx.conf

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events
{
  worker_connections 1024;
}

http
{
    upstream springboot
    {
        server springboot:8081;
    }

    upstream viewer
    {
      server viewer;
    }

   # Redirect all requests to HTTPS server
    server
    {
        listen 80;
        server_name mywebsite.com;

        # redirects http requests to https
        return 301 https://mywebsite.com$request_uri;
    }

  server
  {
    listen 443 ssl http2;
    server_name mywebsite.com;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_certificate /etc/nginx/certs/mywebsite_com.crt;
    ssl_certificate_key /etc/nginx/certs/mywebsite_com.key;
    ssl_trusted_certificate /etc/nginx/certs/mywebsite_com_truster_chain.crt;
    ssl_prefer_server_ciphers on;

    location /
    {
      #### Gzip Settings  ####
      gzip on;
      gzip_min_length   1100;
      gzip_vary         on;
      gzip_proxied      expired no-cache no-store private auth;
      gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      gzip_comp_level   5;

      #### Serve Angular Application ####
      root /usr/share/nginx/html;
      try_files $uri $uri/ /index.html;
      add_header Cache-Control "no-store, no-cache, must-revalidate";
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
    }

   location /viewer
    {
      proxy_pass http://viewer;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api
    {
      proxy_pass http://springboot;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;
  sendfile on;
  keepalive_timeout 30m;
  include /etc/nginx/conf.d/*.conf;
}

nginx.conf (React)

server {
  listen 80;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

角度应用程序和 /api/ 路径工作正常。但是当我尝试转到 https://mywebsite.com/viewer 路径时,正在渲染 JS 文件。只有 index.html 文件呈现,其余 JS 文件无法找到,并回退到 index.html,这当然会失败

错误:

Uncaught SyntaxError: Unexpected token '<'.init-service-worker.js:1 
        
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. vendors~app.bundle.9…07e3ba0a6ba969.js:1 
        
Uncaught SyntaxError: Unexpected token '<'

I have docker swarm setup for a website. The main website is in Angular and specific portion of the website is React application.

The swarm cluster has 3 services for 3 apps. Both Angular and React apps served through Nginx

  1. Angular App (https://mywebsite.com)
  2. React App (https://mywebsite.com/viewer)
  3. Spring Boot app with /api end points (https://mywebsite.com/api)

Primary Nginx config that serves the website and Spring boot and React apps served as upstreams. Below Nginx file shows 3 location blocks for 3 apps.

nginx.conf

user nginx;
worker_processes auto;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events
{
  worker_connections 1024;
}

http
{
    upstream springboot
    {
        server springboot:8081;
    }

    upstream viewer
    {
      server viewer;
    }

   # Redirect all requests to HTTPS server
    server
    {
        listen 80;
        server_name mywebsite.com;

        # redirects http requests to https
        return 301 https://mywebsite.com$request_uri;
    }

  server
  {
    listen 443 ssl http2;
    server_name mywebsite.com;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_certificate /etc/nginx/certs/mywebsite_com.crt;
    ssl_certificate_key /etc/nginx/certs/mywebsite_com.key;
    ssl_trusted_certificate /etc/nginx/certs/mywebsite_com_truster_chain.crt;
    ssl_prefer_server_ciphers on;

    location /
    {
      #### Gzip Settings  ####
      gzip on;
      gzip_min_length   1100;
      gzip_vary         on;
      gzip_proxied      expired no-cache no-store private auth;
      gzip_types        text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
      gzip_comp_level   5;

      #### Serve Angular Application ####
      root /usr/share/nginx/html;
      try_files $uri $uri/ /index.html;
      add_header Cache-Control "no-store, no-cache, must-revalidate";
      proxy_http_version 1.1;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_set_header X-Forwarded-Port $server_port;
    }

   location /viewer
    {
      proxy_pass http://viewer;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /api
    {
      proxy_pass http://springboot;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Host $host;
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-Port $server_port;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  '$status $body_bytes_sent "$http_referer" '
  '"$http_user_agent" "$http_x_forwarded_for"';

  access_log /var/log/nginx/access.log main;
  sendfile on;
  keepalive_timeout 30m;
  include /etc/nginx/conf.d/*.conf;
}

nginx.conf (React)

server {
  listen 80;

  location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}

The angular app and /api/ paths work fine. But when I try to go to https://mywebsite.com/viewer path, the JS files are being rendered. Only index.html file renders and rest of the JS files could not be found and falls back to index.html which of course fails

Error:

Uncaught SyntaxError: Unexpected token '<'.init-service-worker.js:1 
        
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec. vendors~app.bundle.9…07e3ba0a6ba969.js:1 
        
Uncaught SyntaxError: Unexpected token '<'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

对岸观火 2025-01-17 21:41:24

事实证明问题出在 Nginx 上。 /viewer/ 修复问题后重写 URL

rewrite ^/viewer(/.*)$ $1 break;

,这里是完整的 Nginx 文件

server {
  listen 80;

  location / {
    rewrite ^/viewer(/.*)$ $1 break;
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}


As it turns out the issue was with Nginx. Rewriting URL after /viewer/ fixed the issue

rewrite ^/viewer(/.*)$ $1 break;

here is the complete Nginx file

server {
  listen 80;

  location / {
    rewrite ^/viewer(/.*)$ $1 break;
    root   /usr/share/nginx/html;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
  }
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root   /usr/share/nginx/html;
  }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文