Nginx + HaProxy usage and a gotcha
I’ve recently switched from using a straight up Apache mod_proxy + mongrel setup to Nginx + HaProxy +mongrel. Basically, mod_proxy is dumb as shit, and should never be used with mongrel + rails, especially with better options like Passenger and HaProxy.
Why does mod_proxy suck? Take a look for yourself:
[8030/5/247]: handling 127.0.0.1: GET
[8031/6/134]: handling 127.0.0.1: GET
[8032/2/160]: handling 127.0.0.1: GET
[8033/0/93]: idle
[8034/10/128]: handling 127.0.0.1: GET
[8036/2/124]: handling 127.0.0.1: GET
[8035/0/110]: idle
[8037/0/101]: idle
Look at this garbage. There are three idle mongrels even though the other five have 25 requests to be processed.
Whats great about HaProxy is that it will queue up requests and only distribute them to idle mongrels. It translates into way better response times under load. Add Nginx, which is way easier to configure that Apache, allowing me to easily setup things like sending known ‘slow’ requests to separate mongrels and it’s a big win. As a bonus, haproxy has a great status tool, which gives a quick overview of your proxy clusters.
One little problem …
The gotcha when I initially switched over was that exception_notifier stopped sending me error emails, in fact the errors were being displayed to the user. What the hell?Exception notifier looks at the incoming IP address. If it’s local (aka: 127.0.0.1), it’s assumed that the user is a developer, so it won’t email the exception to you. haproxy was sending nginx’s IP address, rather than the forwarded client address. The problem was that I had configured both nginx and haproxy to forward headers.
haproxy.conf:option forwardfor # enable insert of X-Forwarded-For headers
nginx.conf
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Removing the header forwarding in the haproxy configuration solved the problem. You only need to set the X-Forwarded header in Nginx (or Apache or Pound or whatever front end you choose). Setting it in Haproxy as well will over write the header with one containing the IP of the front end. Since I have Nginx and Haproxy running on the same machine, that IP is 127.0.0.1, which broke the exception_notifier.
Page Author
From Here You Can…
Information
- 673 Views
- 0 Comments
Most Recent Related Content
- Lesson
- Avatar

- Title
- Ruby on Rails实践(4)--- 做一个出来看看
- Body
- 总算到了这激动人心的一步了。 注...
- Author
- Lesson
- Avatar

- Title
- Ruby on Rails实践(6)---用Rails的方式编程
- Body
- scaffold生成程序在 Mybook 目录下打开 DOS 命令行窗口,运行 ruby script\generate model bookrails ...
- Author
- Video
- Avatar

- Title
- Hack Autotest to clear Terminal after update
- Description
- I’ve gotten asked more than once how to clear terminal after autotest u...
- Author
- Lesson
- Avatar

- Title
- Using the Immutable Attribute Plugin
- Body
- Rails surprisingly does not include a validation method to make a model attri...
- Author
- Lesson
- Avatar

- Title
- Action Controller: Rescue
- Body
- Rescue Most likely your application is going to contain bugs or otherwise ...
- Author
- Lesson
- Avatar

- Title
- How to use Fliqz4R
- Body
- About Fliqz Fliqz is the leader in full-service, plug-an...
- Author
- Lesson
- Avatar

- Title
- Ruby on Rails实践(1)---我们需要J2EE吗
- Body
- 如果你作为一个Java 程序员从事j2ee开发的话,你一定会使用到众多应用程序框架。没有任何...
- Author
- Lesson
- Avatar

- Title
- Action Controller: What does a controller do?
- Body
- What does a controller do? Action Controller is the C in MVC. After routin...
- Author
