Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails redirect fails on nginx & unicorn setup

I have set up my up to run on nginx and unicorn as described in Railscasts episode #293.

When I try to redirect, such as

class PostsController < ApplicationController
  def show
    redirect_to posts_path, :notice => "Test redirect"
  end
end

I get redirected to http://unicorn/posts instead of http://mydomain.com/posts

Here's my nginx.conf for the app

upstream unicorn {
  server unix:/tmp/unicorn.scvrush.sock fail_timeout=0;
}

server {
  listen 80 default deferred;
  # server_name example.com;
  root /var/apps/current/public;  

  try_files $uri/index.html $uri @unicorn;

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

  keepalive_timeout 5;
}
like image 1000
Jakub Arnold Avatar asked Nov 17 '11 09:11

Jakub Arnold


1 Answers

This works for me:

upstream unicorn {
  server unix:/tmp/unicorn.example.sock fail_timeout=0;
}

server {
  listen       80;
  listen       localhost;
  server_name  www.example.com;
  keepalive_timeout 5;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    # this is required for HTTPS:                                                                                                                                                                                                                                             
    # proxy_set_header X-Forwarded-Proto https;
    proxy_set_header Host $http_host;
    proxy_redirect off;
    proxy_pass http://unicorn;
  }

}

and in my ./config/unicorn.rb file:

# Listen on a Unix data socket                                                                                                                                                                                                                                                                                  
listen "/tmp/unicorn.example.sock", :backlog => 64
like image 161
Tilo Avatar answered Sep 23 '22 21:09

Tilo