Voxfor - All rights reserved - 2013-2025
We Accepted





Redirect is a technique used in web development to send users from one URL to another. It’s usually used whenever a webpage is moved, deleted or redirects traffic for a better user experience and SEO. Common redirection types are 301 Redirect (permanent), indicating a page moved to a new URL permanently, and 302 Redirect (temporary) when a page was temporarily moved. Redirects make sure that even if the original URL changes, users get the right content and help search engines know to update the index so SEO rankings are not lost. Proper use of redirects prevents “404 Not Found” errors and improves site navigation.
To start, access your VPS through SSH. Using the following command
ssh username@server_ip
It’s a good idea to update the package list and install any available updates before starting:
sudo apt update && sudo apt upgrade -y
Ubuntu/Debian:
sudo apt install nginx -y
CentOS/RHEL:
sudo yum install nginx -y
Enable Nginx to start on boot and verify it’s running:
sudo systemctl start nginx
sudo systemctl enable nginx
sudo systemctl status nginx
Using Let’s Encrypt and Certbot:
Let’s Encrypt is a free certificate provider, and Certbot automates the process of obtaining and configuring certificates.
First, install Certbot:
sudo apt install certbot python3-certbot-nginx -y
Requesting a Certificate:
Use Certbot to generate an SSL certificate and configure Nginx automatically:
sudo certbot --nginx -d your_domain -d www.your_domain
Configuring a Paid SSL Certificate:
For a paid certificate, upload your certificate files (your_domain.crt and your_domain.key) to the VPS, typically placing them in /etc/ssl/.
Edit the Nginx configuration file to specify the SSL certificate paths:
server {
listen 443 ssl;
server_name your_domain www.your_domain;
ssl_certificate /etc/ssl/your_domain.crt;
ssl_certificate_key /etc/ssl/your_domain.key;
}
Create a server block for HTTPS in /etc/nginx/sites-available/your_domain:
server {
listen 443 ssl;
server_name your_domain www.your_domain;
ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
location / {
root /var/www/your_domain;
index index.html index.htm;
}
}
Testing Configurations:
Always check configuration syntax with the following:
sudo nginx -t
Reload Nginx if there is no error:
sudo systemctl reload nginx
Redirect HTTP to HTTPS in Nginx is a recommended practice to keep all communications secure and consistent.
Add a 301 redirect to force HTTP traffic to HTTPS:
server {
listen 80;
server_name your_domain www.your_domain;
return 301 https://$host$request_uri;
}
Complete Configuration Example:
A full example of both HTTP redirect and HTTPS server blocks:
server {
listen 80;
server_name your_domain www.your_domain;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name your_domain www.your_domain;
ssl_certificate /etc/letsencrypt/live/your_domain/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/your_domain/privkey.pem;
location / {
root /var/www/your_domain;
index index.html index.htm;
}
}
Verifying the Redirect:
Use curl to confirm the redirect:
curl -I http://your_domain
HSTS forces browsers always to use HTTPS, enhancing security by preventing access via HTTP. Add this directive to your HTTPS server block:
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
Configuring Strong SSL Ciphers and Protocols:
Make sure Nginx uses only secure protocols and ciphers:
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
ssl_prefer_server_ciphers on;
Using OCSP Stapling:
OCSP stapling speeds up SSL certificate validation:
ssl_stapling on;
ssl_stapling_verify on;
resolver 8.8.8.8 8.8.4.4 valid=300s;
resolver_timeout 5s;
Use OpenSSL to diagnose issues with SSL certificates:
openssl s_client -connect your_domain:443
Using cURL to Check Redirects:
curl -I http://your_domain
SSL Analysis Tools:
Set up an automated renewal for Let’s Encrypt certificates:
sudo certbot renew
It’s wise to back up configuration files regularly:
sudo cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
Setting up HTTPS and redirect HTTP to HTTPS in Nginx is a necessary step for a secure web presence. By following these steps, maintaining regular backups, and staying updated with security practices, Check for a robust configuration that serves users securely and reliably.
Vinayak Baranwal wrote this article. Use the provided link to connect with Vinayak on LinkedIn for more insightful content or collaboration opportunities.
Amelia
Thanks for sharing such an informative post! Youโve explained the HTTP to HTTPS redirection process in Nginx very clearlyโgreat for beginners setting up secure servers.