如何让 www 网站自动重定向到非 www 网站

如何让 www 网站自动重定向到非 www 网站

我正在尝试自动将此网站重定向到非 www ,我已通过 certbot 设置了 https 证书。目前,我有两个独立的域名,一个带有 www,一个不带有 www,如果用户登录到不带“www”的网站,则必须在从地址中删除“www”后再次登录,不确定如何配置 nginx 和 certbot,非常感谢任何帮助,我希望两个域名都可用,但对于www.soundpro.city重定向到 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


}

这是我的 nginx.conf 文件:

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/*;
}

相关内容