I have a Spring MVC project, I collected it in a docker container
docker pull dockerHubAcc/project-admin:latest
docker run -d --name project-admin-container -p 8088:443 --network someNetwork -v someVolume:/var/photos dockerHubAcc/project-admin
I wrote in the application.yaml file
server:
servlet:
context-path: /project
why if I enter the following address path: http://ssh.host:8088/project/admin/view
then everything works correctly and all styles and scripts from the src/main/resources/static folder and files from volume are loaded.
But when I enter the following address path: https://ssh.host:/project/admin/view (removed port)
I encounter an issue where some styles are not loading, while others load successfully. This is despite the fact that all styles are stored in the src/main/resources/static directory.
For example:
<script src="/project/adminlte/plugins/jquery/jquery.min.js"></script>
<script src="/project/adminlte/plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="/project/adminlte/dist/js/adminlte.js?v=3.2.0"></script>
<script src="/project/adminlte/dist/js/demo.js"></script>
<script src="/project/script.js"></script>
In this block, all scripts load except for this one:
<script src="/project/script.js"></script>
In the configuration class, I added:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry
.addResourceHandler("/**")
.addResourceLocations("classpath:/static/");
registry
.addResourceHandler("/uploads/**")
.addResourceLocations("file:/var/photos/");
}
Also, when I retrieve files from the volume, for example:
When I use src="/uploads/house/k1mmljpoct78hawq.jpg" and specify the port in the URL, everything works, and the photo loads. However, when I remove the port, the files do not load.
before:
enter image description here
after:
enter image description here
I tried changing the port to 80 and it didn’t help.
I don’t know how important this information is, but I noticed that in the src/main/resources/static folder, files are uploaded only from the adminlte subfolder, and data is not uploaded from other subfolders, and this is quite strange.
enter image description here
2
Answers
Your Spring MVC application is configured to serve static resources from the
src/main/resources/static
directory. However, the reverse proxy doesn’t forward the request to your Spring MVC application when you access your application using the URLhttps://ssh.host:/project/admin/view
. This is because the reverse proxy is configured to only forward requests to port 443.To fix your issue, you need to configure your reverse proxy to forward requests to your Spring MVC application for all paths, not just
port 443
.P.s.
I hope it can help you. If you would have have problems still, let me know, please.
You should use a webserver like Nginx to forward your requests to your applications.
You can forward your requests to applications on Nginx with reverse proxy configuration. for instance:
Also, you can find Other examples of nginx reverse proxy here.
this config should be on
/etc/nginx/sites-enabled/example.conf
or/etc/nginx/conf.d/example.conf