Skip to content

Commit c6f69b9

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

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/bci_build/package/appcontainers.py

Lines changed: 10 additions & 1 deletion
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 []),
@@ -298,8 +300,15 @@ def _get_nginx_kwargs(os_version: OsVersion):
298300
COPY index.html /srv/www/htdocs/
299301
{DOCKERFILE_RUN} chmod +x /docker-entrypoint.d/*.sh /usr/local/bin/docker-entrypoint.sh
300302
{DOCKERFILE_RUN} install -d -o nginx -g nginx -m 750 /var/log/nginx; \
303+
install -d /var/cache/nginx /var/run/nginx; \
301304
ln -sf /dev/stdout /var/log/nginx/access.log; \
302-
ln -sf /dev/stderr /var/log/nginx/error.log
305+
ln -sf /dev/stderr /var/log/nginx/error.log; \
306+
chown -R nginx:nginx /var/cache/nginx; \
307+
chown -R nginx:nginx /etc/nginx; \
308+
chown -R nginx:nginx /var/run/nginx; \
309+
install -d -o nginx -g nginx /tmp/client_temp /tmp/proxy_temp /tmp/fastcgi_temp /tmp/uwsgi_temp /tmp/scgi_temp; \
310+
chown -R nginx:nginx /tmp; \
311+
chmod -R g+w /var/cache/nginx /var/log/nginx /etc/nginx /var/run/nginx /tmp
303312
STOPSIGNAL SIGQUIT"""),
304313
}
305314

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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,23 @@ if [ "$1" = "nginx" ] || [ "$1" = "nginx-debug" ]; then
4444
fi
4545
fi
4646

47+
# Ensure PID path is set to /var/run/nginx.pid for both privileged and unprivileged users
48+
sed -i 's,^#\?\s*pid\s\+.*;$,pid /var/run/nginx/nginx.pid;,' /etc/nginx/nginx.conf
49+
# modify temp paths for both privileged and unprivileged users
50+
sed -i "/^http {/a \ proxy_temp_path /tmp/proxy_temp;\n client_body_temp_path /tmp/client_temp;\n fastcgi_temp_path /tmp/fastcgi_temp;\n uwsgi_temp_path /tmp/uwsgi_temp;\n scgi_temp_path /tmp/scgi_temp;\n" /etc/nginx/nginx.conf
51+
52+
CURRENT_UID=$(id -u)
53+
if [ "$CURRENT_UID" -gt "0" ]; then
54+
# Running as Unprivileged User
55+
entrypoint_log "$0: Running as unprivileged user (UID: $CURRENT_UID). Configuring for unprivileged mode (Port 8080)."
56+
57+
# Remove 'user' directive (unprivileged users can't switch users)
58+
sed -i '/^user/d' /etc/nginx/nginx.conf
59+
entrypoint_log "$0: Removed 'user' directive for unprivileged worker."
60+
61+
sed -i 's/listen \(.*\)80;/listen \18080;/' /etc/nginx/conf.d/default.conf 2>/dev/null || \
62+
sed -i 's/listen \(.*\)80;/listen \18080;/' /etc/nginx/nginx.conf 2>/dev/null || true
63+
entrypoint_log "$0: Listening on port 8080."
64+
fi
65+
4766
exec "$@"

0 commit comments

Comments
 (0)