Nginx 在 Tomcat 中获取的 javascript 末尾附加 html

Nginx 在 Tomcat 中获取的 javascript 末尾附加 html

我在 Tomcat 中以可部署方式运行的 Web 应用程序遇到了一个奇怪的问题。

用户最终登录时获得的 htmlhttps://my-url.com/admin 尝试从部署到 Tomcat 的应用程序中获取多个资源。

现在,如果通过 Nginx 向 Tomcat 发出第一个请求,则获取的其中一个 JavaScript 文件将具有

<html><head><title>301 Moved Permanently</title></head><body bgcolor="white"><center><h1>301 Moved Permanently</h1></center><hr><center>nginx/1.12.2</center></body></html>;

附加在文件的最末端。这会破坏整个页面,因为我们非常依赖 Javascript。但是,如果我现在重新启动 Tomcat 并直接向应用程序发出第一个请求而不经过 Nginx,则不会附加此行。之后的所有请求都将按预期工作,即使 Nginx 也是如此。

这是 nginx 配置:

user              nginx  nginx;

worker_processes  2;

pid        /var/run/nginx.pid;

worker_rlimit_nofile 1024;


events {
        worker_connections 512;
}


http {

        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        sendfile "on";
        client_max_body_size 20m;
        client_body_timeout 300s;
        client_body_in_file_only clean;
        client_body_buffer_size 16K;
        client_body_temp_path /tmp/client_body_temp;
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
        keepalive_timeout  65;
        gzip  on;
        gzip_vary on;
        gzip_proxied any;
        gzip_comp_level 6;
        gzip_buffers 16 8k;
        gzip_http_version 1.1;
        gzip_min_length 256;
        gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}

server {
  listen 443 ssl;

  ssl_certificate      /etc/ssl/certs/name.crt;
  ssl_certificate_key  /etc/ssl/private/name.key;
  ssl_session_cache shared:SSL:1m;
  ssl_prefer_server_ciphers   on;

  server_name my-url.com;

  location / {
    return 301 https://$host/admin$request_uri;
  }

  location /admin {
    proxy_set_header    Host $host;
    proxy_set_header    X-Forwarded-Host $host;
    proxy_set_header    X-Forwarded-Server $host;
    proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header    X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_pass          http://127.0.0.1:8080/admin/;
    proxy_redirect      http://127.0.0.1:8080/admin/ /admin/;
  }
}

server {
    listen 80;
    server_name my-url.com;
    access_log /dev/null;
    return 301 https://$host$request_uri;
}

有人知道发生了什么事情导致了这个问题吗?

相关内容