public
Description: Run rails applications reliably as runit services
Homepage: http://www.sanityinc.com/
Clone URL: git://github.com/purcell/rails-runit.git
Click here to lend your support to: rails-runit and make a donation at www.pledgie.com !
rails-runit / nginx-run
100755 102 lines (78 sloc) 2.357 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/bin/sh -e
 
PORT=$(basename $(pwd)|awk -F- '{print $2}')
HAPROXY_PORT=$(basename $(pwd)|awk -F- '{print $3}')
USER=$(id -un)
BASE=$(dirname $(readlink $0))
APP_DIR=$($BASE/readlink_canonical $BASE/app)
 
if [ -e $BASE/nginx ]; then
NGINX=$BASE/nginx
else
NGINX=nginx
fi
 
TEMP_BASE_DIR=/var/tmp/${USER}-nginx-${PORT}
if [ ! -d $TEMP_BASE_DIR ]; then
mkdir -p $TEMP_BASE_DIR;
  chmod og-rwx $TEMP_BASE_DIR;
fi
 
cat <<EOF > nginx.conf
### DO NOT EDIT - THIS IS REGENERATED WHEN NGINX STARTS: SEE $0 ###
daemon off;
 
pid ${TEMP_BASE_DIR}/pid; # we don't want this, but it's mandatory
 
worker_processes 2;
 
error_log ${APP_DIR}/log/nginx-errors.log;
 
events {
worker_connections 1024;
}
 
http {
access_log ${APP_DIR}/log/access.log;
 
fastcgi_temp_path ${TEMP_BASE_DIR}/fastcgi;
client_body_temp_path ${TEMP_BASE_DIR}/client-body 1 2;
proxy_temp_path ${TEMP_BASE_DIR}/proxy;
 
include mime.types;
default_type application/octet-stream;
 
sendfile on;
 
tcp_nopush on;
keepalive_timeout 65;
tcp_nodelay on;
 
gzip on;
gzip_min_length 1100;
gzip_buffers 4 8k;
gzip_proxied any;
gzip_types text/plain text/html text/xhtml text/css text/js text/csv;
 
upstream haproxy {
server 127.0.0.1:$HAPROXY_PORT;
}
 
server {
listen 127.0.0.1:${PORT};
#server_name example.com;
 
root ${APP_DIR}/public;
index index.html index.htm;
 
location / {
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header Host \$http_host;
proxy_redirect false;
if (-f \$request_filename/index.html) {
rewrite (.*) \$1/index.html break;
}
 
if (-f \$request_filename.html) {
rewrite (.*) \$1.html break;
}
 
# Can be used by upstream caches (e.g. varnish) if desired
add_header X-FromFile yes;
if (!-f \$request_filename) {
proxy_pass http://haproxy;
add_header X-FromFile no;
break;
}
}
 
# For sendfile headers, e.g. "X-Accel-Redirect: /RAILS_ROOT/data/photos/123"
location /RAILS_ROOT/ {
internal;
root ${APP_DIR};
}
 
error_page 500 502 503 504 /500.html;
}
}
EOF
 
exec $NGINX -c nginx.conf || echo "do you need to create the symlink '$BASE/nginx?'"