How to set https as default on nginx?

Member

by kadin , in category: Third Party Scripts , 5 months ago

How to set https as default on nginx?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 4 months ago

@kadin 

To set HTTPS as the default protocol on Nginx, you can follow these steps:

  1. Obtain an SSL/TLS certificate: First, you need to obtain an SSL/TLS certificate from a trusted Certificate Authority (CA) for your domain.
  2. Configure Nginx to use HTTPS: Edit your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default) and add the following configuration settings:
1
2
3
4
5
6
7
8
9
server {
    listen 443 ssl default_server;
    server_name example.com;

    ssl_certificate /path/to/fullchain.pem;
    ssl_certificate_key /path/to/privkey.pem;

    // other SSL/TLS settings such as SSL protocols and ciphers
}


Replace example.com with your domain name and provide the correct paths to your SSL certificate files.

  1. Redirect HTTP to HTTPS: To force all traffic to use HTTPS, you can add a server block for port 80 with a redirect to HTTPS. Add the following configuration settings:
1
2
3
4
5
server {
    listen 80 default_server;
    server_name example.com;
    return 301 https://$host$request_uri;
}


  1. Reload Nginx: After making the necessary changes, reload Nginx to apply the new configuration settings:
1
sudo systemctl reload nginx


Now, HTTPS should be set as the default protocol on your Nginx server. Make sure to test your HTTPS configuration using SSL testing tools to ensure it's properly configured and secure.