【问题标题】:nginx - redirect only main domain to www, not subdomainsnginx - 仅将主域重定向到 www,而不是子域
【发布时间】:2017-10-19 09:49:44
【问题描述】:

我是 Nginx 的新手,并试图弄清楚如何正确处理子域。我想要实现的是主域example.com总是重定向到https://www.example.com,但子域sub.example.com应该总是重定向到https://sub.example.com。在我当前的设置中,第一个要求得到满足,但sub.example.com 总是被重定向到https://www.sub.example.com。我的配置有什么问题,我该如何解决?

在此先感谢您,费边。


我的两个服务器配置文件:

默认

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://www.$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name example.com;
    ssl_certificate /path/on/my/server/to/certificate.pem;
    ssl_certificate_key /path/on/my/server/to/privatekey.pem;
    return 301 https://www.$host$request_uri;
}


server {
    listen 443 default_server ssl http2;
    listen [::]:443 default_server ssl http2;
    server_name www.example.com;
    ssl_certificate /path/on/my/server/to/certificate.pem;
    ssl_certificate_key /path/on/my/server/to/privatekey.pem;

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

    location / {
        try_files $uri $uri/ =404;
        index index.php index.html index.htm;
    }
}



server {
    listen 80;
    listen [::]:80;
    server_name sub.example.com;
    return 301 https://$host$request_uri;
}

server {

    listen 443 ssl;
    listen [::]:443 ssl;
    server_name sub.example.com;
    ssl_certificate /path/on/my/server/to/subcertificate.pem;
    ssl_certificate_key /path/on/my/server/to/subprivatekey.pem;

    root /var/www/sub;

    location / {
        index index.php index.html index.htm;
        try_files $uri = 404;
    }

    location ~ \.php$ {
        try_files $uri = 404;
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php7-fpm-web1.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

【问题讨论】:

  • 您当前的配置还将www.example.com 重定向到www.www.example.com。您确定正在加载sub 配置文件吗?试试:nginx -tnginx -T
  • 是的,配置文件已经加载,奇怪的是,在我清除浏览器缓存后它现在可以工作了......

标签: http redirect nginx https subdomain


【解决方案1】:

如果其他人也遇到此问题:尝试清理浏览器缓存。

【讨论】:

  • 对我不起作用。它发生在我使用的每个浏览器甚至计算机中......
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 2015-10-16
  • 2018-06-02
  • 2018-06-01
  • 2014-09-23
  • 2020-12-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多