nginx loops between www and non-www versions, Please help with nginx config

Need to resolve this asap, this is basic setup but does not work, please have a look at config files below. site is looping between www and non-www until firefox of chrome stops it:

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

here is my nginx.conf

user xxxxxxx; worker_processes 1; pid /run/; include /etc/nginx/modules-enabled/*.conf;

events { worker_connections 1024; multi_accept on; }

http {

# Basic Settings

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
server_tokens off;
client_max_body_size 16m;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

# SSL Settings

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

# Logging Settings

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

# Gzip Settings

gzip on;
gzip_disable "msie6";

# gzip_vary on;
gzip_proxied any;
gzip_comp_level 2;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

# Virtual Host Configs

include /etc/nginx/conf.d/*.conf;

include /etc/nginx/sites-enabled/*;

server { listen 80 default_server; listen [::]:80 default_server; server_name _; return 444; }


and sites-available -

server { listen 80; listen [::]:80;


access_log /home/xxxx/;
error_log /home/xxxxx/;

root /home/xxxxx/;
index index.php;

location / { try_files $uri $uri/ /index.php$is_args$args;

try_files $uri $uri/ /index.php?$args;


location ~ .php$ { try_files $uri =404; fastcgi_split_path_info ^(.+.php)(/.+)$; fastcgi_pass unix:/run/php/php7.1-fpm.sock; fastcgi_index index.php; include fastcgi_params; }


Submit an answer

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!

Sign In or Sign Up to Answer

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.

Site Moderator
Site Moderator badge
July 26, 2023


If your website is stuck in a redirect loop between the www and non-www versions, it’s usually because your server configuration is telling it to redirect from www to non-www and vice versa. This is a common problem, and can often be resolved by carefully examining your server block configurations.

The part of your configuration that is responsible for the redirect loop might not be included in your main nginx.conf file. The redirect rules are typically located in the server blocks for the specific sites, which are included in the ‘sites-enabled’ directory.

Let’s assume that you want to redirect all www requests to the non-www version of your site. Your server block might look like this:

server {
    return 301 $scheme://$request_uri;

server {
    listen 80 default_server;
    listen [::]:80 default_server;


    location / {
        # Your site configuration

    # Other configuration...

Here, the first server block matches requests to and redirects them (with a 301 - Permanent Redirect) to The second server block is the one that actually handles requests to

Remember to replace ‘’ with your own domain name.

After making changes, don’t forget to test your configuration with sudo nginx -t and if everything is okay, reload your Nginx configuration with sudo service nginx reload or sudo systemctl reload nginx, depending on your system.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel