├── .gitignore ├── README.md └── etc ├── init.d └── nginx-proxy └── nginx.conf /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | .Thumbs 3 | *~ 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # nginx-forward-proxy 2 | 3 | Forward proxy configuration for nginx. 4 | 5 | ## Usage 6 | 7 | ```sh 8 | git clone http://github.com/kawanet/nginx-forward-proxy.git 9 | cd nginx-forward-proxy 10 | etc/init.d/nginx-proxy start 11 | tail -f logs/access.log 12 | ``` 13 | 14 | This will launch a HTTP proxy server at port 3128 per default. 15 | 16 | ## Local Development 17 | 18 | Any requests to hostname with suffix ".devel" will be forwarded to localhost. 19 | 20 | - http://example.com/foo -> http://example.com/foo 21 | - http://example.com.devel/bar -> http://localhost/bar 22 | - http://example.org.devel:3000/buz -> http://localhost:3000/buz 23 | 24 | Edit `etc/nginx.conf` to specify proxy server's port number, local HTTP server's IP address and port number. 25 | 26 | ``` 27 | server { 28 | # proxy server port 29 | listen 3128; 30 | 31 | location / { 32 | # local http server host and port 33 | set $devel_host "127.0.0.1"; 34 | set $devel_port "80"; 35 | ``` 36 | 37 | ## Installing nginx (Mac OS X) 38 | 39 | You need to install nginx before running nginx-forward-proxy. 40 | 41 | ```sh 42 | ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" 43 | brew install nginx 44 | ``` 45 | 46 | ## Author 47 | 48 | @kawanet 49 | -------------------------------------------------------------------------------- /etc/init.d/nginx-proxy: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | nginx=/usr/local/sbin/nginx 4 | logs=logs 5 | pid=logs/nginx.pid 6 | conf=etc/nginx.conf 7 | prog=$(basename $nginx) 8 | cd $(dirname $0)/../.. 9 | path=$(pwd) 10 | 11 | usage() { 12 | echo "Usage: $0 [start|stop|restart|configtest]" 13 | } 14 | 15 | start() { 16 | [ -d $logs ] || mkdir $logs 17 | [ -f $pid ] && stop 18 | /bin/echo -n "starting $prog ... " 19 | $nginx -p $path -c $conf && echo '[OK]' || echo '[NG]' 20 | } 21 | 22 | stop() { 23 | /bin/echo -n "stoping $prog ... " 24 | $nginx -p $path -c $conf -s stop && echo '[OK]' || echo '[NG]' 25 | } 26 | 27 | restart() { 28 | stop 29 | sleep 1 30 | start 31 | } 32 | 33 | configtest() { 34 | $nginx -p $path -c $conf -t 35 | } 36 | 37 | case $1 in 38 | start|stop|restart|configtest) $1;; 39 | *) usage;; 40 | esac 41 | -------------------------------------------------------------------------------- /etc/nginx.conf: -------------------------------------------------------------------------------- 1 | error_log ./logs/error.log; 2 | pid ./logs/nginx.pid; 3 | 4 | events { 5 | worker_connections 1024; 6 | } 7 | 8 | http { 9 | # google's DNS server 10 | resolver 8.8.8.8; 11 | resolver_timeout 5s; 12 | 13 | access_log ./logs/access.log; 14 | proxy_temp_path ./temp; 15 | 16 | server { 17 | # proxy server port 18 | listen 3128; 19 | 20 | location / { 21 | # local http server host and port 22 | set $devel_host "127.0.0.1"; 23 | set $devel_port "80"; 24 | 25 | # proxy (default) 26 | set $proxy_host "$http_host"; 27 | set $url "$scheme://$http_host$request_uri"; 28 | 29 | # http://any.with.suffix.devel/path 30 | if ($host ~* "^(.+)\.devel$") { 31 | set $proxy_host "$1"; 32 | set $url "http://$devel_host:$devel_port$request_uri"; 33 | } 34 | 35 | # http://any.with.suffix.devel:8000/path 36 | if ($http_host ~* "^(.+)\.devel:(\d+)$") { 37 | set $proxy_host "$1:$2"; 38 | set $url "http://$devel_host:$2$request_uri"; 39 | } 40 | 41 | proxy_redirect off; 42 | proxy_set_header Host $proxy_host; 43 | proxy_set_header X-Forwarded-Host $http_host; 44 | proxy_pass "$url"; 45 | } 46 | 47 | # access policy denies global ip addresses 48 | allow 127.0.0.1/32; 49 | allow 192.168.0.0/16; 50 | allow 172.16.0.0/12; 51 | allow 10.0.0.0/8; 52 | deny all; 53 | } 54 | } 55 | --------------------------------------------------------------------------------