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!
चला मित्रांनो! Nginx इन्स्टॉल झाला, welcome पेज आलं – आता खरी मजा. आज तीन शब्द समजून घ्यायचे: listen, root आणि index. बिल्डिंग म्हणजे IP, फ्लॅट्स म्हणजे पोर्ट्स – listen सांगतो Nginx कोणत्या फ्लॅट मध्ये बसलाय. root सांगतो फाइल्स कोणत्या फोल्डर मध्ये आहेत, आणि index सांगतो दार उघडलं की पहिलं कोणतं पेज दाखवायचं. इथे listen 80 ला 90 करूया, root फोल्डर चेंज करूया, आणि index shayari.html लिहूया. लक्ष द्या!
चलो दोस्तों! Nginx इंस्टॉल हुआ, welcome पेज आया – अब असली मज़ा. आज तीन शब्द समझने हैं: listen, root और index. बिल्डिंग मतलब IP, फ़्लैट्स मतलब पोर्ट्स – listen बताता है Nginx किस फ़्लैट में बैठा है. root बताता है फ़ाइलें किस फ़ोल्डर में हैं, और index बताता है दरवाज़ा खुलते ही पहला कौन सा पेज दिखाना है. यहाँ listen 80 को 90 करेंगे, root फ़ोल्डर चेंज करेंगे, और index shayari.html लिखेंगे. ध्यान दो!
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?
- Nginx installed and running on Amazon Linux 2023 — see How to Install Nginx on Amazon Linux 2023.
- Basic editing with
nanoandsudo. - Access to the instance's security group — see How to Open Port 80 and 443 in an EC2 Security Group.
How does Nginx use listen, root and index?
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.
http://IP:90/ ची रिक्वेस्ट पोर्ट 90 च्या सिक्युरिटी ग्रुप रूल मधून पास होते. Nginx listen 90 असलेला server block शोधतो, त्याच्या root फोल्डर /var/www/tujanena मध्ये जातो, आणि index फाइल shayari.html पाठवतो.
http://IP:90/ की रिक्वेस्ट पोर्ट 90 के सिक्योरिटी ग्रुप रूल से पास होती है. Nginx listen 90 वाला server block ढूँढता है, उसके root फ़ोल्डर /var/www/tujanena में जाता है, और index फ़ाइल 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.
Ravindra Bagale's Tip – मराठी
Nginx मधली प्रत्येक directive लाइन semicolon ने एंड होते – एक ; डिलीट केला तर एरर येईल. आणि nano मधून बाहेर पडताना Ctrl+X दाबताना Ctrl सुटला तर फाइल मध्ये एक x टाइप होतो – मग unknown directive एरर. म्हणून reload आधी नेहमी sudo nginx -t: तो फाइलचं नाव आणि लाइन नंबर सांगतो, आणि चूक बहुतेक वेळा त्या लाइनच्या आधीच्या लाइनवर असते. शांतपणे वाचा.
Ravindra Bagale's Tip – हिंदी
Nginx की हर directive लाइन semicolon से एंड होती है – एक ; डिलीट किया तो एरर आएगा. और nano से बाहर निकलते समय Ctrl+X दबाते हुए Ctrl छूट गया तो फ़ाइल में एक x टाइप हो जाता है – फिर unknown directive एरर. इसलिए reload से पहले हमेशा sudo nginx -t: वह फ़ाइल का नाम और लाइन नंबर बताता है, और ग़लती ज़्यादातर उस लाइन की पिछली लाइन पर होती है. शांति से पढ़ो.
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?
Ravindra Bagale's Tip – मराठी
"बॅकअप फाइल त्या फोल्डर मध्ये कॉपी करू नका" – conf.d मधली प्रत्येक .conf फाइल लोड होते. tujanena-copy.conf तिथे ठेवली तर Nginx ला दोन सेम server blocks मिळतात: conflicting server name warning येतो, आणि जो आधी लोड होतो तो जिंकतो – तुमचे बदल दिसतच नाहीत. बॅकअप home फोल्डर मध्ये ठेवा. समजलं का सगळ्यांना?
Ravindra Bagale's Tip – हिंदी
"बैकअप फ़ाइल उस फ़ोल्डर में कॉपी मत करो" – conf.d की हर .conf फ़ाइल लोड होती है. tujanena-copy.conf वहाँ रखी तो Nginx को दो एक जैसे server blocks मिलते हैं: conflicting server name warning आता है, और जो पहले लोड होता है वही जीतता है – आपके बदलाव दिखते ही नहीं. बैकअप home फ़ोल्डर में रखो. समझ आया सबको?
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.
समजलं का? listen म्हणजे पोर्ट, root म्हणजे फोल्डर, index म्हणजे पहिलं पेज, आणि charset utf-8 म्हणजे मराठी बरोबर. प्रत्येक लाइनला ;, reload आधी sudo nginx -t, नवीन पोर्ट साठी सिक्युरिटी ग्रुप रूल, आणि बॅकअप conf.d च्या बाहेर. आता तुमची स्वतःची कॉन्फिग लिहून बघा.
समझ आया? listen मतलब पोर्ट, root मतलब फ़ोल्डर, index मतलब पहला पेज, और charset utf-8 मतलब मराठी सही. हर लाइन पर ;, reload से पहले sudo nginx -t, नए पोर्ट के लिए सिक्योरिटी ग्रुप रूल, और बैकअप conf.d के बाहर. अब अपनी ख़ुद की कॉन्फ़िग लिखकर देखो.
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.