sunnyrjuneja
sunnyrjuneja

Reputation: 6123

nginx subdomain redirect catches other subdomains

I'm currently running an application on a subdomain through nginx and require SSL. I implemented redirect that forces all http traffic to https on that subdomain. I noticed that http://www.mydomain.com is being redirected to https://app.mydomain.com. However, visiting http://mydomain.com works fine.

My goal is to have http://www.mydomain.com redirect to http://mydomain.com.

And continuing redirect all http to https traffic for http://app.mydomain.com.

My current nginx configuration looks like this.

server {
  listen   80; ## listen for ipv4
  server_name  www.mydomain.com;
  return 301 http://mydomain.com$request_uri;
}

server {
  listen   80; ## listen for ipv4
  server_name mydomain.com;
  access_log  off;
  location / {
    root   /usr/share/nginx/html/mydomain;
    index  index.html index.htm;
}

My configuration for the application is like this:

server {
    listen *:80;
    server_name app.mydomain.com;
    server_tokens off;
    root /nowhere; # this doesn't have to be a valid path since we are redirecting, you don't have to change it.
    return 301 https://app.mydomain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name app.mydomain.com;
    server_tokens off;
    ...
}

Upvotes: 0

Views: 970

Answers (2)

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

The only reason the app server will take priority if it for some reason matches better, the only difference I see is the listen line, change it from *:80 to 80 to match the rest, and tell me if that works. Also remove the root, it's pointless like you said, don't forget to restart/reload the service.

EDIT: according to you you were using firefox, firefox caches the responses, so if i set server1 to redirect to server2 and I visit server1 from firefox, firefox saves that redirect, next time it would redirect without visiting server1, to solve that you need to clear firefox cache, if you know your first visit was today for example, you can only clear today's cache, I learned this by trials

Upvotes: 1

sunnyrjuneja
sunnyrjuneja

Reputation: 6123

Unfortunately, it looked like the problem was resolved on a completely different level. I was having some issues with DNS cache and once that cleared my problem was resolved.

Upvotes: 0

Related Questions