nginx 重定向自website.com到 https://www.website.com

w80xi6nr  于 2023-03-01  发布在  Nginx
关注(0)|答案(1)|浏览(148)

我正在使用nginx托管一个网站,并使用certbot为www.website.com创建了一个HTTPS证书。以下是当前配置:

server {

        root /var/www/website/html;
        index index.html index.htm index.nginx-debian.html;

        server_name website www.website.com website.com;

        location / {
                try_files $uri $uri/ =404;
        }

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

        listen 80;
        listen [::]:80;

        server_name website www.website.com website.com;
    return 404; # managed by Certbot
}

这适用于从www.website.comhttps://www.website.com的重定向。但是,当我尝试连接到website.com时,我得到了404错误。
我想知道如何正确设置重定向,使website.com重定向到https://www.website.com

xdyibdwo

xdyibdwo1#

通过使用rewrite设法解决了这个问题:

server {
    server_name  website.com;
    rewrite ^(.*) https://www.website.com$1 permanent;
}

另外,当我重新启动nginx时,我注意到一些关于nginx.service启动的警告:

nginx.service:

Feb 24 00:42:04 webserver.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Feb 24 00:42:04 webserver.com nginx[3321]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Feb 24 00:42:05 webserver.com nginx[3322]: nginx: [warn] conflicting server name "website.com" on 0.0.0.0:80, ignored
Feb 24 00:42:05 webserver.com systemd[1]: Started The nginx HTTP and reverse proxy server.

我从配置文件的server_name变量中删除了website.com,重新启动,警告消失了。

server {
    server_name  website.com;
    rewrite ^(.*) https://www.website.com$1 permanent;
}

server {

        root /var/www/website/html;
        index index.html index.htm index.nginx-debian.html;

        server_name website www.website.com;

        location / {
                try_files $uri $uri/ =404;
        }

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

        listen 80;
        listen [::]:80;

        server_name website www.website.com;
    return 404; # managed by Certbot
}

相关问题