Ways to Secure NGINX

  1. Keep the Operating System and Software Patched and Updated.
  2. Restrict access where possible
  3. Use a password to protect sensitive information
  4. Use TLS/SSL to identify your site and encrypt/decrypt transmissions

Limiting Access

You can limit the access in http access module using allow or deny.

  • 192.168.1.1 - single IP address
  • 192.168.1.0/24 - network address
  • all - all address

For example

location /admin {
	allow 192.168.1.1;
	allow 192.168.1.0/16;
	allow 172.16.0.0/12;
	allow 10.0.0.0/8;
	deny all;
}

 

Configuring Authentication in NGINX

HTTP Auth Basic Module

  • auth_basic
  • auth_basic_user_file

Can be used in the HTTP server and location contexts.

Example:

location /admin {
	auth_basic "Please authenticate...";
	auth_basic_user_file /etc/nginx/passwords;
	...

}
  • Use the 'htpasswd' program to create and manage password files
user1:pr3ttyg1r7
user2:pr3ttyg1r8
user3:pr3ttyg1r9
  • You need to install the htpasswd utility to use it using apt install -y apache2-utils
htpasswd -c /etc/nginx/passwords robots
New password: 
Re-type new password: 
Adding password for user robots

 

Configuring https in nginx

For this example, I will create a SAN certificate in Let's Encrypt

sudo apt update
sudo apt install certbot
sudo apt install python3-certbot-nginx  # If you use nginx

 

Now create the SANS certificate

root@ip-172-31-2-29:/etc/nginx# sudo certbot certonly --manual --preferred-challenges dns \
-d synapseneuron.com \
-d www.synapseneuron.com \
-d ieltsspeak.synapseneuron.com \
-d ieltstest.synapseneuron.com \
-d ieltspractice.synapseneuron.com \
-d ieltsguru.synapseneuron.com
Command 'Now' not found, did you mean:
  command 'cow' from deb fl-cow (0.6-7)
  command 'sow' from deb ruby-hoe (3.22.1+dfsg1-3)
Try: apt install <deb name>
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Requesting a certificate for piconeuron.com and 5 more domains

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.ieltsguru.synapseneuron.com.

with the following value:

x0iX4VLjkwn2lPsrhqId2B2ZTI0Q_q1fMmcRZAAgPCE

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.ieltspractice.synapseneuron.com.

with the following value:

v4tKi8JBmxyDGVLsKr7JpHIqdLHlL0wJehmSdwueJHI

(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.ieltsspeak.synapseneuron.com.

with the following value:

-JgTFPnYKq3Oudz3cc4RESPWHKLut5WX3tE_L6Y1i7k

(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.ieltstest.synapseneuron.com.

with the following value:

Ot_-BGnixNHdpJ2opsQRs7HQL3xCH8p9THmS3XX2p8M

(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.synapseneuron.com.

with the following value:

z24XSVwo6HOvZIbWgjNyiehGRR4siCl9trtGcPdxXMM

(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Please deploy a DNS TXT record under the name:

_acme-challenge.www.synapseneuron.com.

with the following value:

P3DEK3IQUprLagMG80yfYWiaLPvUVXA4otGhNffZrU4

(This must be set up in addition to the previous challenges; do not remove,
replace, or undo the previous challenge tasks yet. Note that you might be
asked to create multiple distinct TXT records with the same name. This is
permitted by DNS standards.)

Before continuing, verify the TXT record has been deployed. Depending on the DNS
provider, this may take some time, from a few seconds to multiple minutes. You can
check if it has finished deploying with aid of online tools, such as the Google
Admin Toolbox: https://toolbox.googleapps.com/apps/dig/#TXT/_acme-challenge.www.piconeuron.com.
Look for one or more bolded line(s) below the line ';ANSWER'. It should show the
value(s) you've just added.

 

Remember to set the TXT record in your DNS server or DNS provider before pressing enter. This was seen in the last instruction above "Before continuing, verify the TXT record has been deployed. Depending on the DNS provider, this may take some time, from........."

TXT records are used to verify and authenticate that you own the DNS records.

I verified mine using nslookup

marygracezabala@Mac ~ % nslookup -q=TXT _acme-challenge.www.synapseneuron.com
Server:		192.168.2.1
Address:	192.168.2.1#53

Non-authoritative answer:
_acme-challenge.www.synapseneuron.com	text = "P3DEK3IQUprLagMG80yfYWiaLPvUVXA4otGhNffZrU4"

Authoritative answers can be found from:
piconeuron.com	nameserver = dns1.registrar-servers.com.
piconeuron.com	nameserver = dns2.registrar-servers.com.

marygracezabala@Mac ~ % nslookup -q=TXT _acme-challenge.ieltsspeak.synapseneuron.com
Server:		192.168.2.1
Address:	192.168.2.1#53

Non-authoritative answer:
_acme-challenge.ieltsspeak.synapseneuron.com	text = "-JgTFPnYKq3Oudz3cc4RESPWHKLut5WX3tE_L6Y1i7k"

Authoritative answers can be found from:
piconeuron.com	nameserver = dns1.registrar-servers.com.
piconeuron.com	nameserver = dns2.registrar-servers.com.

 

After that, you will see that the certificate was successfully received.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Press Enter to Continue

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/synapseneuron.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/synapseneuron.com/privkey.pem
This certificate expires on 2025-07-07.
These files will be updated when the certificate renews.

NEXT STEPS:
- This certificate will not be renewed automatically. Autorenewal of --manual certificates requires the use of an authentication hook script (--manual-auth-hook) but one was not provided. To renew this certificate, repeat this same certbot command before the certificate's expiry date.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

 

Remember that the certificates were saved at:

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/synapseneuron.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/synapseneuron.com/privkey.pem
This certificate expires on 2025-07-07.

 

Now configure the nginx to use those certificates

Map it in your nginx conf

server {
    listen 443 ssl default_server;

    ssl_certificate /etc/letsencrypt/live/synapseneuron.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/synapseneuron.com/privkey.pem;
    root /var/www/az_ielts;
    access_log /var/log/nginx/az_ielts.synapseneuron.com.access.log;
    error_log /var/log/nginx/az_ielts.synapseneuron.com.error.log;

    location / {
        try_files $uri $uri/ =404;
    }
}

After that, you can now access your site using https. Example, access your site  https://<yoursite>.com and https://www.<yoursite>.com