By nlubkov
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!
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:
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.
www Server Block for RedirectionIn 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.
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...
}
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
Open your web browser and try accessing www.yourdomain.com. It should automatically redirect you to yourdomain.com.
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.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.