【问题标题】:Nginx Rule to redirect one specific server_nameNginx 规则重定向一个特定的 server_name
【发布时间】:2017-07-25 16:34:05
【问题描述】:

我有一个带有多个 server_name 的 nginx,需要重定向到 https 一个特定的 server_name。

使用我的配置(如下),我收​​到错误 ERR_TOO_MANY_REDIRECTS

我的会议:

 # /etc/nginx/sites-available/k2cloud_staging

upstream puma_k2cloud_staging {
  server unix:/home/outracoisa/k2cloud/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
  listen 8080;

  client_max_body_size 4G;
  keepalive_timeout 10;

  error_page 500 502 504 /500.html;
  error_page 503 @503;

  server_name contoso.com www.contoso.com contoso.com.br www.contoso.com.br contoso.sapo.pt www.contoso.sapo.pt;
  root /home/outracoisa/k2cloud/current/public;
  try_files $uri/index.html $uri @puma_k2cloud_staging;


  if ($host ~* www\.(.*)) {
    set $host_without_www $1;
    rewrite ^(.*)$ http://$host_without_www$1 permanent;
  }



  rewrite ^/rio/?$ http://contoso.com/rio/pt-BR permanent;
  rewrite ^/lisboa/?$ http://contoso.sapo.pt/lisboa/pt-PT permanent;

  if ($host ~ contoso.com(\.br)) {
    rewrite ^/?$ http://contoso.com/rio/pt-BR permanent;
  }

  if ($host = contoso.sapo.pt) {
    rewrite ^/?$ http://contoso.sapo.pt/lisboa/pt-PT permanent;
  }



  location ~ ^/(rio|lisboa) {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;


    proxy_pass http://puma_k2cloud_staging;
    # limit_req zone=one;
    access_log /home/outracoisa/k2cloud/shared/log/nginx.access.log;
    error_log /home/outracoisa/k2cloud/shared/log/nginx.error.log;
}

########## Ramos #####
 if ($scheme != "https") {
        set $a x;
}

if ($host = contoso.sapo.pt) {
        set $a "${a}y";
}
if ($a = xy) {
    rewrite ^(.*) https://$host$1 permanent;
        break;
        }

################



  location ^~ /assets/ {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.html {
    root html;
  }

  location = /404.html {
    root html;
  }

  location @503 {
    error_page 405 = /system/maintenance.html;
    if (-f $document_root/system/maintenance.html) {
      rewrite ^(.*)$ /system/maintenance.html break;
    }
    rewrite ^(.*)$ /503.html break;
  }

  if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
    return 405;
  }

  if (-f $document_root/system/maintenance.html) {
    return 503;
  }

  location ~ \.(php|html)$ {
    return 405;
  }
}

谁能帮我解决一下?

【问题讨论】:

  • 使用单独的server 块。

标签: regex nginx url-rewriting


【解决方案1】:

我认为你做的很复杂。第一:If is evil :)

我看到你想做几件事:

  1. 删除www“前缀”
  2. /rio/lisboa:使用特定主机
  3. contoso.comcontoso.sapo.pt:使用特定目录
  4. contoso.sapo.pt:强制https

我会换一种方式:

  1. 将另一个server 部分与server_name www.contoso.comserver_name www.contoso.com.brreturn 301 $scheme://... 一起使用。
  2. 试试location /riolocation /lisboa!或者更好的解决方案是使用alias(我认为你的nginx服务器是静态内容,/rio/pt-BR是你服务器上的一个目录)。
  3. 使用server 块!
  4. 再次进入server 块! return 301 https://$host$request_uri;

我认为,如果您创建小块而不仅仅是包含许多 ifrewrite 的大块,那么维护和调试会更容易。

【讨论】:

  • 这是个好主意 :) 如果是邪恶的,但在位置,但邪恶的 rsrsrs,我会尝试并在此处发布修复。谢谢你!!
猜你喜欢
  • 2015-12-22
  • 2011-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 1970-01-01
  • 2016-06-15
  • 2016-05-20
相关资源
最近更新 更多