@code33
2018-07-15T11:01:08.000000Z
字数 7365
阅读 1671
nginx 配置文档 技术文档
modify by jyo on 2018-07-15
create by jyo on 2016-04-26
contact code3346#gmail.com
以下配置在centos6.7版本下调试通过
yum updateyum install gcc gcc-c++ autoconf automake
apt-get updateapt-get install gcc g++ autoconf automake
使 tegine 支持 pcel 正则表达式
安装到 /usr/local/pcre 目录
cd /downloadwget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gztar zxvf pcre-8.40.tar.gzcd pcre-8.40./configure --prefix=/usr/local/pcremake && make install
使 tegine 支持 https
安装到 /usr/local/openssl目录
cd /downloadwget http://www.openssl.org/source/openssl-1.0.2.tar.gztar zxvf openssl-1.0.2.tar.gzcd openssl-1.0.2./config --prefix=/usr/local/opensslmake && make install
使tegine 支持 gzip压缩
安装到/usr/local/zlib目录
cd /downloadwget http://zlib.net/zlib-1.2.11.tar.gztar zxvf zlib-1.2.11.tar.gzcd zlib-1.2.11./configure --prefix=/usr/local/zlibmake && make install
内存管理工具 优化 tengine 内存
安装到/usr/local/jemalloc目录
cd /downloadwget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2tar jxvf jemalloc-3.6.0.tar.bz2cd jemalloc-3.6.0./configure --prefix=/usr/local/jemallocmake && make install
用户组及权限增加
groupadd www-datauseradd -s /sbin/nologin -g www-data www-data
也可以先到taobao tengine官网下载
cd /usr/local/srcwget http://tengine.taobao.org/download/tengine-2.2.0.tar.gztar -zxvf tengine-2.2.0.tar.gzcd tengine-2.2.0
可以做成一个 install_nginx.sh 脚本
安装路径/usr/local/nginx
export SOURCEDIR=/download && \./configure --prefix=/usr/local/nginx \--user=www-data \--group=www-data \--with-pcre=${SOURCEDIR}/pcre-8.40 \--with-openssl=${SOURCEDIR}/openssl-1.0.2 \--with-jemalloc=${SOURCEDIR}/jemalloc-3.6.0 \--with-http_gzip_static_module \--with-http_realip_module \--with-http_stub_status_module \--with-http_concat_module \--with-zlib=${SOURCEDIR}/zlib-1.2.11make && make install
tengine安装后的 conf 文件在 /path/conf下面 如果按此配置
需要执行命令复制到 /etc/nginx/conf目录下
cp -rp /path/conf /etc/nginx/
/etc/init.d/nginx
其中要将 /etc/nginx/nginx.conf的pid设置为/var/run/nginx.pid此路径
#!/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/functionsif [ -L $0 ]; theninitscript=`/bin/readlink -f $0`elseinitscript=$0fisysconfig=`/bin/basename $initscript`if [ -f /etc/sysconfig/$sysconfig ]; then. /etc/sysconfig/$sysconfigfinginx=${NGINX-/usr/sbin/nginx}prog=`/bin/basename $nginx`conffile=${CONFFILE-/etc/nginx/nginx.conf}lockfile=${LOCKFILE-/var/lock/subsys/nginx}#进程文件路径定义pidfile=${PIDFILE-/var/run/nginx.pid}SLEEPMSEC=${SLEEPMSEC-200000}UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}RETVAL=0start() {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} -HUPRETVAL=$?echo}upgrade() {oldbinpidfile=${pidfile}.oldbinconfigtest -q || returnecho -n $"Starting new master $prog: "killproc -p ${pidfile} ${prog} -USR2echofor i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do/bin/usleep $SLEEPMSECif [ -f ${oldbinpidfile} -a -f ${pidfile} ]; thenecho -n $"Graceful shutdown of old $prog: "killproc -p ${oldbinpidfile} ${prog} -QUITRETVAL=$?echoreturnfidoneecho $"Upgrade failed!"RETVAL=1}configtest() {if [ "$#" -ne 0 ] ; thencase "$1" in-q)FLAG=$1;;*);;esacshiftfi${nginx} -t -c ${conffile} $FLAGRETVAL=$?return $RETVAL}rh_status() {status -p ${pidfile} ${nginx}}# See how we were called.case "$1" instart)rh_status >/dev/null 2>&1 && exit 0start;;stop)stop;;status)rh_statusRETVAL=$?;;restart)configtest -q || exit $RETVALstopstart;;upgrade)rh_status >/dev/null 2>&1 || exit 0upgrade;;condrestart|try-restart)if rh_status >/dev/null 2>&1; thenstopstartfi;;force-reload|reload)reload;;configtest)configtest;;*)echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"RETVAL=2esacexit $RETVAL
Ubuntu启动脚本
#!/bin/sh## nginx - this script starts and stops the nginx daemon## chkconfig: - 85 15# description: Nginx is an HTTP(S) server, HTTP(S) reverse \# proxy and IMAP/POP3 proxy server# processname: nginx# config: /etc/nginx/nginx.conf# config: /etc/sysconfig/nginx# pidfile: /var/run/nginx.pid# Source function library.. /etc/rc.d/init.d/functions# Source networking configuration.. /etc/sysconfig/network# Check that networking is up.[ "$NETWORKING" = "no" ] && exit 0nginx="/usr/sbin/nginx"prog=$(basename $nginx)sysconfig="/etc/sysconfig/$prog"lockfile="/var/lock/subsys/nginx"pidfile="/var/run/${prog}.pid"NGINX_CONF_FILE="/etc/nginx/nginx.conf"[ -f $sysconfig ] && . $sysconfigstart() {[ -x $nginx ] || exit 5[ -f $NGINX_CONF_FILE ] || exit 6echo -n $"Starting $prog: "daemon $nginx -c $NGINX_CONF_FILEretval=$?echo[ $retval -eq 0 ] && touch $lockfilereturn $retval}stop() {echo -n $"Stopping $prog: "killproc -p $pidfile $progretval=$?echo[ $retval -eq 0 ] && rm -f $lockfilereturn $retval}restart() {configtest_q || return 6stopstart}reload() {configtest_q || return 6echo -n $"Reloading $prog: "killproc -p $pidfile $prog -HUPecho}configtest() {$nginx -t -c $NGINX_CONF_FILE}configtest_q() {$nginx -t -q -c $NGINX_CONF_FILE}rh_status() {status $prog}rh_status_q() {rh_status >/dev/null 2>&1}# Upgrade the binary with no downtime.upgrade() {local oldbin_pidfile="${pidfile}.oldbin"configtest_q || return 6echo -n $"Upgrading $prog: "killproc -p $pidfile $prog -USR2retval=$?sleep 1if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; thenkillproc -p $oldbin_pidfile $prog -QUITsuccess $"$prog online upgrade"echoreturn 0elsefailure $"$prog online upgrade"echoreturn 1fi}# Tell nginx to reopen logsreopen_logs() {configtest_q || return 6echo -n $"Reopening $prog logs: "killproc -p $pidfile $prog -USR1retval=$?echoreturn $retval}case "$1" instart)rh_status_q && exit 0$1;;stop)rh_status_q || exit 0$1;;restart|configtest|reopen_logs)$1;;force-reload|upgrade)rh_status_q || exit 7upgrade;;reload)rh_status_q || exit 7$1;;status|status_q)rh_$1;;condrestart|try-restart)rh_status_q || exit 7restart;;*)echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"exit 2esac
Nginx提供了很多内置的变量,如:
$arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。$args 这个变量等于请求行中的参数。$binary_remote_addr 二进制码形式的客户端地址。$body_bytes_sent 传送页面的字节数$content_length 请求头中的Content-length字段。$content_type 请求头中的Content-Type字段。$cookie_COOKIE cookie COOKIE的值。$document_root 当前请求在root指令中指定的值。$document_uri 与$uri相同。$host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。$is_args 如果$args设置,值为"?",否则为""。$limit_rate 这个变量可以限制连接速率。$nginx_version 当前运行的nginx版本号。$query_string 与$args相同。$remote_addr 客户端的IP地址。$remote_port 客户端的端口。$remote_user 已经经过Auth Basic Module验证的用户名。$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。$request_body_file 客户端请求主体信息的临时文件名。$request_completion 未知。$request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。$scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。$server_name 服务器名称。$server_port 请求到达服务器的端口号。$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。
另外:
HTTP_X_FORWARDED_FOR是透过代理服务器取得客户端的真实IP地址,有些用此方法读取到的仍然是代理服务器的IP。还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 HTTP_X_FORWARDED_FOR 取到的值将是空的。