简体   繁体   English

无法将https://www.example.com重定向到https://example.com

[英]Can't redirect https://www.example.com to https://example.com

I am using Nginx on Ubuntu and am able to redirect www.example.com to https://example.com but not from SSL to SSL with the www subdomain. 我在Ubuntu上使用Nginx,并且能够将www.example.com重定向到https://example.com,但不能将SSL重定向到带有www子域的SSL。

Here is my /etc/nginx/sites-enabled/myapp.conf : 这是我的/etc/nginx/sites-enabled/myapp.conf

# redirect non https traffic for the correct domains
server {
    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80 default_server;
    listen [::]:80 default_server;

    server_name www.example.com;
    return 301 https://example.com$request_uri;
    return 404; # managed by Certbot

}

server {
    listen [::]:443 ssl ipv6only=on;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # server for these domains
    server_name example.com;

    # first try to serve the erb version.
    index index.html;

    # Tell Nginx and Passenger where your app's 'public' directory is
    root /var/www/myapp/code/public;

    # Turn on Passenger
    passenger_enabled on;
    passenger_ruby /usr/local/rvm/gems/ruby-2.5.1/wrappers/ruby;
}

Is there any way to redirect all www subdomain traffic to root domain, even when entering the www URL after https://? 即使在https://之后输入www URL,也可以将所有www子域流量重定向到根域吗?

Thank you, any help is appreciated. 谢谢您的帮助。

Yes, add the following server block: 是的,添加以下服务器块:

# remove 'www'
server {
    listen 80;
    listen 443 ssl;
    server_name www.domain.com;
    return 301 https://example.com$request_uri;
}

This is from the nginx docs . 这来自nginx docs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django如何仅将example.com重定向到https并将* .example.com重定向到http? - Django How to redirect only example.com to https and *.example.com to http? 使www.example.com和example.com使用相同会话变量的最佳方法是什么? - Best way to make www.example.com AND example.com use the same session variables? 网站https://example.com的安全连接失败,但http://example.com的安全连接正常 - Secure Connection Failed for website https://example.com but works fine for http://example.com Nginx将https:// www重定向到https:// - Nginx redirect https://www to https:// 重定向到HTTPS www-Nginx - Redirect to HTTPS www - Nginx 我的域只适用于 https://www.domainname.com 否则 domainname.com 和 www.domainname.com 不起作用 - My domain only works with https://www.domainname.com otherwise domainname.com and www.domainname.com not working Django 为什么以及如何将 HTTP 200OK for example.com 发送到 AWS Scanner - Why and how does Django send HTTP 200OK for example.com to AWS Scanner example.com 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS - example.com redirected you too many times. ERR_TOO_MANY_REDIRECTS 如何使用SSL将example.com/directory指向另一个EC2实例? - How to point example.com/directory to another EC2 instance with SSL? nginx将非www重定向到www和https - nginx Redirect non-www to www and https
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM