Skip to content

Instantly share code, notes, and snippets.

@asterite3 asterite3/nginx.sh

Last active May 20, 2020
Embed
What would you like to do?
Bash script to run nginx on command line as regular (non-root) user to serve files from current directory (using current folder as doc-root, 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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link
Owner Author

asterite3 commented Sep 3, 2019

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
You can’t perform that action at this time.