简体   繁体   English

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

[英]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 . 我有托管在同一台服务器上2域假设testwebsite.comstaging.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. 我添加了nginx配置,其中存在一个问题,子域仅在非安全协议上才被重定向到主域。

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

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

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

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

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 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. 浏览器倾向于非常积极地缓存301重定向。

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

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