rpayanm
By:
rpayanm

Unexpected redirect on Nginx

June 23, 2016 940 views
Nginx DNS Debian

Hello,

I have 2 domain and 1 subdomain on 1 droplet (example1.com, example2.com, subdomain.example2.com).

This is my example1.com virtualhost:

#
# Redirect all www to non-www
#
server {
    server_name          www.example1.com;
    ssl_certificate /etc/letsencrypt/live/example1.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example1.com/privkey.pem;

    listen               *:80;
    listen               *:443 ssl spdy;
    listen               [::]:80;
    listen               [::]:443 ssl spdy;

    return 301 https://example1.com$request_uri;
}

#
# Redirect all non-encrypted to encrypted
#
server {
    server_name          example1.com;
    listen               *:80;
    listen               [::]:80;

    return 301 https://example1.com$request_uri;
}

server {
    server_name          example1.com;
    listen               *:443 ssl spdy;
    listen               [::]:443 ssl spdy;

    root /var/www/html/blog;

    ...
}

This is my example2.com virtualhost:

server {
    server_name          example2.com;
    root /var/www/html/example2.com;
    ...
}

This is my subdomain.example2.com virtualhost:

server {
    listen [::]:80;
    server_name subdomain.example2.com;

    root  /var/www/html/subdomain.example2.com;
    ...
}

There are no problem when I browser example1.com and example2.com, but when I go to subdomain.example2.com the browser make a redirect to example1.com

I try clean the browsers cache and nothing.

Greetings.

2 Answers

Maybe you (or a browser plugin) was trying https://subdomain.example2.com and it went to example1 because exmple2 isn't listening on 443. I have a browser plugin that auto changes http to https unless there is an error. And nginx would return the example1 as the best match on 443 rather than an error.

Have another answer? Share your knowledge.