aboutsummaryrefslogtreecommitdiff
path: root/.local/share/bin/nginx-dev
blob: 4e9825d4c60d7a3e838e80566d6244f07d359969 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/bin/sh
folder="$PWD"
port=8080
try_files='/$uri /$uri.html /$uri/index.html =404'
access_log='/dev/stdout'
cache_control='
	add_header Last-Modified $date_gmt;
	add_header Cache-Control "private no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
	if_modified_since off;
	expires off;
	etag off;
'

usage() {
	cat << EOF
usage: $(basename "$0") [options] [folder]

options:
	-p PORT  host server on port PORT (default $port)
	-t STR   set try_files pattern to STR (default '$try_files')
	-C       enable server cache (disabled by default)
	-v       verbose mode (prints config before starting server)
	-q       quiet mode (disable access_log)
	-h       show this help
EOF
exit $1
}

ARGC=0
while getopts hvp:Ct:q OPT; do
	[ $OPTIND -gt $ARGC ] && ARGC=$OPTIND
	case $OPT in
		h) usage 0 ;;
		p) port="$OPTARG" ;;
		t) try_files="$OPTARG" ;;
		v) print_config=1 ;;
		C) cache_control="" ;;
		q) access_log="/dev/null" ;;
		\?|*) usage 1 ;;
	esac
done
shift $(( $OPTIND - 1 ))

[ $# -ge 1 ] && folder="$(readlink -f "$1")"

config="$(mktemp)"
pidfile="$(mktemp)"
cat << EOF > "$config"
worker_processes 1;
daemon off;
pid $pidfile;

events {
	worker_connections 1024;
}

http {
	types_hash_max_size 4096;
	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	$cache_control

	access_log $access_log;

	server {
		listen $port;
		listen [::]:$port;

		root $folder;

		location / {
			try_files $try_files;
		}
	}
}
EOF
[ $print_config ] && cat "$config"
nginx -c "$config"
rm -f "$config" "$pidfile"