Report this

What is the reason for this report?

how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04

Posted on August 13, 2020

As i was following https://www.digitalocean.com/community/tutorials/how-to-redirect-www-to-non-www-with-nginx-on-ubuntu-14-04 i cant get to redirect from non www to the www. i still get the “Welcome to nginx!” when i dont put the www previous the domain.



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there @nlubkov,

Can you share the Nginx Server Blocks that you are currently using here?

Note that, if you want a redirect from non-www to www, you should only add the 301 rule in your non-www server block, that is where the redirection would be handled.

Your www server block should contain your correct document root and the rest of your configuration.

Regards, Bobby

I once faced this on ubuntu 20.04 and I had already configured the SSL.

Add a server block inside the config file.

server {
server_name www.example.come;
return 301 $scheme://example.com$request_uri;

#Copy all the content of the original server block except server_name

}
Then remove the www domain from the original server under server_name. If you keep the www domain under the server_name section, then your app will keep redirecting. Then restart your Nginx service
sudo service nginx restart

Redirecting www to non-www with Nginx on an Ubuntu server is a common task and can be achieved by editing your Nginx configuration file. Here’s a step-by-step guide to set this up:

Step 1: Open Your Nginx Configuration File

First, locate and open the Nginx configuration file for your domain. This file is typically found in /etc/nginx/sites-available/. The file name is usually your domain name.

sudo nano /etc/nginx/sites-available/yourdomain.com

Replace yourdomain.com with your actual domain name.

Step 2: Configure the www Server Block for Redirection

In the configuration file, you’ll set up a server block to listen for www requests and redirect them to the non-www version. Add the following configuration:

server {
    server_name www.yourdomain.com;
    return 301 $scheme://yourdomain.com$request_uri;
}

This block will catch requests to www.yourdomain.com and use a 301 redirect to send them to yourdomain.com.

Step 3: Ensure the Main Server Block is Correct

Make sure that your main server block for yourdomain.com is correctly configured to listen on the desired port (usually 80 for HTTP and 443 for HTTPS). For example:

server {
    listen 80;
    server_name yourdomain.com;

    # Other settings...
}

Step 4: Check the Configuration and Restart Nginx

After updating the configuration file, check to make sure there are no syntax errors in any of your Nginx configuration files:

sudo nginx -t

If you get a message saying that the syntax is OK, restart Nginx to apply the changes:

sudo systemctl restart nginx

Step 5: Test the Redirection

Open your web browser and try accessing www.yourdomain.com. It should automatically redirect you to yourdomain.com.

Optional: SSL Configuration

If your site is served over HTTPS, make sure you also have the SSL configuration set up in both server blocks. For the redirecting server block, it should listen on port 443 and include the SSL certificate and key paths:

server {
    listen 443 ssl;
    server_name www.yourdomain.com;

    ssl_certificate /path/to/ssl/certificate.crt;
    ssl_certificate_key /path/to/ssl/private.key;

    return 301 $scheme://yourdomain.com$request_uri;
}

Remember to replace /path/to/ssl/certificate.crt and /path/to/ssl/private.key with the actual paths to your SSL certificate and key.

By following these steps, you should be able to redirect www to non-www for your domain using Nginx on Ubuntu.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.