Skip to content

Instantly share code, notes, and snippets.

@asterite3
Last active October 12, 2023 20:39
Show Gist options
  • Save asterite3/89236d1753a669e173531aca4b87afdc to your computer and use it in GitHub Desktop.
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)
#!/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"
@asterite3
Copy link
Author

asterite3 commented Mar 6, 2019

Tested on nginx 1.4.6 (Ubuntu) and 1.14.0 (Ubuntu).

As a one-liner: https://gist.github.com/asterite3/4b9159b8bfcdf9ad8def88168d28b60e

@asterite3
Copy link
Author

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