Skip to content

Commit d28fa23

Browse files
authored
Merge pull request #50 from tiangolo/generate-nginx-from-entrypoint
Generate Nginx configs from entrypoint in a re-usable way and add tests for it.
2 parents a14ce6a + 724cae9 commit d28fa23

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+648
-633
lines changed

python2.7-alpine3.7/Dockerfile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
139139
&& ln -sf /dev/stdout /var/log/nginx/access.log \
140140
&& ln -sf /dev/stderr /var/log/nginx/error.log
141141

142-
COPY nginx.conf /etc/nginx/nginx.conf
143-
144142
# Standard set up Nginx finished
145143

146144
EXPOSE 80
@@ -152,10 +150,6 @@ EXPOSE 443
152150
# Install uWSGI
153151
RUN apk add --no-cache uwsgi-python
154152

155-
# Make NGINX run on the foreground
156-
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
157-
# Copy the modified Nginx conf
158-
COPY nginx-custom.conf /etc/nginx/conf.d/nginx.conf
159153
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
160154
COPY uwsgi.ini /etc/uwsgi/
161155

python2.7-alpine3.7/entrypoint.sh

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
11
#!/usr/bin/env sh
22
set -e
33

4-
# Get the maximum upload file size for Nginx, default to 0: unlimited
5-
USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0}
6-
# Generate Nginx config for maximum upload file size
7-
echo "client_max_body_size $USE_NGINX_MAX_UPLOAD;" > /etc/nginx/conf.d/upload.conf
8-
94
# Explicitly add installed Python packages and uWSGI Python packages to PYTHONPATH
105
# Otherwise uWSGI can't import Flask
116
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages:/usr/lib/python2.7/site-packages
127

8+
# Get the maximum upload file size for Nginx, default to 0: unlimited
9+
USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0}
10+
1311
# Get the number of workers for Nginx, default to 1
1412
USE_NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-1}
15-
# Modify the number of worker processes in Nginx config
16-
sed -i "/worker_processes\s/c\worker_processes ${USE_NGINX_WORKER_PROCESSES};" /etc/nginx/nginx.conf
1713

18-
# Set the max number of connections per worker for Nginx, if requested
14+
# Set the max number of connections per worker for Nginx, if requested
1915
# Cannot exceed worker_rlimit_nofile, see NGINX_WORKER_OPEN_FILES below
20-
if [ -n "$NGINX_WORKER_CONNECTIONS" ] ; then
21-
sed -i "/worker_connections\s/c\ worker_connections ${NGINX_WORKER_CONNECTIONS};" /etc/nginx/nginx.conf
22-
fi
23-
24-
# Set the max number of open file descriptors for Nginx workers, if requested
25-
if [ -n "$NGINX_WORKER_OPEN_FILES" ] ; then
26-
echo "worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};" >> /etc/nginx/nginx.conf
27-
fi
16+
NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-1024}
2817

