Showing posts with label nginx. Show all posts
Showing posts with label nginx. Show all posts

Saturday, 9 May 2020

Docker: Nginx container: hello world

In this post, I am going to explain how to run nginx container and serve html pages.

Step 1: Create HTML pages.

index.html
<html>
 <head>
  <title>Hello World</title>
 </head>

 <body>
  <h1>Hello World</h1>
 </body>
</html>

welcome.html
<html>
 <head>
  <title>Welcome</title>
 </head>

 <body>
  <h1>Welcome Dear Customer!!!!!!</h1>
 </body>
</html>

Step 2: Execute the below command to start Nginx container.

docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest

--rm: Remove this container once the command execution done.
-v $(pwd):/usr/share/nginx/html: Mount current working directory to /usr/share/nginx/html folder.
-p 1234:80: Whatever the request comes to localhost 1234 port, redirect it to container port 80.
--name my_nginx: 'my_nginx' is the name of container.
nginx:latest: Pull latest version of nginx.

$docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
54fec2fa59d0: Already exists 
4ede6f09aefe: Pull complete 
f9dc69acb465: Pull complete 
Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Status: Downloaded newer image for nginx:latest

Open the url ‘http://localhost:1234’, you will see the content of index.html file.


Open the url ‘http://localhost:1234/welcome.html’ to see the content of welcome.html file.

Login to the container my_nginx
Using ‘docker exec’ command, you can login to the container my_nginx.

docker exec -it my_nginx /bin/bash

$docker exec -it my_nginx /bin/bash
root@0973badab5e5:/#



Previous                                                    Next                                                    Home

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