Search code examples
nginxurl-rewritingnginx-locationwinginx

Static content is not loading with sub url with Nginx Server Try files


I have new to Nginx server. I have site with url and I am using try_file with Ngnix server configuration. These are some url:

http://localhost:81/test-url
http://localhost:81/test-url/abc

If am try to fetch this url

http://localhost:81/test-url

then it work fine and all css will be loaded fine.But when I am try to fetch this url

http://localhost:81/test-url/abc

then It css and js file is not loaded. Because css path will be changed to

http://localhost:81/test-url/css/style.css

but I actual path need is

http://localhost:81/css/style.css

My Nginx Server Config is

upstream dev {
        server 127.0.0.1:9130;
    }

    server {
        rewrite_log on;
        listen [::]:81;
        server_name localhost;

        location / {
            root   D:/project/src/main/webapp;
            try_files $uri /index.html;
        }
        location ~ ^/app/.+ {
            proxy_pass  http://dev;
                proxy_set_header  Host $http_host;
        }
        location ~ /api/.+ {
            proxy_pass  http://dev;
            proxy_set_header  Host $http_host;
        }
        location ~ /client/.+ {
            proxy_pass  http://dev;
            proxy_set_header  Host $http_host;
        }
        location ~ /admin/.+ {
            proxy_pass  http://dev;
            proxy_set_header  Host $http_host;
        }
        location ~ /oauth/.+ {
            proxy_pass  http://dev;
            proxy_set_header  Host $http_host;
        }
        location ~ /webhook/.+ {
            proxy_pass  http://dev;
            proxy_set_header  Host $http_host;
        }
    }

So, Please any one can help me. How to resolve this issue with Nginx server.I am using angular url to load website.


Solution

  • I have found the solution by using adding

    <base href="/">
    

    at the top in head tag.