2918
# Get the listen port for Nginx, default to 80
3019
USE_LISTEN_PORT=${LISTEN_PORT:-80}
31-
# Modify Nginx config for listen port
32-
if ! grep -q "listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf ; then
33-
sed -i -e "/server {/a\ listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf
20+
21+
content='user nginx;\n'
22+
# Set the number of worker processes in Nginx
23+
content=$content"worker_processes ${USE_NGINX_WORKER_PROCESSES};\n"
24+
content=$content'error_log /var/log/nginx/error.log warn;\n'
25+
content=$content'pid /var/run/nginx.pid;\n'
26+
content=$content'events {\n'
27+
content=$content" worker_connections ${NGINX_WORKER_CONNECTIONS};\n"
28+
content=$content'}\n'
29+
content=$content'http {\n'
30+
content=$content' include /etc/nginx/mime.types;\n'
31+
content=$content' default_type application/octet-stream;\n'
32+
content=$content' log_format main '"'\$remote_addr - \$remote_user [\$time_local] \"\$request\" '\n"
33+
content=$content' '"'\$status \$body_bytes_sent \"\$http_referer\" '\n"
34+
content=$content' '"'\"\$http_user_agent\" \"\$http_x_forwarded_for\"';\n"
35+
content=$content' access_log /var/log/nginx/access.log main;\n'
36+
content=$content' sendfile on;\n'
37+
content=$content' keepalive_timeout 65;\n'
38+
content=$content' include /etc/nginx/conf.d/*.conf;\n'
39+
content=$content'}\n'
40+
content=$content'daemon off;\n'
41+
# Set the max number of open file descriptors for Nginx workers, if requested
42+
if [ -n "${NGINX_WORKER_OPEN_FILES}" ] ; then
43+
content=$content"worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};\n"
3444
fi
45+
# Save generated /etc/nginx/nginx.conf
46+
printf "$content" > /etc/nginx/nginx.conf
47+
48+
content_server='server {\n'
49+
content_server=$content_server" listen ${USE_LISTEN_PORT};\n"
50+
content_server=$content_server' location / {\n'
51+
content_server=$content_server' include uwsgi_params;\n'
52+
content_server=$content_server' uwsgi_pass unix:///tmp/uwsgi.sock;\n'
53+
content_server=$content_server' }\n'
54+
content_server=$content_server'}\n'
55+
# Save generated server /etc/nginx/conf.d/nginx.conf
56+
printf "$content_server" > /etc/nginx/conf.d/nginx.conf
57+
58+
# Generate Nginx config for maximum upload file size
59+
printf "client_max_body_size $USE_NGINX_MAX_UPLOAD;\n" > /etc/nginx/conf.d/upload.conf
60+
3561
exec "$@"

python2.7-alpine3.7/nginx-custom.conf

Lines changed: 0 additions & 6 deletions
This file was deleted.

python2.7-alpine3.7/nginx.conf

Lines changed: 0 additions & 31 deletions
This file was deleted.

python2.7-alpine3.8/Dockerfile

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,6 @@ RUN GPG_KEYS=B0F4253373F8F6F510D42178520A9993A1C052F8 \
139139
&& ln -sf /dev/stdout /var/log/nginx/access.log \
140140
&& ln -sf /dev/stderr /var/log/nginx/error.log
141141

142-
COPY nginx.conf /etc/nginx/nginx.conf
143-
144142
# Standard set up Nginx finished
145143

146144
EXPOSE 80
@@ -152,10 +150,6 @@ EXPOSE 443
152150
# Install uWSGI
153151
RUN apk add --no-cache uwsgi-python
154152

155-
# Make NGINX run on the foreground
156-
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
157-
# Copy the modified Nginx conf
158-
COPY nginx-custom.conf /etc/nginx/conf.d/nginx.conf
159153
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
160154
COPY uwsgi.ini /etc/uwsgi/
161155

python2.7-alpine3.8/entrypoint.sh

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,61 @@
11
#!/usr/bin/env sh
22
set -e
33

4-
# Get the maximum upload file size for Nginx, default to 0: unlimited
5-
USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0}
6-
# Generate Nginx config for maximum upload file size
7-
echo "client_max_body_size $USE_NGINX_MAX_UPLOAD;" > /etc/nginx/conf.d/upload.conf
8-
94
# Explicitly add installed Python packages and uWSGI Python packages to PYTHONPATH
105
# Otherwise uWSGI can't import Flask
116
export PYTHONPATH=$PYTHONPATH:/usr/local/lib/python2.7/site-packages:/usr/lib/python2.7/site-packages
127

8+
# Get the maximum upload file size for Nginx, default to 0: unlimited
9+
USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0}
10+
1311
# Get the number of workers for Nginx, default to 1
1412
USE_NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-1}
15-
# Modify the number of worker processes in Nginx config
16-
sed -i "/worker_processes\s/c\worker_processes ${USE_NGINX_WORKER_PROCESSES};" /etc/nginx/nginx.conf
1713

18-
# Set the max number of connections per worker for Nginx, if requested
14+
# Set the max number of connections per worker for Nginx, if requested
1915
# Cannot exceed worker_rlimit_nofile, see NGINX_WORKER_OPEN_FILES below
20-
if [ -n "$NGINX_WORKER_CONNECTIONS" ] ; then
21-
sed -i "/worker_connections\s/c\ worker_connections ${NGINX_WORKER_CONNECTIONS};" /etc/nginx/nginx.conf
22-
fi
23-
24-
# Set the max number of open file descriptors for Nginx workers, if requested
25-
if [ -n "$NGINX_WORKER_OPEN_FILES" ] ; then
26-
echo "worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};" >> /etc/nginx/nginx.conf
27-
fi
16+
NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-1024}
2817

