nginx reescreve sub-domínios específicos

1

Estou tentando reescrever qualquer subdomínio fora de uma lista aprovada, por exemplo, (dev, test, home) .domain.com

aqui está o que estou trabalhando agora:

#rewrite all sub domains
 if ($host ~* "^([^.]+(\.[^.]+)*)\.domain.com$"){
        rewrite ^(.*)$ http://domain.com$1 permanent;
        break;
    }

o problema é que reescreve todos os sub-domínios. Eu tenho uma solução no apache, mas estou tendo problemas para convertê-lo. versão do apache:

RewriteCond %{HTTP_HOST} !^(dev|test|local\.blog)\.domain\.me$ [NC]

Atualizações:

server {
    listen 80;
  server_name *.domain.com domain.com;
  root /srv/www/www.domain.com;
  access_log /srv/logs/www_domain.com.access;
  error_log /srv/logs/www_domain.com.error error;

  error_page 400 401 402 403 404 500 502 503 504 /error.htm;
  location  /error.htm {
    internal;
  }
  # ------------------------------------------------------------------------  
  # remove all sub domains and www
  if ($host !~* (dev|test|prod)\.domain\.com) {
            rewrite ^ http://domain.com$request_uri permanent;
    }
  # ------------------------------------------------------------------------
  location / {
    index index.html index.php index.htm;
    location ~* ^.*\.php$ {
      include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      }
  }
  # ------------------------------------------------------------------------
  # serve favicon
  location = /favicon.ico { 
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # process robots.txt
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # serve static files directly
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
    # ------------------------------------------------------------------------
  # block access to .ht files
  location ~ /\.ht {
    deny  all;
  }
  # ------------------------------------------------------------------------
    # short cuts
    if ($uri ~* "/login") {
        rewrite ^/login(/.*)? /wp-admin$1;
    }
    # ------------------------------------------------------------------------
    # BEGIN W3TC Browser Cache
    gzip on;
    gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain    text/xsd text/xsl text/xml image/x-icon;
    location ~ \.(css|js)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
            expires 180s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    # END W3TC Browser Cache
    # BEGIN W3TC Skip 404 error handling by WordPress for static files
    if (-f $request_filename) {
            break;
    }
    if (-d $request_filename) {
            break;
    }
    if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") {
            break;
    }
    if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
            return 404;
    }
    # END W3TC Skip 404 error handling by WordPress for static files
    # ------------------------------------------------------------------------
}
    
por Chris Hough 28.09.2011 / 05:08

4 respostas

1

com base na resposta do @AlexD Atualizei minha solução. Se você tiver alguma sugestão de outros geeks, me avise.

server {
  listen 80;
  server_name *.noconformity.co;
  rewrite ^ http://noconformity.co$request_uri? permanent;
}
server {
  listen 80;
  server_name noconformity.co ~(sub1|sub2)\.noconformity\.co$;
  root /srv/www/www.noconformity.co;
  access_log /var/log/nginx-hosts/www_noconformity.co.access;
  error_log /var/log/nginx-hosts/www_noconformity.co.error error;

  error_page 400 401 402 403 404 500 502 503 504 /error.htm;
  location  /error.htm {
    internal;
  }
  # ------------------------------------------------------------------------
  location / {
    index index.html index.php index.htm;
    location ~* ^.*\.php$ {
      include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      }
  }
  # ------------------------------------------------------------------------
  # serve favicon
  location = /favicon.ico { 
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # process robots.txt
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # serve static files directly
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
    # ------------------------------------------------------------------------
  # block access to .ht files
  location ~ /\.ht {
    deny  all;
  }
  # ------------------------------------------------------------------------
    # short cuts
    if ($uri ~* "/login") {
        rewrite ^/login(/.*)? /wp-admin$1;
    }
    # ------------------------------------------------------------------------
    # BEGIN W3TC Browser Cache
    gzip on;
    gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
    location ~ \.(css|js)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
            expires 180s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    # END W3TC Browser Cache
    # BEGIN W3TC Skip 404 error handling by WordPress for static files
    if (-f $request_filename) {
            break;
    }
    if (-d $request_filename) {
            break;
    }
    if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") {
            break;
    }
    if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
            return 404;
    }
    # END W3TC Skip 404 error handling by WordPress for static files
    # ------------------------------------------------------------------------
}

este é o site padrão:

