Score:0

how to get www site to automatically redirect to non-www

in flag

I'm trying to automatically redirect this site to non-www , I have setup a https certificate through certbot. At the moment I have 2 seperate domains one with www and one without, if a user is logged into the site without 'www' they have to login again when removing 'www' from the address, not sure how to config nginx and certbot, appreciate any help with this, I'd like to have both domains available but for the www.soundpro.city to redirect to soundpro.city.

nginx/sites-available/myconfig


server {
server_name 18.169.114.2 soundpro.city www.soundpro.city;


location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/ubuntu/djangoapp1;
}

location /media/ {
root /home/ubuntu/djangoapp1;
}

location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}







    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/soundpro.city/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/soundpro.city/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 = www.soundpro.city) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


server_name  www.soundpro.city;
    listen 80;
    return 404; # managed by Certbot


}

this is my nginx.conf file :

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # 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 TLSv1.3; # 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;
        client_max_body_size 100M;

        ##
        # Gzip Settings
        ##

        gzip on;

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # 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/*;
}
Ivan Shatsky avatar
gr flag
For [example](https://stackoverflow.com/questions/60969930/nginx-generic-redirect-all-www-to-non-www).
milo2810 avatar
in flag
thanks for that , the 2 server blocks in your answer solved my problem
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.