2918
# Get the listen port for Nginx, default to 80
3019
USE_LISTEN_PORT=${LISTEN_PORT:-80}
31-
# Modify Nginx config for listen port
32-
if ! grep -q "listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf ; then
33-
sed -i -e "/server {/a\ listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf
20+
21+
content='user nginx;\n'
22+
# Set the number of worker processes in Nginx
23+
content=$content"worker_processes ${USE_NGINX_WORKER_PROCESSES};\n"
24+
content=$content'error_log /var/log/nginx/error.log warn;\n'
25+
content=$content'pid /var/run/nginx.pid;\n'
26+
content=$content'events {\n'
27+
content=$content" worker_connections ${NGINX_WORKER_CONNECTIONS};\n"
28+
content=$content'}\n'
29+
content=$content'http {\n'
30+
content=$content' include /etc/nginx/mime.types;\n'
31+
content=$content' default_type application/octet-stream;\n'
32+
content=$content' log_format main '"'\$remote_addr - \$remote_user [\$time_local] \"\$request\" '\n"
33+
content=$content' '"'\$status \$body_bytes_sent \"\$http_referer\" '\n"
34+
content=$content' '"'\"\$http_user_agent\" \"\$http_x_forwarded_for\"';\n"
35+
content=$content' access_log /var/log/nginx/access.log main;\n'
36+
content=$content' sendfile on;\n'
37+
content=$content' keepalive_timeout 65;\n'
38+
content=$content' include /etc/nginx/conf.d/*.conf;\n'
39+
content=$content'}\n'
40+
content=$content'daemon off;\n'
41+
# Set the max number of open file descriptors for Nginx workers, if requested
42+
if [ -n "${NGINX_WORKER_OPEN_FILES}" ] ; then
43+
content=$content"worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};\n"
3444
fi
45+
# Save generated /etc/nginx/nginx.conf
46+
printf "$content" > /etc/nginx/nginx.conf
47+
48+
content_server='server {\n'
49+
content_server=$content_server" listen ${USE_LISTEN_PORT};\n"
50+
content_server=$content_server' location / {\n'
51+
content_server=$content_server' include uwsgi_params;\n'
52+
content_server=$content_server' uwsgi_pass unix:///tmp/uwsgi.sock;\n'
53+
content_server=$content_server' }\n'
54+
content_server=$content_server'}\n'
55+
# Save generated server /etc/nginx/conf.d/nginx.conf
56+
printf "$content_server" > /etc/nginx/conf.d/nginx.conf
57+
58+
# Generate Nginx config for maximum upload file size
59+
printf "client_max_body_size $USE_NGINX_MAX_UPLOAD;\n" > /etc/nginx/conf.d/upload.conf
60+
3561
exec "$@"

python2.7-alpine3.8/nginx-custom.conf

Lines changed: 0 additions & 6 deletions
This file was deleted.

python2.7-alpine3.8/nginx.conf

Lines changed: 0 additions & 31 deletions
This file was deleted.

python2.7/Dockerfile

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@ EXPOSE 80
104104
# Expose 443, in case of LTS / HTTPS
105105
EXPOSE 443
106106

107-
# Make NGINX run on the foreground
108-
RUN echo "daemon off;" >> /etc/nginx/nginx.conf
109107
# Remove default configuration from Nginx
110108
RUN rm /etc/nginx/conf.d/default.conf
111-
# Copy the modified Nginx conf
112-
COPY nginx.conf /etc/nginx/conf.d/
113109
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
114110
COPY uwsgi.ini /etc/uwsgi/
115111

python2.7/entrypoint.sh

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,57 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22
set -e
33

44
# Get the maximum upload file size for Nginx, default to 0: unlimited
55
USE_NGINX_MAX_UPLOAD=${NGINX_MAX_UPLOAD:-0}
6-
# Generate Nginx config for maximum upload file size
7-
echo "client_max_body_size $USE_NGINX_MAX_UPLOAD;" > /etc/nginx/conf.d/upload.conf
86

