¿Cómo redirijo subdominios al dominio raíz en Nginx en CentOS?

¿Cómo redirijo subdominios al dominio raíz en Nginx en CentOS?

Estoy usando Centos con Nginx y Puma. Me gustaría redirigir todos los subdominios a mi dominio raíz principal, así que seguí las instrucciones aquí:https://stackoverflow.com/questions/26801479/nginx-redirect-all-subdomains-to-main-domain. Sin embargo, no puedo hacerlo funcionar. A continuación se muestra mi configuración.

upstream projecta {
  server unix:///home/rails/projecta_production/shared/sockets/puma.sock;
}

server {
  listen 80;
  server_name mydomein.com;
  return 301 http://mydomein.com$request_uri;
  root /home/rails/projecta_production/public; # I assume your app is located at this location

  location / {
    proxy_pass http://projecta; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

Si excluyo el "retorno 301http://mydomein.com$request_uri;" línea entonces mi sitio funcionará en el dominio raíz, pero no en ninguno de los subdominios (por ejemplo, ver un subdominio generará la página de índice predeterminada de Nginx). ¿Cómo redirijo todos los subdominios a mi dominio principal? y conservar mi configuración de Rails/Puma?

Respuesta1

Actualmente está escuchando en el vhost del dominio apex la redirección. Lo que debe hacer es tener un oyente de vhost separado que redirija al vértice. Este es un ejemplo de un oyente comodín que redirige a la definición del dominio principal:

upstream projecta {
  server unix:///home/rails/projecta_production/shared/sockets/puma.sock;
}

# Listener for all subdomains
server {
  listen 80;
  server_name *.mydomein.com;
  # If you want to redirect all requests, not just subdomains, use below config instead.
  # server_name _;
  return 301 http://mydomein.com$request_uri;
}

# Listener for Apex Domain
server {
  listen 80;
  server_name mydomein.com;
  root /home/rails/projecta_production/public; # I assume your app is located at this location

  location / {
    proxy_pass http://projecta; # match the name of upstream directive which is defined above
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

  location ~* ^/assets/ {
    # Per RFC2616 - 1 year maximum expiry
    expires 1y;
    add_header Cache-Control public;

    # Some browsers still send conditional-GET requests if there's a
    # Last-Modified header or an ETag header even if they haven't
    # reached the expiry date sent in the Expires header.
    add_header Last-Modified "";
    add_header ETag "";
    break;
  }
}

información relacionada