Skip to content

Commit 915c6d9

Browse files
committed
try to nginxify
1 parent 81c7ee6 commit 915c6d9

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

Dockerfile.cabotage

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FROM python:3.9-bullseye
2+
COPY --from=ewdurbin/nginx-static:1.25.x /usr/bin/nginx /usr/bin/nginx
23
ENV PYTHONUNBUFFERED=1
34
ENV PYTHONDONTWRITEBYTECODE=1
45
RUN mkdir /code

Procfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
release: python manage.py migrate --noinput
2-
web: gunicorn -c gunicorn.conf pydotorg.wsgi
2+
web: bin/start-nginx gunicorn -c gunicorn.conf pydotorg.wsgi

bin/start-nginx

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/usr/bin/env bash
2+
3+
psmgr=/tmp/nginx-buildpack-wait
4+
rm -f $psmgr
5+
mkfifo $psmgr
6+
7+
n=1
8+
while getopts :f option ${@:1:2}
9+
do
10+
case "${option}"
11+
in
12+
f) FORCE=$OPTIND; n=$((n+1));;
13+
esac
14+
done
15+
16+
# Initialize log directory.
17+
mkdir -p logs/nginx
18+
touch logs/nginx/access.log logs/nginx/error.log
19+
echo 'buildpack=nginx at=logs-initialized'
20+
21+
# Start log redirection.
22+
(
23+
# Redirect nginx logs to stdout.
24+
tail -qF -n 0 logs/nginx/*.log
25+
echo 'logs' >$psmgr
26+
) &
27+
28+
# Start App Server
29+
(
30+
# Take the command passed to this bin and start it.
31+
# E.g. bin/start-nginx bundle exec unicorn -c config/unicorn.rb
32+
COMMAND=${@:$n}
33+
echo "buildpack=nginx at=start-app cmd=$COMMAND"
34+
$COMMAND
35+
echo 'app' >$psmgr
36+
) &
37+
38+
if [[ -z "$FORCE" ]]
39+
then
40+
FILE="/tmp/app-initialized"
41+
42+
# We block on app-initialized so that when nginx binds to $PORT
43+
# are app is ready for traffic.
44+
while [[ ! -f "$FILE" ]]
45+
do
46+
echo 'buildpack=nginx at=app-initialization'
47+
sleep 1
48+
done
49+
echo 'buildpack=nginx at=app-initialized'
50+
fi
51+
52+
# Start nginx
53+
(
54+
# We expect nginx to run in foreground.
55+
# We also expect a socket to be at /tmp/nginx.socket.
56+
echo 'buildpack=nginx at=nginx-start'
57+
/usr/bin/nginx -p . -c config/nginx.conf
58+
echo 'nginx' >$psmgr
59+
) &
60+
61+
# This read will block the process waiting on a msg to be put into the fifo.
62+
# If any of the processes defined above should exit,
63+
# a msg will be put into the fifo causing the read operation
64+
# to un-block. The process putting the msg into the fifo
65+
# will use it's process name as a msg so that we can print the offending
66+
# process to stdout.
67+
read exit_process <$psmgr
68+
echo "buildpack=nginx at=exit process=$exit_process"
69+
exit 1

config/nginx.conf.erb config/nginx.conf

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
daemon off;
2-
#Heroku dynos have at least 4 cores.
3-
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;
2+
worker_processes 2;
43

54
events {
65
use epoll;
@@ -33,7 +32,7 @@ http {
3332
}
3433

3534
server {
36-
listen <%= ENV["PORT"] %>;
35+
listen unix:/var/run/cabotage/cabotage.sock;
3736
server_name _;
3837
keepalive_timeout 5;
3938

gunicorn.conf

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
bind = 'unix:/var/run/cabotage/cabotage.sock'
1+
bind = 'unix:/tmp/nginx.sock'
22
backlog = 1024
33
preload_app = True
44
max_requests = 2048

0 commit comments

Comments
 (0)