97
# Get the number of workers for Nginx, default to 1
108
USE_NGINX_WORKER_PROCESSES=${NGINX_WORKER_PROCESSES:-1}
11-
# Modify the number of worker processes in Nginx config
12-
sed -i "/worker_processes\s/c\worker_processes ${USE_NGINX_WORKER_PROCESSES};" /etc/nginx/nginx.conf
139

14-
# Set the max number of connections per worker for Nginx, if requested
10+
# Set the max number of connections per worker for Nginx, if requested
1511
# Cannot exceed worker_rlimit_nofile, see NGINX_WORKER_OPEN_FILES below
16-
if [[ -v NGINX_WORKER_CONNECTIONS ]] ; then
17-
sed -i "/worker_connections\s/c\ worker_connections ${NGINX_WORKER_CONNECTIONS};" /etc/nginx/nginx.conf
18-
fi
19-
20-
# Set the max number of open file descriptors for Nginx workers, if requested
21-
if [[ -v NGINX_WORKER_OPEN_FILES ]] ; then
22-
echo "worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};" >> /etc/nginx/nginx.conf
23-
fi
12+
NGINX_WORKER_CONNECTIONS=${NGINX_WORKER_CONNECTIONS:-1024}
2413

2514
# Get the listen port for Nginx, default to 80
2615
USE_LISTEN_PORT=${LISTEN_PORT:-80}
27-
# Modify Nignx config for listen port
28-
if ! grep -q "listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf ; then
29-
sed -i -e "/server {/a\ listen ${USE_LISTEN_PORT};" /etc/nginx/conf.d/nginx.conf
16+
17+
content='user nginx;\n'
18+
# Set the number of worker processes in Nginx
19+
content=$content"worker_processes ${USE_NGINX_WORKER_PROCESSES};\n"
20+
content=$content'error_log /var/log/nginx/error.log warn;\n'
21+
content=$content'pid /var/run/nginx.pid;\n'
22+
content=$content'events {\n'
23+
content=$content" worker_connections ${NGINX_WORKER_CONNECTIONS};\n"
24+
content=$content'}\n'
25+
content=$content'http {\n'
26+
content=$content' include /etc/nginx/mime.types;\n'
27+
content=$content' default_type application/octet-stream;\n'
28+
content=$content' log_format main '"'\$remote_addr - \$remote_user [\$time_local] \"\$request\" '\n"
29+
content=$content' '"'\$status \$body_bytes_sent \"\$http_referer\" '\n"
30+
content=$content' '"'\"\$http_user_agent\" \"\$http_x_forwarded_for\"';\n"
31+
content=$content' access_log /var/log/nginx/access.log main;\n'
32+
content=$content' sendfile on;\n'
33+
content=$content' keepalive_timeout 65;\n'
34+
content=$content' include /etc/nginx/conf.d/*.conf;\n'
35+
content=$content'}\n'
36+
content=$content'daemon off;\n'
37+
# Set the max number of open file descriptors for Nginx workers, if requested
38+
if [ -n "${NGINX_WORKER_OPEN_FILES}" ] ; then
39+
content=$content"worker_rlimit_nofile ${NGINX_WORKER_OPEN_FILES};\n"
3040
fi
41+
# Save generated /etc/nginx/nginx.conf
42+
printf "$content" > /etc/nginx/nginx.conf
43+
44+
content_server='server {\n'
45+
content_server=$content_server" listen ${USE_LISTEN_PORT};\n"
46+
content_server=$content_server' location / {\n'
47+
content_server=$content_server' include uwsgi_params;\n'
48+
content_server=$content_server' uwsgi_pass unix:///tmp/uwsgi.sock;\n'
49+
content_server=$content_server' }\n'
50+
content_server=$content_server'}\n'
51+
# Save generated server /etc/nginx/conf.d/nginx.conf
52+
printf "$content_server" > /etc/nginx/conf.d/nginx.conf
53+
54+
# Generate Nginx config for maximum upload file size
55+
printf "client_max_body_size $USE_NGINX_MAX_UPLOAD;\n" > /etc/nginx/conf.d/upload.conf
56+
3157
exec "$@"

0 commit comments

Comments
 (0)