Score:0

Nginx is failing to load css, js and png of tomcat server

ng flag

I'm trying to deploy the tomcat & Nginx server on a single AWS EC2 instance. I have 3 instances & on each instance, I wanted to deploy Nginx & Tomcat server. Below is my configuration file

/etc/nginx/nginx.conf

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

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

        ##
        # SSL Settings
        ##

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
        ssl_prefer_server_ciphers on;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
}"

/etc/nginx/conf.d/application.conf

server {
    listen         80 default_server;
    listen         [::]:80 default_server;
    server_name    localhost;
    root           /var/lib/tomcat9/webapps/ROOT;
    index          deploy.html;
    location /admin {
                try_files $uri $uri/ /deploy.html;
        }
    location /admin/admin-portal {
        alias /opt/tomcat/webapps/ROOT/;
        rewrite /admin-portal/(.*) /$1  break;
        proxy_set_header X-Forwarded-Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://localhost:8080/;
        }
    location ~ \.css {
           add_header  Content-Type    text/css;
        }
    location ~ \.js {
            add_header  Content-Type    application/x-javascript;
        }

My goal is, when I hit http://IP/ or HTTP://IP/admin then it should redirect to deploy.html and when I hit HTTP://IP/admin/admi-portal it should open tomcat server

NOTE: I got success in both conditions except when I hit HTTP://IP/admin/admi-portal then it is opening only HTML page and CSS/png/js files getting 404:not found error

/opt/tomcat/webapps/ROOT/ this is the file path for all tomcat static file CSS/js/png etc

Can anyone help me with this?

Score:0
us flag

In this case, the requests for CSS/JS files with /admin/portal/ prefix are still served by the location ~ \.css block, due to the way nginx location selection algorithm work.

If you want to prevent regex matching for anything that has /admin/admin-portal prefix, then you need to use:

location ^~ /admin/admin-portal {
    ...
}
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.