Day 16: Install and Configure Nginx as an LBR

sudo cat /etc/nginx/nginx.conf

# Following is the output of the above command without any changes

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    

    server {  # Block-1, already present.

        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers PROFILE=SYSTEM;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}

I added Block-2 to the nginx.conf without making any changes to Block-1

# Block-2   

upstream app_servers {
server stapp01.stratos.xfusioncorp.com:3002; # Change this port to the one on which Apache is running in all the three App Servers
server stapp02.stratos.xfusioncorp.com:3002;
server stapp03.stratos.xfusioncorp.com:3002;
}
server {
listen 80; # "Avoid using a port that conflicts with other ports in the configuration file by changing it to a unique port number."
listen [::]:80;
server_name _;
location / {
proxy_pass http://app_servers;
}
error_page 404 /404.html;
location = /404.html {}
error_page 500 502 503 504 /50x.html;
location = /50x.html {}
}

Question : After adding Block-2 though i an getting the following output for the command on the terminal

curl http://stapp01:3002

Output: Welcome to xFusionCorp Industries!

But I am not getting the desired webpage and my task is considered wrong.
Even after changing the ports to 81 in Block-2 things are same.
Kindly help.

Hi @dpakmishra.1983

I’ve just checked, and it worked properly on my side. Could you please test the application after updating Nginx and make sure it is accessible?

Many Thanks for your reply.
Shall i include both Block-1 & Block-2 in the file nginx.conf or only Block-2 to achieve the desires result ?
Also please note that both the Blocks have the same port 80.

Hi @dpakmishra.1983

You should remove the block 1 and add the block 2, then verify it.

1 Like

Have you verified if the Port 3002 is the port Apache is running on any of the App Servers?

Your Block-2 looks good.

Now you need to install nginx on the LB server, start and enable the nginx service.
Update the nginx.conf as in your Block-2, then verify that the updated nginx.conf is valid by running sudo nginx -t, and then reload nginx for the new configuration to take effect.

Yes , i did the same and got the Output.
Thank you

Yes I did the same, and running the command “sudo nginx -t” was giving error related to port conflict. So as advised by Raymond Sir , I removed Block-1 added Block-2 with port 80 , and got the output,
Thank you.


i see this webpage and
Connection to stlb01 closed.
thor@jumphost ~$ curl 172.16.238.10:3003
Welcome to xFusionCorp Industries!thor@jumphost ~$ ^C
thor@jumphost ~$ curl 172.16.238.11:3003
Welcome to xFusionCorp Industries!thor@jumphost ~$ curl 172.16.238.11:3003^C
thor@jumphost ~$ curl 172.16.238.12:3003
My nginx.conf file contents

For more information on configuration, see:

* Official English Documentation: nginx documentation

* Official Russian Documentation: nginx: документация

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.

include /usr/share/nginx/modules/*.conf;

events {
worker_connections 1024;
}

http {
log_format main '$remote_addr - $remote_user [$time_local] “$request” ’
'$status $body_bytes_sent “$http_referer” ’
‘“$http_user_agent” “$http_x_forwarded_for”’;

access_log  /var/log/nginx/access.log  main;

sendfile            on;
tcp_nopush          on;
tcp_nodelay         on;
keepalive_timeout   65;
types_hash_max_size 4096;

include             /etc/nginx/mime.types;
default_type        application/octet-stream;

Load modular configuration files from the /etc/nginx/conf.d directory.

# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
upstream app_servers {
    # The port has been updated to 3003 as per your Apache configuration
    server 172.16.238.10:3003;
    server 172.16.238.11:3003;
    server 172.16.238.12:3003;
}
server {
    listen       80;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    include /etc/nginx/default.d/*.conf;

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}
  • ‘Nginx load balancer’ is not setup correctly as website is not running on LBR URL this is the message i get
  • Add a location / block to proxy to app_servers.
upstream app_servers {
    server stapp01.stratos.xfusioncorp.com:3003;
    server stapp02.stratos.xfusioncorp.com:3003;
    server stapp03.stratos.xfusioncorp.com:3003;
}

server {
    listen 80;
    listen [::]:80;
    server_name _;

    location / {
        proxy_pass http://app_servers;
    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}


Thanks a bunch sir…
I there any particular port, there app servers must run?

Will give it a shoT!

Pls all,
I am still in the euphoria of scaling over this particular task/assessment…
I’ve actually tried all means and processes but still getting failed and stuck with LBR not configure to app servers…Pls, help generously with a step-by-step walkthrough.

I’ve actually used #Ansible

I’m told this solution works.

Thank you very much sir

I’ve fixed the issue, it was from my UI/UX…All good now

#appreciative