server {
  listen 80 default_server;
  server_name localhost;
  root /srv/www/www.default.site;
  access_log /var/log/nginx-hosts/default.access;
  error_log /var/log/nginx-hosts/default.error error;

  error_page 400 401 402 403 404 500 502 503 504 /error.htm;
  location  /error.htm {
    internal;
  }

  location / {
    index index.html index.php index.htm;
    location ~* ^.*\.php$ {
        if (!-f $document_root/$fastcgi_script_name){
            return 404;
      }
      include        fastcgi_params;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
    }
  }
}
    
por 01.10.2011 / 02:15
1

Verifique a documentação do nginx como converter as regras de reescrita do apache em nginx.

server {
  listen 80;
  server_name domain.com ~(dev|test|local)\.domain\.com$;

  # ... your rules here ... 

}

server {
  listen 80 default_server;
  server_name *.domain.com;
  rewrite ^ http://domain.com$request_uri? permanent;
}
    
por 28.09.2011 / 08:04
0

Parece que você deve usar look-behind negativo :

server {
    listen 80;
    server_name *.domain.com;
    ...

    if ($host ~* (?<!(dev|test|local))\.domain\.com) {
        rewrite ^ http://domain.com$request_uri permanent;
    }

}
    
por 28.09.2011 / 05:29
0

depois de localizar uma ferramenta matadora: link Eu consegui reconstruir minha configuração niganx vhost para funcionar corretamente, no entanto, estou aberto a sugestões, modificações. Este é um servidor multi-host e um arquivo host individual em sites disponíveis para cada host

server {
  listen 80;
  server_name *.domain.com domain.com;
  root /srv/www/www.domain.com;
  access_log /srv/logs/www_domain.com.access;
  error_log /srv/logs/www_domain.com.error error;

  error_page 400 401 402 403 404 500 502 503 504 /error.htm;
  location  /error.htm {
    internal;
  }
  # ------------------------------------------------------------------------
  #RewriteCond %{HTTP_HOST} !^(dev|test|prod)\.domain\.com$ [NC]
  #RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$
  #RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
  # all sub domains and www
    if ($http_host !~* "^(dev|test|prod)\.domain\.com$"){
        set $rule_0 1$rule_0;
    }
    if ($http_host ~ "^([^.]+)\.domain\.com$"){
        set $rule_0 2$rule_0;
    }
    if ($rule_0 = "21"){
        rewrite ^/(.*)$ http://domain.com/$1 permanent;
        break;
    }
  # ------------------------------------------------------------------------
  location / {
    index index.html index.php index.htm;
    location ~* ^.*\.php$ {
      include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      }
  }
  # ------------------------------------------------------------------------
  # serve favicon
  location = /favicon.ico { 
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # process robots.txt
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
    # ------------------------------------------------------------------------
  # serve static files directly
  location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
    access_log off;
    expires max;
  }
    # ------------------------------------------------------------------------
  # block access to .ht files
  location ~ /\.ht {
    deny  all;
  }
  # ------------------------------------------------------------------------
    # short cuts
    if ($uri ~* "/login") {
        rewrite ^/login(/.*)? /wp-admin$1;
    }
    # ------------------------------------------------------------------------
    # BEGIN W3TC Browser Cache
    gzip on;
    gzip_types text/css application/x-javascript text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
    location ~ \.(css|js)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
            expires 180s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=180, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
            expires 31536000s;
            add_header Pragma "public";
            add_header Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate";
            add_header X-Powered-By "W3 Total Cache/0.9.2.3";
    }
    # END W3TC Browser Cache
    # BEGIN W3TC Skip 404 error handling by WordPress for static files
    if (-f $request_filename) {
            break;
    }
    if (-d $request_filename) {
            break;
    }
    if ($request_uri ~ "(robots\.txt|sitemap(_index|[0-9]+)?\.xml(\.gz)?)") {
            break;
    }
    if ($request_uri ~* \.(css|js|html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|swf|tar|tif|tiff|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$) {
            return 404;
    }
    # END W3TC Skip 404 error handling by WordPress for static files
    # ------------------------------------------------------------------------
}

configuração do servidor padrão:

server {
  listen 80 default;
  server_name localhost;
  root /srv/www/www.default.site;
  access_log /srv/logs/localhost.access;
  error_log /srv/logs/localhost.error error;

  error_page 400 401 402 403 404 500 502 503 504 /error.htm;
  location  /error.htm {
    internal;
  }

  location / {
    index index.html index.php index.htm;
    location ~* ^.*\.php$ {
        if (!-f $document_root/$fastcgi_script_name){
            return 404;
      }
      include        fastcgi_params;
      fastcgi_pass   127.0.0.1:9000;
      fastcgi_index  index.php;
      fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      fastcgi_intercept_errors on;
    }
  }
}
    
por 28.09.2011 / 16:03