0

Im new to all this, but how do i redirect my from www to a non-www. I have tried multiple ways to fix it in NGINX but no mater how i change it, there is still a www and an non-www site. The payment gateway is redirected to a non-www website after a transaction.

server {
    server_name example.com www.example.com;
    location / {
    proxy_pass http://123.0.0.1:5000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
     }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/dineshudayan.tech/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/dineshudayan.tech/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name  www.example.com;
    return 404; # managed by Certbot

   


}

server {
    listen 80;
    server_name admin.example.com www.admin.example.com;
    location / {
        proxy_pass http://123.0.0.1:8000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
     }
}

1 Answers1

1

@richard-smith is right; here's a fully worked example with some comments:

# Your default server - assuming DNS is set up correctly
# will serve http & https requests for any *.example.com
# hosts and redirect to them to https://example.com
server {
    listen  80 default_server;
    listen  443 ssl default_server;

    server_name www.example.com;

    ssl_certificate /etc/letsencrypt/live/dineshudayan.tech/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dineshudayan.tech/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # redirect all requests to https://example.com
    return  301 https://example.com$request_uri;
}

# http server for example.com
# - will redirect requests to https://example.com
server {
    listen  80;

    server_name example.com;

    # redirect all requests to https://example.com
    return  301 https://example.com$request_uri;
}

# Your example.com https server
server {
    listen  443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/dineshudayan.tech/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/dineshudayan.tech/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
}
jaygooby
  • 2,436
  • 24
  • 42