Nginx 无法加载 tomcat 服务器的 css、js 和 png

发布于 2025-01-10 06:27:55 字数 2735 浏览 0 评论 0原文

我正在尝试部署 tomcat &单个 AWS EC2 实例上的 Nginx 服务器。我有 3 个实例 &在每个实例上,我想部署 Nginx & Tomcat 服务器。下面是我的配置文件

/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;
        }

我的目标是,当我点击 http://IP/ 或 HTTP://IP/admin 时,它应该重定向到deploy.html,当我点击HTTP://IP/admin/admi-portal时,它应该打开tomcat服务器

注意:我在这两种情况下都取得了成功,除非我点击HTTP://IP/admin/admi-portal,然后它是仅打开 HTML 页面和 CSS/png/js 文件时出现 404:not find错误

/opt/tomcat/webapps/ROOT/ 这是所有 tomcat 静态文件 CSS/js/png 等的文件路径

谁能帮我解决这个问题吗?

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?

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

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

发布评论

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

评论(1

森罗 2025-01-17 06:27:55

也尝试点击您的 EC2 实例的竞争 URL

<instanceip>:8080/admin/admin-portal/


您可以在末尾添加“/”:-

location /admin/admin-portal/

然后尝试点击网址

<instance-ip>:8080/admin/admin-portal

现在您不需要在末尾添加“/”

Try hitting the compete url of your EC2 instance

<instanceip>:8080/admin/admin-portal/

also,
you can add "/" in the end:-

location /admin/admin-portal/

then try hitting the url with

<instance-ip>:8080/admin/admin-portal

Now you don't need to add "/" at the end

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