Skip to content

Instantly share code, notes, and snippets.

@isao
Last active December 10, 2015 21:57
Show Gist options
  • Save isao/322e60af62057dad6e16 to your computer and use it in GitHub Desktop.
Save isao/322e60af62057dad6e16 to your computer and use it in GitHub Desktop.
nginx setup
// This is a proxy config file that echos the requested server ip and port, by
// using nginx SSIs. Generated on: <!--# echo var="date_local" -->
function FindProxyForURL(url, host) {
// These values are derived from the current request's server/port.
var prefix = 'http://' + '<!--# echo var="arg_prefix" default="reachclient" -->';
var proxyhost = '<!--# echo var="server_addr" default="dev.pondr.in" -->';
var proxyport = '<!--# echo var="server_port" default="8000" -->'
if (url.substring(0, prefix.length) === prefix) {
return "PROXY " + proxyhost + ":" + proxyport;
} else {
return "DIRECT";
}
}
gzip on;
gzip_proxied any;
gzip_min_length 1100;
gzip_comp_level 3;
gzip_disable "msie6";
gzip_types
# text/html is always compressed by HttpGzipModule
application/atom+xml
application/javascript
application/json
application/pdf
application/rss+xml
application/vnd.ms-fontobject
application/x-javascript
application/xml
application/xml+rss
font/opentype
font/truetype
image/svg+xml
text/css
text/javascript
text/plain
text/x-component
text/xml;
events {}
http {
server {
listen 80;
server_name reachclient.dev.mr.tv3cloud.com;
root MRGIT/ReachClient/Web/builds/private;
index Landing.html;
location ~ \.ts$ {
root MRGIT/ReachClient/Web;
}
expires off;
add_header X-MEDIAFIRST-DEVELOPER-BUILD 'httpd $document_root';
}
server {
listen 8000;
server_name reachclient.dev.mr.tv3cloud.com;
root MRGIT/ReachClient/Web/builds/private;
index Landing.html;
location ~ \.ts$ {
root MRGIT/ReachClient/Web;
}
location ~ /\w+\.pac$ {
root nginx;
ssi on;
ssi_types text/plain;
}
expires off;
add_header X-MEDIAFIRST-DEVELOPER-BUILD 'proxy $document_root';
proxy_pass_header Accept;
proxy_pass_header Server;
proxy_http_version 1.1;
}
access_log /tmp/nginx-access.log combined;
error_log /tmp/nginx-error.log debug;
include /usr/local/etc/nginx/mime.types;
include gzip.conf;
}
#!/bin/bash -eu
# https://gist.github.com/isao/24ef4278764fd6c5e8ac
#
# functions
#
err() {
echo "error: $2" >&2
exit $1
}
get_this_files_dir()
{
if [[ -h $0 ]]
then
# follow symlink. TODO: follow until !-h
(cd $(dirname $(readlink "$0")) && pwd)
else
(cd "$(dirname $0)" && pwd)
fi
}
init_log_files()
{
for i in $@
do
> $i
chmod a+rw "$i"
done
}
#
# vars
#
# main config to use to start/stop/reload nginx
CONFNAME=nginx.conf
# $0: ./nginx/run.sh
# $_dir: /Users/isao/work/nginx
_DIR_=$(get_this_files_dir $0)
# use the path to this file's parent dir for nginx's --prefix. nginx server root
# paths can be relative to this.
# $prefix: /Users/isao/work
prefix=$(dirname "$_DIR_")
# use the path to this file's dir to find configs. configs included in the
# nginx.conf can be relative to this.
# here
# $config: /Users/isao/work/nginx/nginx.conf
config="$_DIR_/$CONFNAME"
#
# checks
#
[[ -r $config ]] || err 1 "$CONFNAME not found, expected in $thisdir"
[[ $(whoami) = root ]] || err 3 "must run as root"
# check if already running
[[ ! -n $@ ]] && pgrep -f $config >/dev/null && \
err 7 "Already running $config. Did you mean '`basename $0` -s reload'?"
echo "* /etc/hosts \c"
egrep ^127.0.0.1 /etc/hosts | egrep -v 'localhost$' || err 5 'missing host entry'
#
# main
#
# echo this script's shell commands
set -x
init_log_files $(egrep '_log /' "$config" | awk '{print $2}')
# exec nginx, passthru args to script like: -s reload
nginx -p "$prefix" -c "$config" $@
# tail logs
#tail -Fn 0 "$ACCESSLOG" "$ERRORLOG"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment