Nginx Eliminar WWW Y Responder A Ambos


Tengo el siguiente fragmento de configuración de nginx:

server {
   listen 80;

   server_name mydomain.io;

   root /srv/www/domains/mydomain.io;

   index index.html index.php;

   access_log /var/log/nginx/domains/mydomain.io/access.log;
   error_log /var/log/nginx/domains/mydomain.io/error.log;

   location ~\.php {
      try_files $uri =404;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
      fastcgi_pass 127.0.0.1:9000;
      include /etc/nginx/fastcgi_params;
   }
}

Primero, ¿cómo puedo hacer que el bloque del servidor responda a both http://www.mydomain.io y también http://mydomain.io . Segundo, quiero forzar si vienen de http://www.mydomain.io para redirigir a http://mydomain.io .

Gracias.

Author: Justin, 2012-07-04

6 answers

En la primera pregunta - simplemente agregue ambos dominios:

server_name mydomain.io www.mydomain.io;

Para el segundo, necesitarás esta redirección simple:

server {
      listen 80;

      server_name www.mydomain.io mydomain.io;

      if ($host = 'www.mydomain.io' ) {
         rewrite  ^/(.*)$  http://mydomain.io/$1  permanent;
      }
 -17
Author: Tisho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-04 08:08:44

Según https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#server-name-if, usted debe utilizar:

server {
  server_name www.example.com;
  return 301 $scheme://example.com$request_uri;
}
server {
  server_name example.com;
  # [...]
}
 146
Author: Ryan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-03-04 22:06:38

Creo que es mejor agregar dos bloques de servidor separados para evitar comprobaciones innecesarias por parte del bloque if. También uso la variable scheme scheme para que las solicitudes HTTPS no sean redirigidas a sus contrapartes inseguras.

server {
    listen 80;

    server_name www.mydomain.io;

    rewrite ^ $scheme://mydomain.io$uri permanent;
}

server {
    listen 80;

    server_name mydomain.io;

    # your normal server block definitions here
}
 12
Author: Gerry,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-07 08:49:16

Otra forma de codificarlo :

if ($http_host ~* "^www\.(.+)$"){
    rewrite ^(.*)$ http://%1$request_uri redirect;
}

Funciona incluso con varios nombres de dominio en el mismo código.

 1
Author: lio,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-08 18:34:49

Para un enfoque genérico, sin tener que mencionar ningún dominio o protocolo específico, he utilizado esto con bastante éxito:

  # rewrite to remove www.
  if ( $host ~ ^www\.(.+)$ ) {
    set $without_www $1;
    rewrite ^ $scheme://$without_www$uri permanent;
  }

Esto redirigirá: https://www.api.example.com/person/123?q=45 a https://api.example.com/person/123?q=45

 0
Author: GeezerGeek,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-08-14 14:29:08
server {
    listen 80;
    server_name www.mydomain.io;
    return 301 https://$host$request_uri;
}

server {
    listen 80;
    server_name mydomain.io;
    ...
}
 -1
Author: Manuel Becker,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-23 08:37:54