NGINX将非安全子域重定向到主域

时间:2018-12-12 12:45:28

标签: nginx

我在同一台服务器上托管了2个域,例如testwebsite.comstaging.testwebsite.com。我添加了nginx配置,其中存在一个问题,子域仅在非安全协议上才被重定向到主域。

http://testwebsite.com-> https://testwebsite.com =确定

https://testwebsite.com-> https://testwebsite.com =确定

http://staging.testwebsite.com-> https://testwebsite.com =不好

https://staging.testwebsite.com-> https://staging.testwebsite.com =确定

testwebsite.com

server {
    root /var/www/testwebsite.com/live;
    index index.html index.php index.htm index.nginx-debian.html;
    server_name testwebsite.com www.testwebsite.com;

    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/testwebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/testwebsite.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 {
    listen 80;
    listen [::]:80;

    server_name testwebsite.com www.testwebsite.com;
    return 301 https://testwebsite.com$request_uri;

}

staging.testwebiste.com

server {
    root /var/www/testwebsite.com/staging;
    index index.html index.php index.htm index.nginx-debian.html;
    server_name staging.testwebsite.com www.staging.testwebsite.com;
    location / {
        try_files $uri $uri/ =404;
    }
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }
    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/staging.testwebsite.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/staging.testwebsite.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 {
    listen 80;
    listen [::]:80;

    server_name staging.testwebsite.com www.staging.testwebsite.com;
    return 301 https://staging.testwebsite.com$request_uri;

}

任何人都可以帮助解决配置问题吗?

1 个答案:

答案 0 :(得分:1)

配置对我来说还可以。

您确定不是您的浏览器在缓存重定向吗?浏览器倾向于非常积极地缓存301重定向。