Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Web Servers»Nginx»Nginx – The Best Way to Force Redirect to HTTPS with WWW URL

    Nginx – The Best Way to Force Redirect to HTTPS with WWW URL

    By RahulOctober 10, 20191 Min Read

    Questions- How do I force redirect users to https with www prefix domain. How do I redirect a user to https in Nginx? The Best Way to Force Redirect Users to WWW and HTTPS.

    Advertisement

    This tutorial will provide you the best way to redirect users forcefully to https:// URL with www domain prefix. For example, your domain name is example.com, Below configuration will forcefully redirect users to https://www.example.com in all ways.

    Let’s edit Nginx virtual host configuration file:

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

    and add the below content. change example.com with your actual domain name. Also, add any other required settings specific for your Virtual host.

    # Force all users to https://www.example.com
    
    server {
        listen 80;
        server_name example.com www.example.com;
        return 301 https://www.example.com$request_uri;
    }
    server {
        listen 443 ssl;
        server_name example.com;
    
        ssl_certificate /etc/nginx/ssl/www.example.com.pem;
        ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
        return 301 https://www.example.com$request_uri;
    }
    
    server {
        listen 443 ssl;
        server_name www.example.com;
        root /var/www/html
    	
        error_page  403 /error/404.html;
        error_page  404 /error/404.html;
        error_page  500 502 503 504 /error/50x.html;
    
        ssl_certificate /etc/nginx/ssl/www.example.com.pem;
        ssl_certificate_key /etc/nginx/ssl/www.example.com.key;
    }
    

    After making the change to the configuration file, restart your Nginx web server and try to access your domain like URLs. In all the conditions, this will be redirected to https://www.example.com.

    https nginx redirect
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    How to Secure Your Nginx Web Server: Tips and Tricks

    How to enable HSTS for Enhanced Web Security in Nginx

    Boosting Your Website’s Performance with Nginx Caching

    View 8 Comments

    8 Comments

    1. dwarkanath on October 9, 2019 9:43 am

      Hello in which file ur given code need to write

      Reply
    2. cooper on October 7, 2019 12:04 pm

      web in hml get error ngnix don’t restart, and if i install force_https templates too get error ngnix don’t restart

      this is an ngnix error?

      thanks

      Reply
    3. Vinchenzo on August 10, 2019 11:44 pm

      Tnx for the help!

      Reply
    4. Rajesh Chaudhary on June 12, 2019 11:00 am

      Thank You.

      Reply
    5. linki on April 22, 2019 2:46 pm

      Thanks for sharing all of your experiences!

      Reply
    6. Rafael on January 18, 2019 2:43 am

      Hello, what is the path of the file, Nginx virtual host, to edit and insert the information reported in your tutorial?
      Thank you very much.

      Reply
    7. Kirk Douglas on January 10, 2019 12:32 am

      Thank you very much. This was exactly what I was looking for.

      Reply
    8. ibnumalik on July 17, 2018 8:03 am

      thanks for the snippet

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Setting and Getting the Default Timezone in Python
    • What is Media Access Control (MAC) Address?
    • What is Cross-Site Scripting (XSS)?
    • What is Content Security Policy (CSP)?
    • A User’s Guide to Understanding Redirection Operators in Bash
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.