简体   繁体   中英

Nginx, Unicorn, net::ERR_TOO_MANY_REDIRECTS

When I try to visit my rails app on Digital Ocean Server, I get the following error in the browser: redirected you too many times. In console: net::ERR_TOO_MANY_REDIRECTS

At this point capistrano deploys successfully with no errors. The app runs perfect locally (no errors) there either.

Here are other relevant files below starting with error log files for nginx, unicorn, and rails, and also shown below that the configuration files for nginx and unicorn.

/etc/defaults/unicorn

CONFIGURED=yes
TIMEOUT=60
APP_ROOT=/home/rails/rails_project/current
CONFIG_RB=/etc/unicorn.conf
PID=/var/run/unicorn.pid
RAILS_ENV="production"
UNICORN_OPTS="-D -c $CONFIG_RB -E $RAILS_ENV"
PATH=/home/rails/.rvm/gems/ruby-2.2.2/bin:/home/rails/.rvm/gems/ruby-2.2.2@global/bin$
export GEM_HOME=/home/rails/.rvm/gems/ruby-2.2.2
export GEM_PATH=/home/rails/.rvm/gems/ruby-2.2.2:/home/rails/.rvm/gems/ruby-2.2.2@glo$
export HOME=/home/rails
DAEMON=/home/rails/.rvm/gems/ruby-2.2.2/bin/unicorn

/var/log/unicorn/unicorn.log

I, [2016-05-13T22:26:59.271424 #11584] INFO -- : reaped #<Process::Status: pid 11587 exit 0> worker=0
I, [2016-05-13T22:26:59.272911 #11584] INFO -- : reaped #<Process::Status: pid 11590 exit 0> worker=1
I, [2016-05-13T22:26:59.273219 #11584] INFO -- : reaped #<Process::Status: pid 11592 exit 0> worker=2
I, [2016-05-13T22:26:59.273573 #11584] INFO -- : reaped #<Process::Status: pid 11595 exit 0> worker=3
I, [2016-05-13T22:26:59.274209 #11584] INFO -- : master complete
I, [2016-05-13T22:27:00.664033 #12489] INFO -- : unlinking existing socket=/var/run/unicorn.sock
I, [2016-05-13T22:27:00.664570 #12489] INFO -- : listening on addr=/var/run/unicorn.sock fd=10
I, [2016-05-13T22:27:00.668023 #12489] INFO -- : worker=0 spawning...
I, [2016-05-13T22:27:00.669161 #12489] INFO -- : worker=1 spawning...
I, [2016-05-13T22:27:00.670383 #12492] INFO -- : worker=0 spawned pid=12492
I, [2016-05-13T22:27:00.671245 #12489] INFO -- : worker=2 spawning...
I, [2016-05-13T22:27:00.676617 #12489] INFO -- : worker=3 spawning...
I, [2016-05-13T22:27:00.680864 #12495] INFO -- : worker=1 spawned pid=12495
I, [2016-05-13T22:27:00.681924 #12489] INFO -- : master process ready
I, [2016-05-13T22:27:00.689107 #12497] INFO -- : worker=2 spawned pid=12497
I, [2016-05-13T22:27:00.696755 #12500] INFO -- : worker=3 spawned pid=12500
I, [2016-05-13T22:27:00.802486 #12492] INFO -- : Refreshing Gem list
I, [2016-05-13T22:27:00.804209 #12500] INFO -- : Refreshing Gem list
I, [2016-05-13T22:27:00.807876 #12497] INFO -- : Refreshing Gem list
I, [2016-05-13T22:27:00.811879 #12495] INFO -- : Refreshing Gem list
I, [2016-05-13T22:27:10.403242 #12500] INFO -- : worker=3 ready
I, [2016-05-13T22:27:10.404469 #12497] INFO -- : worker=2 ready
I, [2016-05-13T22:27:10.406947 #12495] INFO -- : worker=1 ready
I, [2016-05-13T22:27:10.407474 #12492] INFO -- : worker=0 ready

/var/log/nginx/error.log

2016/05/13 21:00:18 [error] 31654#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 148.75.53.23, server: codepajamas.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "codepajamas.com"
2016/05/13 21:05:33 [error] 31654#0: *5 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 148.75.53.23, server: codepajamas.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "codepajamas.com"
2016/05/13 21:19:52 [error] 31654#0: *8 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 180.76.15.29, server: codepajamas.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "www.codepajamas.com"
2016/05/13 21:21:40 [error] 31654#0: *12 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 148.75.53.23, server: codepajamas.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "codepajamas.com"
2016/05/13 21:30:33 [error] 31654#0: *16 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 148.75.53.23, server: codepajamas.com, request: "GET / HTTP/1.1", upstream: "http://unix:/var/run/unicorn.sock/", host: "codepajamas.com"

/home/rails/rails_project/current/log/production.log

(empty)

/etc/unicorn.conf

listen "unix:/var/run/unicorn.sock"
worker_processes 4
user "rails"
working_directory "/home/rails/rails_project/current"
pid "/var/run/unicorn.pid"
stderr_path "/var/log/unicorn/unicorn.log"
stdout_path "/var/log/unicorn/unicorn.log"

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
  worker_connections 768;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;
  gzip_disable "msie6";

  text/xml application/xml application/xml+rss text/javascript;

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/rails

upstream app_server {
  server unix:/var/run/unicorn.sock fail_timeout=0;
}

server {
  listen 443 ssl;
  server_name <mydomain>.com www.<mydomain>.com;
  ssl_certificate /etc/letsencrypt/live/<mydomain>.com/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/<mydomain>.com/privkey.pem;
  root /home/rails/rails_project/current/public;
  index index.htm index.html;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_dhparam /etc/ssl/certs/dhparam.pem;
  ssl_ciphers <removed don't want you to see>
  ssl_session_timeout 1d;
  ssl_session_cache shared:SSL:50m;
  ssl_stapling on;
  ssl_stapling_verify on;
  add_header Strict-Transport-Security max-age=15768000;

  location / {
    try_files $uri/index.html $uri.html $uri @app;
  }

  location ~* ^.+\.  (jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|mp3|flv|mpeg|avi)$ {
try_files $uri @app;
  }

  location @app {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://app_server;
  }

  location ~ /.well-known {
    allow all;
    root /usr/share/nginx/html;
  }
}

server {
  # redirect HTTP to HTTPS
  listen 80;
  server_name <mydomain>.com www.<mydomain>.com;
  return 301 https://$host$request_uri;
}

Any help is much appreciated.

Okay so, after a lot of digging I discovered the error message was trying to hint to me that I was forcing HTTPS and getting into some kind of redirect loop. I simply had to comment out a line in config/environments/production.rb

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
# config.force_ssl = true

This was commented out by default, I must have uncommented it. Have to remember when something breaks that was working previously check your git diff to see all the changes you made. It appears both the rails app and nginx were both trying to force ssl this created a redirect loop somehow not certain. These other posts were also helpful:

Why am I getting infinite redirect loop with force_ssl in my Rails app?

Nginx configuration leads to endless redirect loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM