Last active
October 12, 2023 20:39
-
-
Save asterite3/89236d1753a669e173531aca4b87afdc to your computer and use it in GitHub Desktop.
Serve current directory with nginx. As a regular (non-root) user (a replacement for python -m SimpleHTTPServer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Usage: | |
# ./nginx.sh | |
# ./nginx.sh 8888 | |
# ./nginx.sh 0.0.0.0 8080 | |
set -e | |
HOST="127.0.0.1" | |
PORT="8000" | |
if [ ! -z "$1" ] ; then | |
if [ -z "$2" ] ; then | |
PORT="$1" | |
else | |
HOST="$1" | |
PORT="$2" | |
fi | |
fi | |
echo Serving $PWD in $HOST:$PORT | |
CONFIG_PATH=`mktemp` | |
PID_FILE=`mktemp` | |
exec 3<"$CONFIG_PATH" | |
exec 4>"$PID_FILE" | |
rm "$PID_FILE" | |
if [ -f "/etc/nginx/mime.types" ]; then | |
MIME_TYPES_INCLUDE="include /etc/nginx/mime.types;" | |
fi | |
cat << EOF > "$CONFIG_PATH" | |
daemon off; | |
worker_processes auto; | |
pid /dev/fd/4; | |
error_log /dev/stderr; | |
events {} | |
http { | |
$MIME_TYPES_INCLUDE | |
access_log /dev/stdout; | |
server { | |
listen $HOST:$PORT; | |
location / { | |
autoindex on; | |
autoindex_exact_size off; | |
root .; | |
} | |
} | |
} | |
EOF | |
rm "$CONFIG_PATH" | |
exec nginx -p . -c "/dev/fd/3" |
Also tested on nginx/1.16.1 in Fedora 30 docker and Arch Linux docker, nginx/1.17.3 in CentOS 7 docker.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Tested on nginx 1.4.6 (Ubuntu) and 1.14.0 (Ubuntu).
As a one-liner: https://gist.github.com/asterite3/4b9159b8bfcdf9ad8def88168d28b60e