NGINX Hardening

This guide assumes you have already hardened the Ubuntu server itself. If you have not completed that step, please refer to the Linux Hardening (Ubuntu) guide before proceeding with web server hardening.

Now that your web server is facing the open internet, it is important to make sure it is secure.

NGINX Configuration

Open and edit the NGINX configuration file:

Terminal
sudo nano /etc/nginx/nginx.conf

Start by hiding your version to help protect against automated attacks. Inside of http{}, remove the # from the following line:

server_tokens off;

Prevent resources from being over-utilized. For example, an attacker may try to crash your server using large files. Add the below lines inside of the http{} section:

# Allow up to 10 Megabytes
client_max_body_size 10M;

Prevent click jacking, MIME sniffing, and cross site scripting. Add the below lines in the http{} section:

# Prevent click jacking, MIME sniffing, and cross site scripting
add_header X-Frame-Options "SAMEORIGIN" always;
add_header X-Content-Type-Options "nosniff" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header Referrer-Policy "strict-origin-when-cross-origin" always;

If you already have a SSL certificate, force SSL:

# Enforce HTTPS (requires SSL) 
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

CTRL+O to save, and CTRL+X to exit.

Validate the configuration:

Terminal
sudo nginx -t

Restart NGINX:

Terminal
sudo systemctl restart nginx

Update FTP Client Login

The server is now forcing key based logins. This will prevent password log ins when connecting via SFTP.

Ensure your FTP user has SSH access.

Create a SSH folder for the user:

Terminal
sudo mkdir -p /home/ftpuser/.ssh

Create the key file. Paste your public key:

Terminal
sudo nano /home/ftpuser/.ssh/authorized_keys

CTRL+O to save, and CTRL+X to close.

Adjust folder and file permissions:

Terminal
sudo chown -R ftpuser:ftpuser /home/ftpuser/.ssh
Terminal
sudo chmod 700 /home/ftpuser/.ssh
Terminal
sudo chmod 600 /home/ftpuser/.ssh/authorized_keys
Terminal
sudo chmod 750 /home/ftpuser

Add the FTP user to your allowed SSH users:

Terminal
sudo nano /etc/ssh/sshd_config

Locate the Allowusers line. Add the FTP username to this line:

Allowusers admin ftpuser

Restart SSH:

Terminal
sudo systemctl restart ssh

CTRL+O to save, and CTRL+X to close.

Create a compatible key file for the FTP client. Open PuTTYgen (available from https://puttygen.com/), then click on the Load button.

Locate and open the SSH key folder, often located in C:\Users\user-name\.ssh\. Change the File Type in the browser to All Files. Select the key file used for accessing the Ubuntu server via SSH. The app will prompt for the associated passphrase. Click on the Save private key button.

Add the generated key file to your FTP client. Using Filezilla for example, go to Edit > Settings > SFTP > Add key file. Change the client log method to Key file. Connect using your FTP client.


Loading...