Friday 27 December 2019

NGINX: forward requests to respective servers


Use ‘proxy_pass’ to forward requests to respective servers.

Set up the application
Clone this repository.
$ git clone https://github.com/harikrishna553/nginx.git
Cloning into 'nginx'...
remote: Enumerating objects: 23, done.
remote: Counting objects: 100% (23/23), done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 23 (delta 2), reused 23 (delta 2), pack-reused 0
Unpacking objects: 100% (23/23), done.


Cloned project structure looks like below.
$ tree
.
└── nginx
    ├── notification-service
    │   ├── pom.xml
    │   └── src
    │       └── main
    │           └── java
    │               └── com
    │                   └── sample
    │                       └── app
    │                           ├── App.java
    │                           └── comtroller
    │                               └── NotificationController.java
    └── user-service
        ├── pom.xml
        └── src
            └── main
                └── java
                    └── com
                        └── sample
                            └── app
                                ├── App.java
                                └── controller
                                    └── UserController.java

17 directories, 6 files

Go to 'nginx/notification-service/' and execute 'mvn package' command.

Go to 'nginx/user-service/' and execute 'mvn package' command.

After executing ‘mvn package’ command, you can see two jar files.
a.   notification-service-1.jar

b.   user-service-1.jar
$find . -name *jar
./notification-service/target/notification-service-1.jar
./user-service/target/user-service-1.jar

Run notification-service-1.jar on port 9090.
Run user-service-1.jar on port 9191.

Syntax
java -jar {jarFile} -Dserver.port={portNumber}

java -jar notification-service-1.jar --server.port=9090
java -jar user-service-1.jar --server.port=9191


Open the url ‘http://localhost:9090/’, you can see below screen.

Open the url ‘http://localhost:9191/’, you can see below screen.


Update nginx.conf file
nginx.conf
events {
}

http {
    include mime.types;

    server {
        listen 1234;
        
        server_name localhost;

        location /user {
            proxy_pass http://localhost:9191/;
        }

        location /notification {
            proxy_pass http://localhost:9090/;
        }
    }
}


Start nginx server, by executing the command ‘sudo nginx’.
$sudo nginx
Password:
$
$ps -eaf | grep nginx
    0 51927     1   0  9:43PM ??         0:00.00 nginx: master process nginx
   -2 51928 51927   0  9:43PM ??         0:00.00 nginx: worker process
188003303 51930 43914   0  9:44PM ttys001    0:00.00 grep nginx


Open the url ‘http://localhost:1234/’, you will se nginx home page.

Open the url ‘http://localhost:1234/user’, you will receive the response from user service.
Open the url ‘http://localhost:1234/notification’, you will receive the response from notification service.

Similary try with below urls.


Previous                                                    Next                                                    Home

No comments:

Post a Comment