2017-07-25 69 views
0

我有一個nginx多個服務器名稱,並需要重定向到https特定的服務器名稱。Nginx規則重定向一個特定的服務器名稱

隨着我的配置(如下圖),我得到了錯誤ERR_TOO_MANY_REDIRECTS

我的conf:

# /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; 
    } 
} 

任何人都可以幫我解決這個問題嗎?

+0

使用一個單獨的'server'塊。 –

回答

0

我覺得你這樣做很複雜。第一:If is evil :)

當我看到你想要做的幾件事情:

  1. 刪除www 「前綴」
  2. /rio/lisboa:使用特定的主機
  3. contoso.comcontoso.sapo.pt:使用特定目錄
  4. contoso.sapo.pt:force 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

+0

這是好想法:) 如果是邪惡的,但在位置,但邪惡rsrsrs,我會嘗試它,並在這裏發佈修復。 Thks you !! –