Skip to content

Instantly share code, notes, and snippets.

@aaronmccall
Created January 4, 2012 00:54
Show Gist options
  • Save aaronmccall/1557845 to your computer and use it in GitHub Desktop.
Save aaronmccall/1557845 to your computer and use it in GitHub Desktop.
Convention-based nginx+django dev environment bootstrapping for Mac OS

Conventions

dev_env.sh

  • Groks nginx installed via MacPorts or Homebrew (/opt/local or /usr/local)
  • Follows the Ubuntu sites-available/sites-enabled convention

runserver.sh

  • Expects top-level project folders to exist in ~/Sites
  • Handles Project/project with a single arg (ie, runserver project)
  • If naming conventions are different, specify both top-level and django project folder. (eg, runserver MyProject myproject)

project.local.conf

  • Static resources will be served by nginx directly if they exist under top-level/httpdocs
  • All requests for URLs that do not match a file under top-level/httpdocs are passed to Django's runserver (http://127.0.0.1:8000)
  • All *.min.js files will cached forever (so don't serve things you change with that naming convention!)
server {
root /Users/Aaron/Sites/Kernel/httpdocs;
server_name kernel.local;
listen 80;
listen 8080;
gzip off;
proxy_read_timeout 600;
location / {
proxy_set_header Host $host:8080;
if (-f $request_filename) {
add_header X-Static hit;
access_log off;
}
if (!-f $request_filename) {
proxy_pass http://127.0.0.1:8000;
add_header X-Static miss;
add_header X-Real-IP $remote_addr;
}
}
location ~.*\.min.js$ {
expires max;
}
}
#!/bin/bash
if [ ! -f "/opt/local/sbin/nginx" ]
then
TOP_LEVEL="/usr/local"
PID="nginx.pid"
else
TOP_LEVEL="/opt/local"
PID="nginx/nginx.pid"
fi
ENABLED="${TOP_LEVEL}/sbin/nginx"
ENABLED="${TOP_LEVEL}/etc/nginx/sites-enabled"
AVAILABLE="${TOP_LEVEL}/etc/nginx/sites-available"
echo "\$1 is $1"
CONF="${1}.local"
if [ -f "${AVAILABLE}/${CONF}" ]
then
# if nginx is running, kill it
if [ -f "${TOP_LEVEL}/var/run/${PID}" ]
then
echo "Stopping nginx..."
sudo kill `cat "${TOP_LEVEL}/var/run/${PID}"`
fi
# iterate the files in sites-enabled and remove any that are not our target config
ls $ENABLED > _conf_list
for i in `cat _conf_list`; do
if [ "${i}" != "${CONF}" ]
then
sudo rm "${ENABLED}/${i}"
fi
done
rm _conf_list
# if our target config is not in sites-enabled, put it in there
if [ ! -f "${ENABLED}/${CONF}" ]
then
sudo ln -s "${AVAILABLE}/${CONF}" "${ENABLED}/${CONF}"
fi
# restart nginx
sudo /opt/local/sbin/nginx
fi
#user Aaron;
worker_processes 1;
#error_log logs/error.log;
error_log /opt/local/etc/nginx/logs/error.log notice;
#error_log logs/error.log info;
pid /opt/local/var/run/nginx/nginx.pid;
events {
worker_connections 64;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /opt/local/etc/nginx/logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
include /opt/local/etc/nginx/conf.d/*.conf;
# here is where project specific config files should be symlinked
include /opt/local/etc/nginx/sites-enabled/*;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root share/nginx/html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers on;
# location / {
# root share/nginx/html;
# index index.html index.htm;
# }
#}
}
#!/bin/bash
if [ $# != 2 ]
then
DJANGO=`echo "$1" | tr "[:upper:]" "[:lower:]"`
CAP=`echo "${DJANGO:0:1}" | tr "[:lower:]" "[:upper:]"`
PROJECT="${CAP}${DJANGO:1}"
else
PROJECT=$1
DJANGO=$2
fi
echo "project is $PROJECT"
echo "django dir is $DJANGO"
dev_env "$DJANGO"
cd "${HOME}/Sites/${PROJECT}/${DJANGO}"
source ../env/bin/activate
./manage.py runserver
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment