Ravindra BagaleCourses & study guides Track your progress

Guides

Nginx Config Explained: Change the Port, Root and Index on Amazon Linux 2023

An Nginx website is defined by a server block: listen sets the port, root sets the folder the files come from, and index sets the file sent for /. On Amazon Linux 2023, create your own file such as /etc/nginx/conf.d/tujanena.conf with listen 90;, root /var/www/tujanena;, index shayari.html; and charset utf-8;, run sudo nginx -t, then sudo service nginx reload. For a custom port, also add an inbound rule for that port in the security group, preferably from My IP.

Come on, friends! Nginx is installed and the welcome page came up – now the real fun starts. Today we understand three words: listen, root and index. The building is the IP and the flats are the ports – listen says which flat Nginx sits in. root says which folder the files are in, and index says which page to show first when the door opens. Here we change listen 80 to 90, change the root folder, and write index shayari.html. Pay attention!

Quick answer

A second site on port 90 with its own folder and page, on Amazon Linux 2023:

sudo mkdir -p /var/www/tujanena
echo '<!DOCTYPE html><html lang="mr"><head><meta charset="utf-8"><title>Shayari</title></head><body><h1>तुझ्याविना...</h1></body></html>' | sudo tee /var/www/tujanena/shayari.html
sudo tee /etc/nginx/conf.d/tujanena.conf > /dev/null <<'EOF'
server {
    listen 90;
    listen [::]:90;
    server_name _;
    root /var/www/tujanena;
    index shayari.html;
    charset utf-8;
}
EOF
sudo nginx -t && sudo service nginx reload
curl -I http://localhost:90          # expect HTTP/1.1 200 OK ... charset=utf-8

Then allow Custom TCP 90 from My IP in the security group and open http://<PUBLIC_IP>:90.

What do I need before changing the Nginx config?

How does Nginx use listen, root and index?

How Nginx uses listen, root and index A browser requests http://IP:90/. The security group allows TCP port 90 from My IP. Nginx picks the server block with listen 90, looks in its root folder /var/www/tujanena, and sends the index file shayari.html back to the browser. Browserhttp://IP:90/ SG: TCP 90 from My IP Nginx server block server { listen 90; root /var/www/tujanena; index shayari.html; charset utf-8; } /var/www/tujanena/└ shayari.html ✓ GET /:90shayari.html

A request for http://IP:90/ passes the security group rule for port 90. Nginx finds the server block with listen 90, goes to its root folder /var/www/tujanena, and sends the index file shayari.html.

Where is the Nginx configuration on Amazon Linux 2023?

File or folder What it is
/​etc/​nginx/​nginx.​conf Main file. Its http { } block contains the default server block (port 80, root /​usr/​share/​nginx/​html)
/​etc/​nginx/​conf.​d/*.​conf Your own sites. Every file ending in .conf here is loaded automatically
/​etc/​nginx/​default.​d/*.​conf Extra settings for the default server (for example PHP)
/​var/​log/​nginx/​error.​log The first place to look when something breaks

The default server block in nginx.conf looks like this (shortened):

server {
    listen       80;
    listen       [::]:80;
    server_name  _;
    root         /usr/share/nginx/html;
    include /etc/nginx/default.d/*.conf;
}

It has no index line, so Nginx uses its built-in default, index.html. Nginx configuration has its own simple syntax: a directive, its values, and a semicolon; blocks go inside { }.

How do I change the port, root and index in Nginx?

Step 1 — Back up, but outside conf.d

sudo cp /etc/nginx/nginx.conf ~/nginx.conf.backup

Keep backups in your home folder. A copy such as tujanena-copy.conf inside /etc/nginx/conf.d/ is loaded as a second site.

Step 2 — Create the root folder and the index page

sudo mkdir -p /var/www/tujanena
sudo nano /var/www/tujanena/shayari.html

Paste a small page with some Marathi text, save with Ctrl+O, Enter, and exit with Ctrl+X:

<!DOCTYPE html>
<html lang="mr">
<head><title>Shayari</title></head>
<body style="font-family:sans-serif;text-align:center;padding-top:60px">
  <h1>तुझ्याविना...</h1>
  <p>Served by Nginx from /var/www/tujanena on port 90.</p>
</body>
</html>

(This page has no <meta charset> on purpose, so you can see what charset utf-8; fixes in Step 5.) Files created with sudo get 644 and folders 755, which Nginx can read.

Step 3 — Write your own server block

sudo nano /etc/nginx/conf.d/tujanena.conf
server {
    listen 90;                 # the port (IPv4)
    listen [::]:90;            # the same port on IPv6 – no space inside [::]:90
    server_name _;             # any name; later your domain
    root /var/www/tujanena;    # the folder with the files
    index shayari.html;        # the page sent for "/"
}

Every line inside the block ends with ;. Using a new port keeps the default site on port 80 untouched, so nothing else breaks.

Ravindra Bagale's Tip

Every directive line in Nginx ends with a semicolon – delete one ; and you get an error. And when you leave nano with Ctrl+X, if Ctrl slips, an x gets typed into the file – then an unknown directive error. So before a reload, always run sudo nginx -t: it tells you the file name and line number, and the mistake is usually on the line before that line. Read calmly.

Step 4 — Test the syntax, then reload

sudo nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful
sudo service nginx reload
curl -I http://localhost:90

reload applies the change without dropping visitors. If nginx -t fails, fix the file first — a failed test never touches the running site.

Step 5 — Fix Marathi text with charset utf-8

Open http://<PUBLIC_IP>:90 (after Step 6) or run curl http://localhost:90. In the browser the Marathi line may show as strange symbols, because neither the page nor the server said which character set it uses. Add one line to the server block:

    charset utf-8;

Then sudo nginx -t && sudo service nginx reload. Now curl -I http://localhost:90 shows Content-Type: text/html; charset=utf-8 and the Marathi text is correct. In your real pages, also keep <meta charset="utf-8">.

Step 6 — Open the port in the security group

Nginx now listens on 90, but the security group still blocks it: the page just keeps loading and then times out. EC2 → the instance → Security → security group → Edit inbound rules → Add rule → Custom TCP, port 90, source My IP → Save rules. Security Group is the wall with fire around the server; only the gates (ports) you open let visitors in. For a quick class demo 0.0.0.0/0 works, but My IP is the safe default. Now http://<PUBLIC_IP>:90 shows your shayari page. 🎉

Step 7 — Try it: break it on purpose

Change What you see Why
index shayri.​html; (typo) 403 Forbidden The index file does not exist, and directory listing is off
root /​var/​www/​tujanenaa; (typo) 404 Not Found The folder does not exist
Remove ; after listen 90 nginx -t fails: invalid parameter The next line was read as part of listen
Remove charset utf-8; Marathi turns into strange symbols The browser guesses the wrong character set

Put each change back and run sudo nginx -t && sudo service nginx reload after every test.

Ravindra Bagale's Tip

"Don't copy the backup file into that folder" – every .conf file in conf.d is loaded. If you keep tujanena-copy.conf there, Nginx gets two identical server blocks: you get a conflicting server name warning, and the one loaded first wins – your changes simply don't show. Keep backups in your home folder. Has everyone got it?

On Ubuntu it is a little different

On Ubuntu, the site files live in /etc/nginx/sites-available/, and /etc/nginx/sites-enabled/ holds symlinks to them; edit the file in sites-available. The default web root there is /var/www/html. The directives (listen, root, index, charset) and sudo nginx -t are exactly the same.

How do I fix common Nginx config errors?

Ghabru naka 😅 — these are the usual ones:

Symptom Likely cause Fix
Page on the new port hangs, then times out No security group rule for that port Add Custom TCP rule for the port (My IP)
nginx -t: unknown directive "xserver" (or similar) A stray character, often an x from a Ctrl+X slip Open the file at the line number shown and delete it
nginx -t: invalid parameter or unexpected "}" Missing ; — usually on the line before the one reported Add the semicolon
conflicting server name "_" on 0.​0.​0.​0:​90, ignored Two .conf files with the same port and name (a backup in conf.d) Move the backup out of /​etc/​nginx/​conf.​d/
bind() to 0.​0.​0.​0:​90 failed (98: Address already in use) Another program uses the port sudo ss -​tlnp | grep :​90; pick another port or stop it
403 Forbidden index file missing or misspelled, or unreadable folder ls -​l /​var/​www/​tujanena; namei -​l /​var/​www/​tujanena/​shayari.​html
Marathi text looks broken No charset sent charset utf-8; in the server block; <meta charset="utf-​8"> in the page

Learn it properly

This guide is the short path. The free AWS course has a full chapter on configuring Nginx and Apache, with labs:

Samajla ka? Got it? listen is the port, root is the folder, index is the first page, and charset utf-8 keeps Marathi correct. A ; on every line, sudo nginx -t before reload, a security group rule for a new port, and backups outside conf.d. Now write your own config.

Frequently asked questions

How do I change the Nginx port on Amazon Linux 2023?

Change the listen directive in the server block (for example listen 90; and listen [::]:90;), run sudo nginx -t, reload with sudo service nginx reload, and allow the new port in the security group.

What do listen, root and index mean in Nginx?

listen is the port the server block answers on, root is the folder the files are served from, and index is the file sent when the visitor asks for a folder such as /.

Why does my site hang after changing the Nginx port?

The security group has no inbound rule for the new port. Add a Custom TCP rule for that port, preferably from My IP. curl http://localhost:PORT on the server proves Nginx itself works.

Why does Marathi or Hindi text show as strange symbols?

The browser guessed the wrong character set. Add charset utf-8; to the server block (and <meta charset="utf-8"> in the HTML), then test and reload.

Why do I get 403 Forbidden after changing root or index?

The index file does not exist in the root folder (check the spelling), or Nginx cannot read the folder. Check with ls -l and namei -l.

Where should I keep a backup of an Nginx config file?

Outside /etc/nginx/conf.d/, for example in your home folder. Every *.conf file in conf.d is loaded, so a backup copy there becomes a second, conflicting server block.