Question

Rails app on Nginx too err to many redirects

Posted November 11, 2021 92 views
Ruby on RailsNginx

I am trying to install an ruby on rails app using nginx, passenger and a digital ocean web server.

But when I try to open the webiste, i get a too many redirects error: i think the error is to be found in my nginx configuration file.

Could someone help me to solve it?

server {

  if ($host = partecipativa.online) {
    return 301 https://$host$request_uri;
  } # managed by Certbot

    listen 80;
   listen [::]:80 ipv6only=on;

  server_name partecipativa.online;
 return 404; # managed by Certbot


 }


server {

    server_name partecipativa.online;
    client_max_body_size 32M;

    passenger_enabled on;
    passenger_ruby /home/decidim/.rbenv/versions/2.7.1/bin/ruby;

    rails_env    production;
    root         /home/decidim/decidim-app/public;

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/partecipativa.online-0001/fullchain.pem; # manage>    ssl_certificate_key /etc/letsencrypt/live/partecipativa.online-0001/privkey.pem; # mana>    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi @robywars,

First I think the server block that listens to port 80 should be like that:

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

Of course where you see mysite.com change it to your actual domain.

If that doesn’t help, then I would recommend checking your Ruby application for some possible redirects. What I see when I curl your domain is, I reach the domain with HTTP, get redirected to HTTPS and then I’m constantly being redirected to:

Location: https://partecipativa.online/system/   
Location: https://partecipativa.online/system/admins/sign_in
Location: https://partecipativa.online/system/

and so on, which leads me to believe it’s highly possible it’s the Ruby App that is causing the issues.

  • Hi,

    you can comment the entry

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

    to check if HTTP site is ok and discard Nginx mismatch configuration.

    As say @KFSys , you need to be sure that Rails application is ok, check for logs, and “redirect” statements…

    tail -f log/production.log | grep redirect
    

    (Y)

    • Changing the block unfortunately didn’t solve the problem.

      So i used the tail -f log/production.log | grep redirect

      IT gives me this:

      I, [2021-11-13T02:16:19.229260 #1628]  INFO -- : [3f6d428f-fb8e-4012-b74e-bd22034685f0] Filter chain halted as :verify_organization rendered or redirected
      I, [2021-11-13T02:22:55.115050 #2681]  INFO -- : [225dbfd0-3082-4ce4-9a3a-da6682f9b24d] Filter chain halted as :verify_organization rendered or redirected
      I, [2021-11-13T02:22:56.695121 #2681]  INFO -- : [86b59e3a-512e-447d-9e02-408845a48355] Filter chain halted as :verify_organization rendered or redirected
      I, [2021-11-13T02:23:00.531166 #2681]  INFO -- : [186df519-472b-4a9c-8173-56865e81177a] Filter chain halted as :verify_organization rendered or redirected
      I, [2021-11-13T02:23:03.982896 #2681]  INFO -- : [f23cb3b4-b04e-4291-bf95-150630ae2440] Filter chain halted as :verify_organization rendered or redirected
      I, [2021-11-13T02:23:08.720298 #2681]  INFO -- : [fdf46cc8-bf83-4388-996a-43a456bce06a] Filter chain halted as :verify_organization rendered or redirected
      

      and it goes on and on and on in loop.
      What should I do?

      • Hi @robywars ,

        it seems that a before_action is called on startup…

        check for these in Rails.root directory and sub-directories

        cd my-awesome-rails-app
        fgrep -r 'verify_organization' .
        

        almost sure that must be in controllers/application_controller.rb.

        is your rails app in any repository ready to clone? I can help you a bit ;)

        But, sure that the problem is in your rails app… Are you using vendor plugins? what’s the Gemfile content? complete log file? in development mode all it’s right?

        Another approach is to build a new, single, empty rails application to test Nginx behavior…

        Please, tell me about what you do…

        A huge ;)

        • Hi @robywars,

          I’m more versed in the System side of things so I would suggest giving a look at @hejeroaz’s answer but as it has been confirmed it’s related to your App rather than Nginx.

          Please do let us know once you are able to resolve the issue.