How to redirect http to https in nginx/1.18.0 ubuntu?

Member

by deron , in category: Third Party Scripts , 2 months ago

How to redirect http to https in nginx/1.18.0 ubuntu?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by giovanny.lueilwitz , 2 months ago

@deron 

To redirect HTTP to HTTPS in Nginx on Ubuntu, you can follow these steps:

  1. Open the Nginx configuration file using a text editor such as nano or vim. The configuration file is usually located at /etc/nginx/sites-available/default.
1
sudo nano /etc/nginx/sites-available/default


  1. Add the following lines to the server block in the configuration file:
1
2
3
4
5
server {
    listen 80;
    server_name your_domain.com;
    return 301 https://$server_name$request_uri;
}


Replace your_domain.com with your actual domain name.

  1. Save the file and exit the text editor.
  2. Test the Nginx configuration for syntax errors:
1
sudo nginx -t


  1. If there are no syntax errors, reload Nginx to apply the changes:
1
sudo systemctl reload nginx


Now, when users access your website using HTTP, they will be automatically redirected to HTTPS.