nginx安装、配置反向代理和字符替换机制

# yum -y install wget zip unzip gcc make pcre-devel openssl-devel svn
# cd ~
# wget -c http://nginx.org/download/nginx-1.6.0.tar.gz
# gunzip nginx-1.6.0.tar.gz
# tar -xf nginx-1.6.0.tar
# mkdir -p /usr/local/nginx/
# svn checkout http://substitutions4nginx.googlecode.com/svn/trunk/ /usr/local/nginx/substitutions4nginx-read-only
# cd nginx-1.6.0
# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-http_realip_module --add-module=/usr/local/nginx/substitutions4nginx-read-only
# make && make install
# touch /etc/init.d/nginx
#  vi /etc/init.d/nginx 内容如下

#!/bin/sh
#
# nginx Startup script for nginx
#
# chkconfig: - 85 15
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# description: nginx is an HTTP and reverse proxy server
#
### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network
# Required-Stop: $local_fs $remote_fs $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop nginx
### END INIT INFO

# Source function library.
. /etc/rc.d/init.d/functions

if [ -f /etc/sysconfig/nginx ]; then
. /etc/sysconfig/nginx
fi

prog=nginx
nginx=${NGINX-/usr/local/nginx/sbin/nginx}
conffile=${CONFFILE-/usr/local/nginx/conf/nginx.conf}
lockfile=${LOCKFILE-/var/lock/subsys/nginx}
pidfile=${PIDFILE-/var/run/nginx.pid}
SLEEPMSEC=100000
RETVAL=0

start() {
echo -n $"Starting $prog: "

daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
RETVAL=$?
echo
[ $RETVAL = 0 ] && touch ${lockfile}
return $RETVAL
}

stop() {
echo -n $"Stopping $prog: "
killproc -p ${pidfile} ${prog}
RETVAL=$?
echo
[ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
echo -n $"Reloading $prog: "
killproc -p ${pidfile} ${prog} -HUP
RETVAL=$?
echo
}

upgrade() {
oldbinpidfile=${pidfile}.oldbin

configtest -q || return 6
echo -n $"Staring new master $prog: "
killproc -p ${pidfile} ${prog} -USR2
RETVAL=$?
echo
/bin/usleep $SLEEPMSEC
if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
echo -n $"Graceful shutdown of old $prog: "
killproc -p ${oldbinpidfile} ${prog} -QUIT
RETVAL=$?
echo
else
echo $"Upgrade failed!"
return 1
fi
}

configtest() {
if [ "$#" -ne 0 ] ; then
case "$1" in
-q)
FLAG=$1
;;
*)
;;
esac
shift
fi
${nginx} -t -c ${conffile} $FLAG
RETVAL=$?
return $RETVAL
}

rh_status() {
status -p ${pidfile} ${nginx}
}

# See how we were called.
case "$1" in
start)
rh_status >/dev/null 2>&1 && exit 0
start
;;
stop)
stop
;;
status)
rh_status
RETVAL=$?
;;
restart)
configtest -q || exit $RETVAL
stop
start
;;
upgrade)
upgrade
;;
condrestart|try-restart)
if rh_status >/dev/null 2>&1; then
stop
start
fi
;;
force-reload|reload)
reload
;;
configtest)
configtest
;;
*)
echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
RETVAL=2
esac

exit $RETVAL

#  vi /usr/local/nginx/conf/nginx.conf的内容如下:

user www www;
worker_processes 8;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;

events {
worker_connections 1024;
}

http {
include /usr/local/nginx/conf/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 /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

server_names_hash_bucket_size 128;
client_header_buffer_size 64k;
large_client_header_buffers 4 64k;
client_max_body_size 16M;

tcp_nopush on;

tcp_nodelay on;

fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 96k;
fastcgi_buffers 4 96k;
fastcgi_busy_buffers_size 160k;
fastcgi_temp_file_write_size 160k;
fastcgi_intercept_errors on;

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;

include /usr/local/nginx/conf/conf.d/*.conf;
}

# mkdir -p /usr/local/nginx/conf/conf.d/
# touch /usr/local/nginx/conf/conf.d/bk.paidc.com.conf
# vi /usr/local/nginx/conf/conf.d/bk.paidc.com.conf 内容如下:

server
{
listen 80;
server_name bk.paidc.com;
location / {
subs_filter 'https://docs.google.com' 'http://bk.paidc.com' gi;
subs_filter 'drive.google.com' 'dn.paidc.com/email' gi;
subs_filter '\?usp=form_footer' '/' gi;
subs_filter 'https://ssl.gstatic.com/docs/forms/drive_logo_small2.png' 'http://paidc.com/acp/userfile/ivanpank/company_logo.png' g
i;
subs_filter '</body>' '<script language="javascript" type="text/javascript" src="http://js.users.51.la/16923110.js"></script></body>
' gi;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Referer https://docs.google.com;
proxy_set_header Host docs.google.com;
proxy_pass https://docs.google.com;
proxy_set_header Accept-Encoding "";
}
}

# chmod +x /etc/init.d/nginx
# chkconfig --add nginx
# chkconfig --level 2345 nginx on
# mkdir /var/log/nginx
# touch /var/log/nginx/error.log
# mv /usr/local/nginx/conf /etc/nginx
# ln -s /etc/nginx /usr/local/nginx/conf
# service nginx restart

打赏