Skip to content

Commit 50d77cf

Browse files
committed
Run nginx as unprevileged user
1 parent dba4344 commit 50d77cf

3 files changed

Lines changed: 48 additions & 3 deletions

File tree

src/bci_build/package/appcontainers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ def _get_nginx_kwargs(os_version: OsVersion):
278278
"nginx",
279279
"findutils",
280280
_envsubst_pkg_name(os_version),
281+
"sed",
282+
"grep",
281283
]
282284
)
283285
+ (["libcurl-mini4"] if os_version.is_sl16 else []),
@@ -297,9 +299,10 @@ def _get_nginx_kwargs(os_version: OsVersion):
297299
COPY docker-entrypoint.sh /usr/local/bin
298300
COPY index.html /srv/www/htdocs/
299301
{DOCKERFILE_RUN} chmod +x /docker-entrypoint.d/*.sh /usr/local/bin/docker-entrypoint.sh
300-
{DOCKERFILE_RUN} install -d -o nginx -g nginx -m 750 /var/log/nginx; \
301-
ln -sf /dev/stdout /var/log/nginx/access.log; \
302-
ln -sf /dev/stderr /var/log/nginx/error.log
302+
{DOCKERFILE_RUN} set -euo pipefail; mkdir -p /var/cache/nginx /var/run/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;\
303+
ln -sf /dev/stdout /var/log/nginx/access.log;\
304+
ln -sf /dev/stderr /var/log/nginx/error.log;\
305+
chmod -R 777 /var/cache/nginx /etc/nginx /var/run/nginx /var/log/nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp;
303306
STOPSIGNAL SIGQUIT"""),
304307
}
305308

src/bci_build/package/nginx/README.md.j2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ The template above is then rendered to `/etc/nginx/conf.d/default.conf` as follo
4141
```nginx
4242
listen 80;
4343
```
44+
## Running nginx as a non-root user
45+
To run the image as a less privileged user using the `nginx` user, do the following:
46+
```ShellSession
47+
$ podman run -it --user nginx --rm -p 8080:8080 -v /path/to/html/:/srv/www/htdocs/:Z -v $PWD/nginx.conf:/etc/nginx/nginx.conf:Z {{ image.pretty_reference }}
48+
```
49+
**Note:** When running as the `nginx` user the default port is 8080.
4450

4551
## Environment variables
4652

src/bci_build/package/nginx/docker-entrypoint.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,40 @@ if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
4444
fi
4545
fi
4646

47+
CURRENT_UID=$(id -u)
48+
if [ "$CURRENT_UID" -gt "0" ]; then
49+
# Running as Unprivileged User
50+
entrypoint_log "$0: Running as unprivileged user (UID: $CURRENT_UID). Configuring for unprivileged mode (Port 8080)."
51+
52+
# Define targets
53+
CONF_FILES="/etc/nginx/conf.d/default.conf /etc/nginx/nginx.conf"
54+
55+
for FILE in $CONF_FILES; do
56+
if [ -w "$FILE" ]; then
57+
# Check if it actually contains port 80
58+
if grep -q "listen .*80;" "$FILE"; then
59+
entrypoint_log "Changing port 80 to 8080 in $FILE"
60+
# Use a safe writable subdirectory for the swap file
61+
sed 's/listen\s*80;/listen 8080;/g' "$FILE" > /tmp/client_temp/nginx_swap.conf && \
62+
cat /tmp/client_temp/nginx_swap.conf > "$FILE" && \
63+
rm -f /tmp/client_temp/nginx_swap.conf
64+
fi
65+
66+
# Redirect temp paths to /tmp if we are editing the main nginx.conf
67+
if [ "$FILE" = "/etc/nginx/nginx.conf" ]; then
68+
entrypoint_log "Redirecting NGINX temp paths and setting PID to /tmp in $FILE"
69+
# Use a safe writable subdirectory for the swap file
70+
sed -e '/^user/d' \
71+
-e 's,^#\?\s*pid\s\+.*;$,pid /var/run/nginx/nginx.pid;,' \
72+
-e '/http {/a \ client_body_temp_path /tmp/client_temp;\n proxy_temp_path /tmp/proxy_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;' \
73+
"$FILE" > /tmp/client_temp/nginx_ultra.conf && \
74+
cat /tmp/client_temp/nginx_ultra.conf > "$FILE" && \
75+
rm -f /tmp/client_temp/nginx_ultra.conf
76+
entrypoint_log "$0: Removed 'user' directive and updated PID path."
77+
fi
78+
fi
79+
done
80+
81+
entrypoint_log "$0: Listening on port 8080."
82+
fi
4783
exec "$@"

0 commit comments

Comments
 (0)