4

Is it possible to run static content and java related code on different ports?

I have a spring boot project where my java code resides on src/main/java and web content on src/main/resources/static.

Is there a possibility to run static content on port like 8080 and java (rest controller) on 8081 ?

I don't want to create a separate project for UI.

Bilesh Ganguly
  • 3,792
  • 3
  • 36
  • 58
Raki_2012
  • 97
  • 1
  • 7
  • Why do you want to do that? – Thilo Aug 29 '16 at 05:43
  • If it's really static content, you can run an nginx on 8080 off `src/main/resources/static` in addition to Spring Boot. – Thilo Aug 29 '16 at 05:44
  • 2
    I don't think so since they run in the same web container. But you can always create 2 Spring boot applications though it's a weird prerequisite to have them running on different ports. However, creating a Spring boot application to just serve static content seems overkill to me so you might prefer a web server like Apache HTTpd, nginx, ... . – g00glen00b Aug 29 '16 at 05:44
  • @Thilo : Thank you for response.. The reason behind this is i want to restrict people from accessing my UI and serve it as an admisntrator UI and i dont want to do a role based authorization. Just an inquisite whether it can be done ?? – Raki_2012 Aug 29 '16 at 06:20
  • why not serve static content under different path like http://server:port/context/static – kuhajeyan Aug 29 '16 at 10:01
  • @Raki_2012 so you plan to restrict people from accessing your UI by running a non secured application on a different port because people won't find out about it? You should google "security through obscurity" and why it's bad. – g00glen00b Aug 29 '16 at 11:51
  • 1
    You can have a single Spring Boot App with multiple profiles (ex: http://stackoverflow.com/questions/39205430/how-to-run-spring-boot-as-a-client-application/39206323#39206323), each launching the application using different application-${profile}.properties. However, I'm not sure I see the point, you could just as well follow @kuhajeyan 's advice and set a different context path for the static content. – alexbt Aug 31 '16 at 04:50

1 Answers1

0
  1. Configure the Spring Boot application to run on port 8080 (or any other desired port) for the REST controllers. You can do this by adding the following property to your application.properties : server.port=8080

  2. Install Nginx on your server if it's not already installed.

    Create a new Nginx configuration file (e.g., my_static_content.conf) in the Nginx configuration directory (/etc/nginx/conf.d/ or /etc/nginx/sites-available/, depending on your system setup).

    Add the following configuration to the my_static_content.conf file to listen on port 8081 and serve the static content:

nginx

server {
    listen 8081;
    server_name localhost;

    location / {
        root /path/to/your/spring/boot/app/src/main/resources;
    }
}

Replace /path/to/your/spring/boot/app/src/main/resources with the actual path to your Spring Boot application's src/main/resources directory.

Ranjan
  • 74
  • 6