简体   繁体   中英

NGINX redirecting non secure subdomain to main domain

I have 2 domain hosted on the same server let's say testwebsite.com and staging.testwebsite.com . I have added the nginx configuration in which there is one problem subdomain is getting redirected to main domain only on non secure protocol.

http://testwebsite.com -> https://testwebsite.com = OK

https://testwebsite.com -> https://testwebsite.com = OK

http://staging.testwebsite.com -> https://testwebsite.com = NOT OK

https://staging.testwebsite.com -> https://staging.testwebsite.com = OK

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;

}

Can anyone please help what went wrong with the config?

The config looks OK to me.

Are you sure that it is not your browser caching the redirect? Browsers tend to cache 301 redirects very aggressively.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM