Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot and Nginx integration

In my project, web app is developed using Spring boot with default tomcat server. I am using NGINX as load-balancer and have configured my spring-boot-web-app in NGINX configuration as follows:

location /spring-boot-web-app {
     proxy_pass http://spring-boot-web-app/
}

http {
    upstream /spring-boot-web-app {
        server <IP_of_spring_boot_app>:<Port_of_spring_boot_app>
    }
}

Now lets say NGINX IP and port as nginx_ip and nginx_port respectively. Also working URL for my web app as: http://web_app_ip:web_app_port/rest/echo/hi

The above URL works fine. But when i try to hit same URI via NGINX it throws 404. URL used via NGINX as: http://nginx_ip:nginx_port/spring-boot-web-app/rest/echo/hi

Is there something i am missing?

like image 779
Sam Avatar asked May 05 '16 18:05

Sam


People also ask

Can we use NGINX with spring boot?

NGINX Unit to the Rescue! Unit also enables you to configure HTTP and HTTPS interfaces independently of the applications using them. Let's explore this powerful feature with our Spring Boot API example. First, we have to build the Spring Boot application for our Unit server.

Does spring use NGINX?

Now that the Spring application is running as a service, an NGINX proxy allows opening the application to an unprivileged port and setting up SSL. The application is now accessible through the browser.

Can we deploy Java application on NGINX?

You cannot deploy using . war files. The application files need to be deployed into a folder, because the Java web application folder must be specified for NGINX so that it can find and directly send static files like images (.

Can we deploy jar in NGINX?

Nginx is a HTTP server. It can't run Jar files (or any Java code).


1 Answers

This works for me. Can you try this?

  1. Running tomcat

    docker run -d -p 8080:8080 --name=tomcat tomcat:8 
    
  2. Running nginx

    docker run -d -p 80:80 --link tomcat:tomcat --name=nginx nginx
    
  3. Go inside nginx container and update the conf

    docker exec -it nginx bash
    

    /etc/nginx/nginx.conf:

    server {
       listen 80 default_server;
      server_name subdomain.domain.com;
      location / {
          proxy_pass http://tomcat:8080;
          proxy_set_header Host      $host;
          proxy_set_header X-Real-IP $remote_addr;
      }
    }
    
  4. Restart nginx service

    nginx -s reload
    
  5. Access the tomcat through nginx from host browser. You may need to add entry to /etc/hosts

    http://subdomain.domain.com
    

Complete nginx conf: nginx.conf

like image 91
Gangaraju Avatar answered Sep 23 '22 02:09

Gangaraju