Configuring HTTPS on Nginx

Prerequisites:

Before setting up HTTPS, ensure the following conditions are met:

  1. Nginx is installed on the server and accessible via HTTP

    For guidance on installing Nginx, refer to: Installing Nginx on Linux (Complete Guide)

  2. An SSL certificate is available; if not, you can purchase one from Alibaba Cloud or apply for a free one-year certificate

Once these prerequisites are satisfied, proceed with configuring HTTPS.

Step One: Install SSL Module for Nginx

Before applying your SSL certificate, verify that your Nginx installation includes the SSL module. Typically, self-installed versions of Nginx do not come with this module enabled.

To check whether the SSL module is present:

Navigate to your Nginx installation directory — in my case, it's (/usr/local/nginx). If your installation process matches the previous guide, you're path should be similar.

Enter the sbin subdirectory and run:

./nginx -V

If the output includes (configure arguments: --with-http_ssl_module), then the module is already installed (you may skip to Step Five).

In most cases, the module will not be installed. Proceed to the uncompressed Nginx source directory (not the installed directory). Mine is located at (/root/nginx). Navigate there and execute:

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

Then run:

make
# Avoid running make install as it will overwrite the current installation

After completion, an objs folder will appear containing the nginx binary file, as shown in the figure below.

Replace the existing Nginx binary in the sbin directory with this new version. It's recommended to backup the old file first and stop the Nginx service before replacing:

./nginx -s stop # Stop the Nginx service
cp /root/nginx/objs/nginx /usr/local/nginx/sbin

Once replaced, navigate to the Nginx installation directory and confirm the SSL module is now active:

./nginx -V

You should see configure arguments: --with-http_ssl_module, indicating successful installation.

Note: If you encounter permission issues when running the -V command, adjust permissions using:

chmod 111 nginx

Step Two: Upload SSL Certificate Files

Extract your downloaded certificate files (typically .pem and .key files with arbitrary names).

Upload the certificate files to your server. I placed mine in a folder named card under the root directory:

cd /root
mkdir card

Step Three: Configure nginx.conf

Navigate to the Nginx configuration file:

cd /usr/local/nginx/conf
vim nginx.conf

Clear unnecessary entries while ensuring matching brackets.

Add the following configuration:

http {
   include mime.types;
   default_type application/octet-stream;
   sendfile on;
   keepalive_timeout 65;

   server {
       listen 443;
       server_name huiblog.top;
       ssl on;
       ssl_certificate /root/card/huiblog.top.pem;
       ssl_certificate_key /root/card/huiblog.top.key;

       location / {
           proxy_pass http://public_ip:port;
       }
   }

   server {
       listen 80;
       server_name huiblog.top;
       rewrite ^(.*)$ https://$host$1 permanent;
   }
}

Note: Ensure port 443 is open in your security group settings.

Step Four: Restart Nginx

With all steps completed successfully, restart the Nginx service:

./nginx -s reload
./nginx -s stop
./nginx

No error messages indicate a successful setup.

Tags: nginx HTTPS ssl certificate configuration

Posted on Sat, 31 Oct 2026 16:03:15 +0000 by opels