Score:1

Nginx too many redirect when trying to redirect non www to www

cn flag

I have this nginx configuration

upstream puma_example.it {
  server unix:/home/deploy/apps/example.it/shared/tmp/sockets/example.it-puma.sock fail_timeout=0;
}
server {
  listen 80;
  listen [::]:80;
  server_name example.it www.example.it;
  return 301 https://www.example.it$request_uri;
}

server {
server_name blog.example.com;
return 301 http://www.example.com/blog$request_uri;
}


server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/example.it/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.it/privkey.pem;
  server_name www.example.it example.it;
  root /home/deploy/apps/example.it/current/public;
  try_files $uri/index.html $uri @puma_example.it;
  return 301 https://www.example.com$request_uri; 

  client_max_body_size 4G;
  keepalive_timeout 10;

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

  location @puma_example.it {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    ssi on;
    proxy_set_header X-Forwarded-Proto https;
    proxy_pass http://puma_example.it;
    # limit_req zone=one;
    access_log /home/deploy/apps/example.it/shared/log/nginx.access.log;
    error_log /home/deploy/apps/example.it/shared/log/nginx.error.log;
  }

  location ^~ /images/ {
    expires max;
    add_header Cache-Control public;
  }

  location ~ ^/(assets|packs)/ {
    gzip_static on;
    brotli_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.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;
  }
}

I'm trying to redirect non www to www and to redirect the third party domain blog to a specific url but I think that adding

return 301 https://www.example.com$request_uri;

in that position it cause a redirect loop. Where do I have to place it? What is wrong in this config?

I try also to separate blocks

upstream puma_example.it {
  server unix:/home/deploy/apps/example.it/shared/tmp/sockets/example.it-puma.sock fail_timeout=0;
}

server {
  listen 80;
  listen [::]:80;
  server_name example.it www.example.it;
  return 301 https://www.example.it$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/example.it/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.it/privkey.pem;
  server_name example.it;
  return 301 https://www.example.it$request_uri;
}

server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/example.it/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.it/privkey.pem;
  server_name hinnovation.example.it;
  return 301 https://www.example.it/speciale/innovation$request_uri;
}


server {
  listen 443 ssl http2;
  listen [::]:443 ssl http2;
  ssl_certificate /etc/letsencrypt/live/example.it/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/example.it/privkey.pem;
  server_name www.example.it;
  root /home/deploy/apps/example.it/current/public;
  try_files $uri/index.html $uri @puma_example.it;

  client_max_body_size 4G;
  keepalive_timeout 10;

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

  location @puma_example.it {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_redirect off;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    ssi on;
    proxy_set_header X-Forwarded-Proto https;
    proxy_pass http://puma_example.it;
    # limit_req zone=one;
    access_log /home/deploy/apps/example.it/shared/log/nginx.access.log;
    error_log /home/deploy/apps/example.it/shared/log/nginx.error.log;
  }

  location ^~ /images/ {
    expires max;
    add_header Cache-Control public;
  }

  location ~ ^/(assets|packs)/ {
    gzip_static on;
    brotli_static on;
    expires max;
    add_header Cache-Control public;
  }

  location = /50x.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;
  }
}
Score:1
us flag

You need to have separate server blocks for your www and non-www domains.

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate ...
    ssl_certificate_key ...
    server_name example.it;

    return 301 https://www.example.com$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    ssl_certificate ...
    ssl_certificate_key ...
    server_name www.example.it;

    ... rest of configuration ...
}
cn flag
I edit, but it doesn't work. The third party redirect works, the non www to www works
us flag
Sorry, you wrote above that both "works". What is the issue then?
cn flag
Ah sorry. The non www to www doesn’t works
us flag
Please add output of `curl -v <url>` to the original question, where `<url>` is the HTTP URL you try. That way we can see what exactly happens.
cn flag
the website is https://dday.it
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.