Skip to content

Commit 4a7376b

Browse files
authored
[ruby/rails] Use nginx for Pitchfork (#10203)
Pitchfork relies on a buffering reverse proxy to efficiently deal with slow clients. +---------------+-------------------------+-------+ | name| branch_name|fortune| +---------------+-------------------------+-------+ |rails-pitchfork| master| 7359| |rails-pitchfork| rails/nginx-pitchfork| 12162| +---------------+-------------------------+-------+
1 parent 1290aa7 commit 4a7376b

File tree

3 files changed

+163
-0
lines changed

3 files changed

+163
-0
lines changed
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
# This is example contains the bare mininum to get nginx going with
2+
# Unicorn or Rainbows! servers. Generally these configuration settings
3+
# are applicable to other HTTP application servers (and not just Ruby
4+
# ones), so if you have one working well for proxying another app
5+
# server, feel free to continue using it.
6+
#
7+
# The only setting we feel strongly about is the fail_timeout=0
8+
# directive in the "upstream" block. max_fails=0 also has the same
9+
# effect as fail_timeout=0 for current versions of nginx and may be
10+
# used in its place.
11+
#
12+
# Users are strongly encouraged to refer to nginx documentation for more
13+
# details and search for other example configs.
14+
15+
# you generally only need one nginx worker unless you're serving
16+
# large amounts of static files which require blocking disk reads
17+
worker_processes 8;
18+
19+
# # drop privileges, root is needed on most systems for binding to port 80
20+
# # (or anything < 1024). Capability-based security may be available for
21+
# # your system and worth checking out so you won't need to be root to
22+
# # start nginx to bind on 80
23+
# user nobody nogroup; # for systems with a "nogroup"
24+
#user nobody nobody; # for systems with "nobody" as a group instead
25+
26+
# Feel free to change all paths to suite your needs here, of course
27+
pid /tmp/nginx.pid;
28+
#error_log /tmp/nginx.error.log;
29+
error_log stderr error;
30+
31+
events {
32+
worker_connections 4096; # increase if you have lots of clients
33+
accept_mutex off; # "on" if nginx worker_processes > 1
34+
use epoll; # enable for Linux 2.6+
35+
# use kqueue; # enable for FreeBSD, OSX
36+
}
37+
38+
http {
39+
# nginx will find this file in the config directory set at nginx build time
40+
include /etc/nginx/mime.types;
41+
42+
# fallback in case we can't determine a type
43+
default_type application/octet-stream;
44+
45+
# click tracking!
46+
#access_log /tmp/nginx.access.log combined;
47+
access_log off;
48+
49+
# you generally want to serve static files with nginx since neither
50+
# Unicorn nor Rainbows! is optimized for it at the moment
51+
sendfile on;
52+
53+
tcp_nopush on; # off may be better for *some* Comet/long-poll stuff
54+
tcp_nodelay off; # on may be better for some Comet/long-poll stuff
55+
56+
# we haven't checked to see if Rack::Deflate on the app server is
57+
# faster or not than doing compression via nginx. It's easier
58+
# to configure it all in one place here for static files and also
59+
# to disable gzip for clients who don't get gzip/deflate right.
60+
# There are other gzip settings that may be needed used to deal with
61+
# bad clients out there, see http://wiki.nginx.org/NginxHttpGzipModule
62+
#gzip on;
63+
#gzip_http_version 1.0;
64+
#gzip_proxied any;
65+
#gzip_min_length 500;
66+
#gzip_disable "MSIE [1-6]\.";
67+
#gzip_types text/plain text/html text/xml text/css
68+
# text/comma-separated-values
69+
# text/javascript application/x-javascript
70+
# application/atom+xml;
71+
72+
# this can be any application server, not just Unicorn/Rainbows!
73+
upstream app_server {
74+
# fail_timeout=0 means we always retry an upstream even if it failed
75+
# to return a good HTTP response (in case the Unicorn master nukes a
76+
# single worker for timing out).
77+
78+
# for UNIX domain socket setups:
79+
server unix:/tmp/.sock fail_timeout=0;
80+
81+
# for TCP setups, point these to your backend servers
82+
# server 192.168.0.7:8080 fail_timeout=0;
83+
# server 192.168.0.8:8080 fail_timeout=0;
84+
# server 192.168.0.9:8080 fail_timeout=0;
85+
}
86+
87+
server {
88+
# enable one of the following if you're on Linux or FreeBSD
89+
listen 8080 default deferred; # for Linux
90+
# listen 80 default accept_filter=httpready; # for FreeBSD
91+
92+
# If you have IPv6, you'll likely want to have two separate listeners.
93+
# One on IPv4 only (the default), and another on IPv6 only instead
94+
# of a single dual-stack listener. A dual-stack listener will make
95+
# for ugly IPv4 addresses in $remote_addr (e.g ":ffff:10.0.0.1"
96+
# instead of just "10.0.0.1") and potentially trigger bugs in
97+
# some software.
98+
# listen [::]:80 ipv6only=on; # deferred or accept_filter recommended
99+
100+
client_max_body_size 4G;
101+
server_name _;
102+
103+
# ~2 seconds is often enough for most folks to parse HTML/CSS and
104+
# retrieve needed images/icons/frames, connections are cheap in
105+
# nginx so increasing this is generally safe...
106+
keepalive_timeout 10;
107+
108+
# path for static files
109+
root /path/to/app/current/public;
110+
111+
# Prefer to serve static files directly from nginx to avoid unnecessary
112+
# data copies from the application server.
113+
#
114+
# try_files directive appeared in in nginx 0.7.27 and has stabilized
115+
# over time. Older versions of nginx (e.g. 0.6.x) requires
116+
# "if (!-f $request_filename)" which was less efficient:
117+
# http://bogomips.org/unicorn.git/tree/examples/nginx.conf?id=v3.3.1#n127
118+
try_files $uri/index.html $uri.html $uri @app;
119+
120+
location @app {
121+
# an HTTP header important enough to have its own Wikipedia entry:
122+
# http://en.wikipedia.org/wiki/X-Forwarded-For
123+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
124+
125+
# enable this if you forward HTTPS traffic to unicorn,
126+
# this helps Rack set the proper URL scheme for doing redirects:
127+
# proxy_set_header X-Forwarded-Proto $scheme;
128+
129+
# pass the Host: header from the client right along so redirects
130+
# can be set properly within the Rack application
131+
proxy_set_header Host $http_host;
132+
133+
# we don't want nginx trying to do something clever with
134+
# redirects, we set the Host: header above already.
135+
proxy_redirect off;
136+
137+
# set "proxy_buffering off" *only* for Rainbows! when doing
138+
# Comet/long-poll/streaming. It's also safe to set if you're using
139+
# only serving fast clients with Unicorn + nginx, but not slow
140+
# clients. You normally want nginx to buffer responses to slow
141+
# clients, even with Rails 3.1 streaming because otherwise a slow
142+
# client can become a bottleneck of Unicorn.
143+
#
144+
# The Rack application may also set "X-Accel-Buffering (yes|no)"
145+
# in the response headers do disable/enable buffering on a
146+
# per-response basis.
147+
# proxy_buffering off;
148+
149+
proxy_pass http://app_server;
150+
}
151+
152+
# Rails error pages
153+
error_page 500 502 503 504 /500.html;
154+
location = /500.html {
155+
root /path/to/app/current/public;
156+
}
157+
}
158+
}

frameworks/Ruby/rails/config/pitchfork.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
num_workers, = auto_tune
66

77
worker_processes num_workers
8+
9+
listen "/tmp/.sock", :backlog => 4096

frameworks/Ruby/rails/rails-pitchfork.dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ RUN apt-get update && \
1212
apt-get install -y --no-install-recommends libjemalloc2
1313
ENV LD_PRELOAD=libjemalloc.so.2
1414

15+
RUN apt-get install -yqq nginx
16+
1517
COPY ./Gemfile* /rails/
1618

1719
ENV BUNDLE_FORCE_RUBY_PLATFORM=true
@@ -24,4 +26,5 @@ ENV RAILS_ENV=production_postgresql
2426
ENV PORT=8080
2527
ENV REDIS_URL=redis://localhost:6379/0
2628
CMD service redis-server start && \
29+
nginx -c /rails/config/nginx.conf && \
2730
RACK_ENV=production bundle exec pitchfork -c config/pitchfork.rb

0 commit comments

Comments
 (0)