Home > Software design >  AWS Load Balancer - 'text/plain' is not a valid JavaScript MIME type
AWS Load Balancer - 'text/plain' is not a valid JavaScript MIME type

Time:11-29

I have my angular application running in AWS ECS (EC2 Instance) behind a load balancer. When i trigger the application using direct IP address of my EC2 instance the application loads fine without any issues. But when i trigger the application through the application load balancer, I see error on my browser console mentioning 'text/plain' is not a valid JavaScript MIME type. I am not sure why i am able to trigger the application without any issues while i trigger using the direct IP, but face this error only when i use the load balancer URL. Please find below the nginx configuration.

server {

        include /etc/nginx/mime.types;

        listen       443;
        listen  [::]:443;

        server_name  sampleweb.com www.sampleweb.com;

        ssl_certificate         /keys/cert.pem;
        ssl_certificate_key     /keys/key.pem;

        ssl on;
        ssl_session_cache  builtin:1000  shared:SSL:10m;
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
        ssl_prefer_server_ciphers on;

        location ~ \.css {
            add_header  Content-Type    text/css;
        }

        location ~ \.js {
            add_header  Content-Type    application/x-javascript;
        }

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;

            try_files $uri /index.html;

            add_header 'Access-Control-Allow-Origin' '*';

        }

        # redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
}

Can anyone help with this issue?

CodePudding user response:

Hard to say specifically what is going wrong without a little more info, but my guess would be that the "Content-Type" header for the HTTP responses coming out of nginx are off somehow for your JS file, and the load balancer is setting Content-Type: text/plain as a placeholder / best guess.

I'd look at two things: first, the contents of your javascript file. Is it a normal text file? e.g. utf-8 encoded, no bom, no binary data. Second, I'd remove the location ~ \.js block from your nginx config: application/x-javascript is now considered deprecated in favour of application/javascript, and nginx should already have a mime.types include file that will set the type correctly for .js files (and most other common file types.)

Best of luck!

CodePudding user response:

I was able to figure out the issue. I went through all my configuration again to see if i overlooked something. It had to do with a silly mistake i had introduced while configuring the listener for load balancer. While i configured the rules for routing based on path, i added configuration for default (when no path matches) at the end to return status code 200 and Content-Type as "text/plain". I now modified it to route it into my target group instead and it works fine.

  • Related