簡體   English   中英

如何在 Nginx 中使用 https 重定向將 www 重定向到裸域?

[英]How to redirect www to naked domain, with https redirect, in Nginx?

我的網站目前正在將 HTTP 重定向到 HTTPS,但是我希望對 www.example.com/page 的任何請求都重定向到 example.com/page。

理想情況下,我只希望從http://www.example.com重定向到https://example.com ,而不是: http://www.example.com 301 到https://www .example.com 301 到https://example.com

但如有必要,我可以接受兩次重定向。

我試過這個,但沒有用:

/etc/nginx/sites-enabled/example

server { server_name www.exmaple.com; return 301 $scheme://example.com$request_uri; }

這是文件的其余部分:

server {
    server_name 165.62.38.252 example.com www.example.com;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/name/example-main;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/name/example-main/example.sock;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Ce$
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by $
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot


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


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


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




}

server { server_name www.example.com; return 301 $scheme://example.com$request_uri; }

提前感謝您的任何提示。

嘗試從配置文件中刪除最后一行

server { server_name www.example.com; return 301 $scheme://example.com$request_uri; }

然后使用“nginx -t”命令檢查配置並使用“service nginx reload”重新加載配置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM