how-to-configure-nginx-as-an-ssl-termination-proxy

How to Configure Nginx as an SSL Termination Proxy?

SSL termination is mandatory for any modern web application that requires secured communication. It decrypts SSL/TLS traffic on a proxy server before sending it to the backend servers. This removes that processing overhead due to encrypting and decrypting within the bounds of your application servers while enhancing overall performance and streamlining certificate management for you. Nginx is a very popular web server and that’s quite used as an ssl termination proxy to do the ssl decryption and then forward the decrypted traffic to your backend servers.

How to Install Nginx on Ubuntu

Step 1: Install Nginx

Update the package index:

				
					 sudo apt update 
				
			

Install Nginx:

				
					sudo apt install nginx -y

				
			

Verify that Nginx is running:

				
					sudo systemctl status nginx

				
			

Step 2: Secure Nginx with Let's Encrypt SSL

2.1 Install Certbot

Install the Certbot package:

				
					

 sudo apt install certbot -y

				
			

2.2 Generate Strong Diffie-Hellman (DH) Group

Generate a new 2048-bit DH parameter:

				
					 sudo openssl dhparam -out /etc/ssl/certs/dhparam.pem 2048
				
			

2.3 Prepare Let’s Encrypt Directory

Create the necessary directory:

				
					sudo mkdir -p /var/lib/letsencrypt/.well-known

				
			

Set permissions for Nginx:

				
					sudo chgrp www-data /var/lib/letsencrypt
sudo chmod g+s /var/lib/letsencrypt

				
			

Step 3: Configure Nginx for Let’s Encrypt

3.1 Create a Configuration Snippet for Let’s Encrypt

Open the file:

				
					sudo nano /etc/nginx/snippets/letsencrypt.conf

				
			

Add the following content:

				
					 location ^~ /.well-known/acme-challenge/ {
    allow all;
    root /var/lib/letsencrypt/;
    default_type "text/plain";
    try_files $uri =404;
}

				
			

Save and exit the file.

3.2 Create a Configuration Snippet for SSL Security

Open the file:

				
					sudo nano /etc/nginx/snippets/ssl.conf

				
			

Add the following content:

				
					ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_session_timeout 1d;
ssl_session_cache shared:SSL:10m;
ssl_session_tickets off;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
ssl_prefer_server_ciphers on;
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 30s;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;

				
			

Save and exit the file.

Step 4: Create the Nginx Server Block

Open the Nginx configuration file:

				
					

 sudo nano /etc/nginx/sites-available/example.com.conf

				
			

Add the following content:

				
					server {
    listen 80;
    server_name example.com www.example.com;
    include snippets/letsencrypt.conf;
}

				
			

Save and exit the file.

Enable the site:

				
					sudo ln -s /etc/nginx/sites-available/example.com.conf /etc/nginx/sites-enabled/

				
			

Restart Nginx:

				
					

 sudo systemctl restart nginx

				
			

Step 5: Obtain an SSL Certificate

Run the following command:

				
					sudo certbot certonly --agree-tos --email admin@example.com --webroot -w /var/lib/letsencrypt/ -d example.com -d www.example.com

				
			

Restart Nginx:

				
					 sudo systemctl restart nginx
				
			

Step 6: Configure SSL in Nginx

Open the Nginx server block file:

				
					

 sudo nano /etc/nginx/sites-available/example.com.conf

				
			

Add the SSL configuration content:

				
					server {
    listen 80;
    server_name www.example.com example.com;
    include snippets/letsencrypt.conf;
    return 301 https://$host$request_uri;
}
server {
    listen 443 ssl http2;
    server_name www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
   include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}
server {
    listen 443 ssl http2;
    server_name example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
    # . . . other code
}

				
			

Restart Nginx:

				
					 sudo systemctl restart nginx

				
			

Step 7: Configure Nginx as an SSL Termination Proxy for Odoo

Open the Nginx configuration file:

				
					

 sudo nano /etc/nginx/sites-enabled/example.com.conf

				
			

Add the Odoo proxy configuration content:

				
					# Odoo servers
upstream odoo {
server 127.0.0.1:8069;
}
upstream odoochat {
server 127.0.0.1:8072;
}
# HTTP -> HTTPS
server {
    listen 80;
    server_name www.example.com example.com;
    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}
# WWW -> NON WWW
server {
    listen 443 ssl http2;
    server_name www.example.com;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
    return 301 https://example.com$request_uri;
}
server {
    listen 443 ssl http2;
    server_name example.com;
    proxy_read_timeout 720s;
    proxy_connect_timeout 720s;
    proxy_send_timeout 720s;
    # Proxy headers

    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    # SSL parameters
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_trusted_certificate /etc/letsencrypt/live/example.com/chain.pem;
    include snippets/ssl.conf;
    include snippets/letsencrypt.conf;
    # log files
    access_log /var/log/nginx/odoo.access.log;
    error_log /var/log/nginx/odoo.error.log;
    # Handle longpoll requests
    location /longpolling {
        proxy_pass http://odoochat;
    }
    # Handle / requests
    location / {
       proxy_redirect off;
       proxy_pass http://odoo;
    }
    # Cache static files
    location ~* /web/static/ {
        proxy_cache_valid 200 90m;
        proxy_buffering on;
        expires 864000;
        proxy_pass http://odoo;
    }
    # Gzip
    gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
    gzip on;
}

				
			

Restart Nginx:

				
					

 sudo systemctl restart nginx

				
			

Step 8: Configure Odoo to Use the Proxy

Open the Odoo configuration file:

				
					 sudo nano /etc/odoo.conf

				
			

Add the following lines:

				
					proxy_mode = True
xmlrpc_interface = 127.0.0.1
netrpc_interface = 127.0.0.1

				
			

Restart Odoo:

				
					

 sudo systemctl restart odoo

				
			
Picture of Alfred Joshi

Alfred Joshi

I’m Alfred Joshi, a Linux System Administrator passionate about system optimization, security, and automation. Here, I share insights and tips on Linux and open-source technologies.

Get a free quote