JavaDream spring boot Redirect Http to Https in Nginx

Redirect Http to Https in Nginx

Hi all in this blog we ‘ll learn about how can we redirect http to https in nginx. In simple word we ‘ll learn how our spring boot application endpoint call redirect from http to https protocol using nginx server. As we all know when we run our spring boot application we do access the endpoint on http protocol. But http protocol is not secure and almost every website use https protocol to communicate.

So if you are curious how do we achieve this we are here for you. We ‘ll see how anyone can access our spring boot api using https protocl.

For this we ‘ll use the Nginx. Nginx is a powerful web server that also act as Load Balancer, Reverse proxy, Basic API gateway etc. Let me explain how it works say for example you have a spring boot application that are running on port 8080 on your local machine. So you try to access the URL http://localhost:8080/someApiEndpoint. So what if i don’t want to show the end-user about my endpoint and port details and want something like https://localhost/someApiEndpoint,to achieve this Nginx come into the picture generally it’s http call but Nginx route it to https.

What issue this http to https redirection resolve

You are wondering why we are doing this redirection what’s the issue with http. So let me explain by an example. Say you have a react application that is hosted on AWS S3 bucket and using CloudFront and configure with certificate using ACM and you hosted it with some custom domain. And now this React application want to call endpoint of our spring boot application.

So ‘ll this React application able to call your endpoint, The answer is No why because our react application using https say my react application hosted on https://javadream.in and my spring boot application is hosted on EC2 instance and running using http so the browser ‘ll block this request due to mixed content policy.

Let’s see how can we do this redirection from http to https using Nginx. Let’s see on our local system how can we do that so later you can perform it anywhere like on AWS or wherever you want.

Install Nginx

So install the Nginx first go to this URL Download Nginx. A zip file ‘ll download just extract that and run the exe file and hit the URL http://localhost/ if everything goes good you ‘ll see the Nginx welcome page. Follow below screenshot for your reference.

Redirect from http to https

This time to see the actual magic of Nginx. We ‘ll create a spring boot application and simply expose one endpoint that return a simple string response. Just create the project and expose one endpoint. for this example we create a spring boot project that runs on 8080 port. Also we expose one endpoint for testing. Below is the controller code that return simply a string as response.

package com.javadream.SpringbootNginx.controlle;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class NginxController {

    @GetMapping("/nginx")
    public String testNginx(){
        return "Hello from Nginx";
    }
}Code language: CSS (css)

if you run your project as we are running our application on port 8080 so our URL ‘ll be http://localhost:8080/nginx. Try to hit this URL and you ‘ll get the response.

So we have everything now. We have our Nginx server and also having a spring boot application that is up and running on port 8080. Let’s see this redirection part.

SSL certificate creation

As we want to use https so we know that we need a certificate also. So we ‘ll create a self signed certificate using openssl. Make sure you have openssl installed in your system. After this we create a folder inside nginx named ssl you can give any name to this folder. Now go to this folder path and run below ssl command and follow the instruction as per given screenshot

openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mycert.key -out mycert.crtCode language: CSS (css)

you can see the .crt and .key inside your ssl folder.

Nginx config change for http to https redirection

This is the final step of the process. Just go to conf folder inside your nginx and open the nginx.conf file in editor as we need to edit this file and make some config changes.

Do the below changes in your nginx.conf file.

    server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate     C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.crt;
    ssl_certificate_key C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.key;

    location / {
        proxy_pass http://localhost:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
       }
    }Code language: JavaScript (javascript)

After doing this changes in your nginx.conf file just reload the nginx using below command.

nginx.exe -s reloadCode language: CSS (css)

After reloading nginx you already know that http://localhost is the nginx address. And we already added the SSL certificate in our conf file so we can also access it using https. So the URL for nginx ‘ll be https://localhost.

And in the config just see the proxy_pass value we mentioned our spring boot application running URL there. It means it by pass call to this given URL. And we also expose one endpoint /nginx in our controller class. So just try to hit https://localhost/nginx you must get the response.

You can see we are able to call our spring boot endpoint with https protocol. And also we are not providing any port now because we are calling the nginx URL using https protocol and behind the scene nginx forward this call to our spring boot endpoint that running on http.

So we are done now if we need to serve our spring boot endpoint on https protocol we can do this using Nginx.

Let’s play with this configuration more. What if i want to access my endpoint on the url https://localhost/vasunginxexample/nginx. here /nginx is our controller endpoint and before that is the URL that we need, We can change it as per our need. So think how can we do this.

It’s very simple we just need to change the location property like below in nginx.conf file .

after do this change just reload nginx. Now if you try to access the last URL you ‘ll get 404. Just try with this new URL that we updated you must get the response.

You are wondering every time we are seeing localhost in URL. What if we want to change this can we do this or not. The answer is yes but for this you need to update the host file on below location. And after this just update the nginx.conf file. Follow the screenshot for your reference.

C:\Windows\System32\drivers\etc

Change in nginx.conf file.

# Below Code Prevent to access by default localhost URL only that URL work that we provide in HTTPS Server
    server {
    listen 443 ssl default_server;
    server_name _;

    ssl_certificate     C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.crt;
    ssl_certificate_key C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.key;

    return 444;
    }

    # HTTPS server
    #
    server {
    listen 443 ssl;
    server_name vasurajput;

    ssl_certificate     C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.crt;
    ssl_certificate_key C:/Users/vasu/Downloads/nginx-1.29.0/nginx-1.29.0/ssl/mycert.key;

    location /{
        proxy_pass http://localhost:8080/;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}Code language: PHP (php)

Hope you like this blog. If you like please share this with others and help us to grow. You can share your feedback in our comment section.

If you need blog on any specific topic please mention it in our comment section we ‘ll try to cover that in our upcoming blogs.

Other blogs you may like

Send SMS to user mobile using Twilio in Spring boot application

Run spring boot application on Docker container

Share with others

Related Post