Nginx www->非www重定向

时间:2019-03-01 15:33:40

标签: ubuntu nginx certbot

设置证书以将www流量重定向到非www时遵循Certbot的说明,但是我的网站仍同时显示www和非www版本。

网站可用/应用

server {

    server_name mysite.com www.mysite.com;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/myusername/app/app.sock;
    }

    location /static {
        alias /home/myusername/app/app/static;
    }

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

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

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

这是Certbot创建的原始配置,除了添加了static别名

尝试了一些不同的操作,但找不到解决方案,并在进行更改后运行了sudo systemctl reload nginx

服务器为Ubuntu 18.04

谢谢!

0 个答案:

没有答案