- Update first the package of the server
grace@ip-172-31-2-79:~$ sudo -i
[sudo] password for grace:
root@ip-172-31-2-79:~# apt update
Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble InRelease
Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates InRelease [126 kB]
Get:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-backports InRelease [126 kB]
Get:4 http://security.ubuntu.com/ubuntu noble-security InRelease [126 kB]
Get:5 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 Components [151 kB]
Get:6 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/universe amd64 Components [365 kB]
Get:7 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/restricted amd64 Components [212 B]
Get:8 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/multiverse amd64 Components [940 B]
Get:9 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-backports/main amd64 Components [7120 B]
Get:10 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-backports/universe amd64 Components [15.8 kB]
Get:11 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-backports/restricted amd64 Components [216 B]
Get:12 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-backports/multiverse amd64 Components [212 B]
Get:13 http://security.ubuntu.com/ubuntu noble-security/main amd64 Components [8960 B]
Get:14 http://security.ubuntu.com/ubuntu noble-security/universe amd64 Components [52.2 kB]
Get:15 http://security.ubuntu.com/ubuntu noble-security/restricted amd64 Components [212 B]
Get:16 http://security.ubuntu.com/ubuntu noble-security/multiverse amd64 Components [208 B]
Fetched 980 kB in 1s (1266 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
54 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@ip-172-31-2-79:~# 2. Install nginx
root@ip-172-31-2-79:~# apt install nginx
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
nginx-common
Suggested packages:
fcgiwrap nginx-doc ssl-cert
The following NEW packages will be installed:
nginx nginx-common
0 upgraded, 2 newly installed, 0 to remove and 54 not upgraded.
Need to get 551 kB of archives.
After this operation, 1596 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx-common all 1.24.0-2ubuntu7.3 [31.2 kB]
Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu noble-updates/main amd64 nginx amd64 1.24.0-2ubuntu7.3 [520 kB]
Fetched 551 kB in 0s (13.5 MB/s)
Preconfiguring packages ...
Selecting previously unselected package nginx-common.
(Reading database ... 70560 files and directories currently installed.)
Preparing to unpack .../nginx-common_1.24.0-2ubuntu7.3_all.deb ...
Unpacking nginx-common (1.24.0-2ubuntu7.3) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.24.0-2ubuntu7.3_amd64.deb ...
Unpacking nginx (1.24.0-2ubuntu7.3) ...
Setting up nginx (1.24.0-2ubuntu7.3) ...
Warning: The unit file, source configuration file or drop-ins of nginx.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Setting up nginx-common (1.24.0-2ubuntu7.3) ...
Created symlink /etc/systemd/system/multi-user.target.wants/nginx.service → /usr/lib/systemd/system/nginx.service.
Processing triggers for ufw (0.36.2-6) ...
Processing triggers for man-db (2.12.0-4build2) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.3. Check if nginx is successfully installed
root@ip-172-31-2-79:~# nginx -v
nginx version: nginx/1.24.0 (Ubuntu)
root@ip-172-31-2-79:~# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Sun 2025-04-06 20:16:37 UTC; 59s ago
Docs: man:nginx(8)
Process: 3116 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 3118 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 3119 (nginx)
Tasks: 2 (limit: 1129)
Memory: 1.8M (peak: 2.0M)
CPU: 10ms
CGroup: /system.slice/nginx.service
├─3119 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─3120 "nginx: worker process"
Apr 06 20:16:37 ip-172-31-2-79 systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Apr 06 20:16:37 ip-172-31-2-79 systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.3. Inside the /etc/nginx, you will see nginx.conf, sites-available and sites-enabled
root@ip-172-31-2-79:/etc/nginx# ls
conf.d fastcgi.conf fastcgi_params koi-utf koi-win mime.types modules-available modules-enabled nginx.conf proxy_params scgi_params sites-available sites-enabled snippets uwsgi_params win-utf
root@ip-172-31-2-79:/etc/nginx# - The nginx.conf is where the configuration of your nginx located. The default config is
user www-data;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 768;
# multi_accept on;
}
events {
worker_connections 768;
# multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
gzip on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
} 1,1 TopYou will notice that it will include /etc/nginx/conf.d/*.conf, and /etc/nginx/sites-enabled/* configurations
- sites-available contain all your vhost configuration (enabled or not)
- sites-available - contains all your symlinks to files in the sites-available folder. You can disable hosts by removing the symlink. For example, if you want to unlink the default site
root@ip-172-31-2-79:/var/www/az_ielts# unlink /etc/nginx/sites-enabled/default
4. You can put the configuration of your nginx inside the /etc/nginx/conf.d. For example
root@ip-172-31-2-79:/etc/nginx# cd conf.d/
root@ip-172-31-2-79:/etc/nginx/conf.d# vi az_ielts.confHere is the config
server {
listen 80;
root /var/www/az_ielts;
}- Next run
nginx -tto check if the configuration doesn't return an error and you can run your site successfully after you create and modify any configuration in your nginx. - You can then reload your nginx using
systemctl reload nginx - Recheck again if there is no error in your nginx using
systemctl status nginx
root@ip-172-31-2-79:/etc/nginx/conf.d# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@ip-172-31-2-79:/etc/nginx/conf.d# systemctl reload nginx
root@ip-172-31-2-79:/etc/nginx/conf.d# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; enabled; preset: enabled)
Active: active (running) since Sun 2025-04-06 20:16:37 UTC; 2h 21min ago
Docs: man:nginx(8)
Process: 3116 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 3118 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 3433 ExecReload=/usr/sbin/nginx -g daemon on; master_process on; -s reload (code=exited, status=0/SUCCESS)
Main PID: 3119 (nginx)
Tasks: 2 (limit: 1129)
Memory: 1.9M (peak: 4.3M)
CPU: 34ms
CGroup: /system.slice/nginx.service
├─3119 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
└─3435 "nginx: worker process"
Apr 06 20:16:37 ip-172-31-2-79 systemd[1]: Starting nginx.service - A high performance web server and a reverse proxy server...
Apr 06 20:16:37 ip-172-31-2-79 systemd[1]: Started nginx.service - A high performance web server and a reverse proxy server.
Apr 06 20:19:30 ip-172-31-2-79 systemd[1]: Reloading nginx.service - A high performance web server and a reverse proxy server...
Apr 06 20:19:30 ip-172-31-2-79 nginx[3194]: 2025/04/06 20:19:30 [notice] 3194#3194: signal process started
Apr 06 20:19:30 ip-172-31-2-79 systemd[1]: Reloaded nginx.service - A high performance web server and a reverse proxy server.
Apr 06 22:38:04 ip-172-31-2-79 systemd[1]: Reloading nginx.service - A high performance web server and a reverse proxy server...
Apr 06 22:38:04 ip-172-31-2-79 nginx[3433]: 2025/04/06 22:38:04 [notice] 3433#3433: signal process started
Apr 06 22:38:04 ip-172-31-2-79 systemd[1]: Reloaded nginx.service - A high performance web server and a reverse proxy server.5. Now create a site /var/www/
grace@ip-172-31-2-79:~$ mkdir -p /var/www/az_ielts
grace@ip-172-31-2-79:~$ sudo -i
root@ip-172-31-2-79:~# echo "site coming soon" > /var/www/az_ielts/index.html
root@ip-172-31-2-79:~#
Since we unlink the default unlink /etc/nginx/sites-enabled/default , when you access the site, the default will be

6. Now if you want to set a default server
server {
listen 80 default_server;
server_name ielts.local www.ielts.local;
index index.html index.htm index.php;
root /var/www/az_ielts;
}Now execute the nginx -t systemctl reload nginx and systemctl status nginx to test and reload nginx configuration.
Now execute curl localhost
root@ip-172-31-2-79:/var/www/az_ielts# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@ip-172-31-2-79:/var/www/az_ielts# systemctl reload nginx
root@ip-172-31-2-79:/var/www/az_ielts# curl localhost
<h1>site coming soon</h1>7. Location configuration in nginx tells the Nginx what directory it must serve first, if it cannot find for example in index.html, then itwill look at index.html/ then if it cannot find it, it will display 404 error. For example
server {
listen 80 default_server;
root /var/www/az_ielts;
server_name ielts.local www.ielts.local;
index index.html index.htm index.php;
access_log /var/log/nginx/az_ielts.local.access.log;
error_log /var/log/nginx/az_ielts.local.error.log;
location / {
# First attempt to serve a request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /images {
# Allow the contents of the /image folder to be listed
autoindex on;
access_log /var/log/nginx/az_ielts.local.images.access.log;
error_log /var/log/nginx/az_ielts.local.images.error.log;
}
# specify the page to serve for 404 errors
error_page 404 /404.html;
# an exact match location for the 404 page
location = /404.html {
# only use this location for internal requests
internal;
}
# specify the page to serve for 500 errors
error_page 500 502 503 504 /50x.html;
# an exact match location for the 50x page
location = /50x.html {
# only use this location for internal requests
internal;
}
# a location to demonstrate 500 errors
location /500 {
fastcgi_pass unix:/this/will/fail;
}
}8. As seen above we also configure where the access and error_logs will be saved. As we set logs, we will notice the logs az_ielts.local.access.log az_ielts.local.error.log az_ielts.local.images.access.log az_ielts.local.images.error.log
root@ip-172-31-2-79:/var/log/nginx# ls
access.log az_ielts.local.access.log az_ielts.local.error.log az_ielts.local.images.access.log az_ielts.local.images.error.log error.logChecking the logs after we execute curl localhost
root@ip-172-31-2-79:/var/log/nginx# for i in {1..10}; do curl localhost > /dev/null; done
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 37.7M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 18.0M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 36.5M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 8762k 0 --:--:-- --:--:-- --:--:-- 9761k
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 15.8M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 11.2M 0 --:--:-- --:--:-- --:--:-- 14.2M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 15.3M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 21.6M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 22.3M 0 --:--:-- --:--:-- --:--:-- 28.5M
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 29987 100 29987 0 0 48.2M 0 --:--:-- --:--:-- --:--:-- 28.5M
We also see GET logs as a result of the testing
root@ip-172-31-2-79:/var/log/nginx# cat az_ielts.local.access.log
127.0.0.1 - - [06/Apr/2025:23:50:33 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
127.0.0.1 - - [06/Apr/2025:23:51:55 +0000] "GET / HTTP/1.1" 200 29987 "-" "curl/8.5.0"
Troubleshoot Commands in Nginx
nginx -tchecks for any typo errors and misconfigurations in nginx.confsudo lsof -i :80 -i :443 | grep nginx- to check for specific files that are using port 80 and 443 and its status- Other tools for checking the network is by using net-tools. You can install it first:
sudo apt install net-tools sudo netstat -plan | grep nginx
tail -f /var/logs/nginx/*.logchecks the logs of nginx- Check for 400 or 500 error
- Check stack overflow
Reverse Proxies and Load Balancer in NGINX
- Nginx acts as a middleman between clients (e.g., browsers) and your backend services (e.g., Django, Node.js, Flask, etc.)
- Load balancer -client forwards request to load balancer then forwards to backend server. It also offer session persistence
Configure NGINX as a Reverse Proxy

- Upstream Directive - defines a group of serves that can be referenced by other directives. It can be use to configure the nginx as load balancer or reverse proxy.
upstream example {
server a.example.com:8001;
server b.example.com:8002;
}Example:
global context
http {
upstream example {
...
}
server {
location {
proxy_pass http://example;
}
}
}
Example of Configuration of Reverse Proxy
- In this example, we have a python app running in port 7001. We used the upstream to forward the http request 80 to port 7001 that runs in our server (127.0.0.1:7001)
upstream app_server_7001 {
server 127.0.0.1:7001;
}
server {
listen 80;
root /var/www/az_ielts;
server_name ielts.local www.ielts.local;
index index.html index.htm index.php;
access_log /var/log/nginx/az_ielts.local.access.log;
error_log /var/log/nginx/az_ielts.local.error.log;
location / {
# First attempt to serve a request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /images {
# Allow the contents of the /image folder to be listed
autoindex on;
access_log /var/log/nginx/az_ielts.local.images.access.log;
error_log /var/log/nginx/az_ielts.local.images.error.log;
}
# specify the page to serve for 404 errors
error_page 404 /404.html;
# an exact match location for the 404 page
location = /404.html {
# only use this location for internal requests
internal;
}
# specify the page to serve for 500 errors
error_page 500 502 503 504 /50x.html;
# an exact match location for the 50x page
location = /50x.html {
# only use this location for internal requests
internal;
}
# a location to demonstrate 500 errors
location /500 {
fastcgi_pass unix:/this/will/fail;
}
# a location to proxy requests to the upstream
# named 'app_server_7001'
location /proxy {
# Trailing slash is key!
proxy_pass http://app_server_7001/;
# Logging for access to the proxy
access_log /var/log/nginx/az_ielts.local.proxy.access.log;
error_log /var/log/nginx/az_ielts.local.proxy.error.log;
}
}
NGINX as Load Balancer
It uses three methods to connect. This method are applied within directives in upstream block
- Round Robin - default method used. There are no directives to use this server. Requests are sent to each server one at a time. Thats why this is used to distribute the load across evenly the servers.
Example:
upstream example {
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}- Least Connections - The server with the fewest connections is favoured for every new request that come in.
upstream example {
least_conn;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}- IP Hashing - IP Hashing is used for session persistence. If the client uses the same IP address, the server will route future requests to the same upstream server."
upstream example {
ip_hash;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}- Weight directive - to modify the methods above. A weight can be added to a server to give it a higher preference.
upstream example {
ip_hash;
server 127.0.0.1:7001 weight=2;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
Now let's apply it to our Linux server. Here is an example of a Python app.py running in three different ports 7001, 7002 and 7003. The nginx will use these ports in multiple upstream block.
upstream app_server_7001 {
server 127.0.0.1:7001;
}
upstream roundrobin {
# default is round robin
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
upstream leastconn {
# The server with the fewest connections will get traffic
least_conn;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
upstream iphash {
# Connections will stick to the same server
ip_hash;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
upstream weighted {
# More connections will be sent to the weighted server
server 127.0.0.1:7001 weight=2;
server 127.0.0.1:7002;
server 127.0.0.1:7003;
}
server {
listen 80;
root /var/www/az_ielts;
access_log /var/log/nginx/az_ielts.local.access.log;
error_log /var/log/nginx/az_ielts.local.error.log;
location / {
try_files $uri $uri/ =404;
}
location /images {
autoindex on;
access_log /var/log/nginx/az_ielts.local.images.access.log;
error_log /var/log/nginx/az_ielts.local.images.error.log;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
location /500 {
fastcgi_pass unix:/this/will/fail;
}
location /proxy {
# Trailing slash is key!
proxy_pass http://app_server_7001/;
# Logging for access to the proxy
access_log /var/log/nginx/az_ielts.local.proxy.access.log;
error_log /var/log/nginx/az_ielts.local.proxy.error.log;
}
location /roundrobin {
# Trailing slash is key!
proxy_pass http://roundrobin/;
}
location /leastconn {
# Trailing slash is key!
proxy_pass http://leastconn/;
}
location /iphash {
# Trailing slash is key!
proxy_pass http://iphash/;
}
location /weighted {
# Trailing slash is key!
proxy_pass http://weighted/;
}
}
No Comments