[关闭]
@code33 2018-07-15T11:01:08.000000Z 字数 7365 阅读 1570

nginx tengine 源码安装 配置(一)

nginx 配置文档 技术文档

modify by jyo on 2018-07-15
create by jyo on 2016-04-26
contact code3346#gmail.com

以下配置在centos6.7版本下调试通过

编译环境支持

  1. yum update
  2. yum install gcc gcc-c++ autoconf automake
  1. apt-get update
  2. apt-get install gcc g++ autoconf automake

编译组件支持

PCRE

使 tegine 支持 pcel 正则表达式
安装到 /usr/local/pcre 目录

  1. cd /download
  2. wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
  3. tar zxvf pcre-8.40.tar.gz
  4. cd pcre-8.40
  5. ./configure --prefix=/usr/local/pcre
  6. make && make install

OpenSSL

使 tegine 支持 https
安装到 /usr/local/openssl目录

  1. cd /download
  2. wget http://www.openssl.org/source/openssl-1.0.2.tar.gz
  3. tar zxvf openssl-1.0.2.tar.gz
  4. cd openssl-1.0.2
  5. ./config --prefix=/usr/local/openssl
  6. make && make install

Zlib

使tegine 支持 gzip压缩
安装到/usr/local/zlib目录

  1. cd /download
  2. wget http://zlib.net/zlib-1.2.11.tar.gz
  3. tar zxvf zlib-1.2.11.tar.gz
  4. cd zlib-1.2.11
  5. ./configure --prefix=/usr/local/zlib
  6. make && make install

jemalloc

内存管理工具 优化 tengine 内存
安装到/usr/local/jemalloc目录

  1. cd /download
  2. wget http://www.canonware.com/download/jemalloc/jemalloc-3.6.0.tar.bz2
  3. tar jxvf jemalloc-3.6.0.tar.bz2
  4. cd jemalloc-3.6.0
  5. ./configure --prefix=/usr/local/jemalloc
  6. make && make install

主角Tengine

用户组及权限增加

  1. groupadd www-data
  2. useradd -s /sbin/nologin -g www-data www-data

也可以先到taobao tengine官网下载

  1. cd /usr/local/src
  2. wget http://tengine.taobao.org/download/tengine-2.2.0.tar.gz
  3. tar -zxvf tengine-2.2.0.tar.gz
  4. cd tengine-2.2.0

可以做成一个 install_nginx.sh 脚本
安装路径/usr/local/nginx

  1. export SOURCEDIR=/download && \
  2. ./configure --prefix=/usr/local/nginx \
  3. --user=www-data \
  4. --group=www-data \
  5. --with-pcre=${SOURCEDIR}/pcre-8.40 \
  6. --with-openssl=${SOURCEDIR}/openssl-1.0.2 \
  7. --with-jemalloc=${SOURCEDIR}/jemalloc-3.6.0 \
  8. --with-http_gzip_static_module \
  9. --with-http_realip_module \
  10. --with-http_stub_status_module \
  11. --with-http_concat_module \
  12. --with-zlib=${SOURCEDIR}/zlib-1.2.11
  13. make && make install

系统服务启动脚本

tengine安装后的 conf 文件在 /path/conf下面 如果按此配置
需要执行命令复制到 /etc/nginx/conf目录下

  1. cp -rp /path/conf /etc/nginx/

/etc/init.d/nginx
其中要将 /etc/nginx/nginx.conf的pid设置为/var/run/nginx.pid此路径

  1. #!/bin/sh
  2. #
  3. # nginx Startup script for nginx
  4. #
  5. # chkconfig: - 85 15
  6. # processname: nginx
  7. # config: /etc/nginx/nginx.conf
  8. # config: /etc/sysconfig/nginx
  9. # pidfile: /var/run/nginx.pid
  10. # description: nginx is an HTTP and reverse proxy server
  11. #
  12. ### BEGIN INIT INFO
  13. # Provides: nginx
  14. # Required-Start: $local_fs $remote_fs $network
  15. # Required-Stop: $local_fs $remote_fs $network
  16. # Default-Start: 2 3 4 5
  17. # Default-Stop: 0 1 6
  18. # Short-Description: start and stop nginx
  19. ### END INIT INFO
  20. # Source function library.
  21. # 执行扩展函数脚本
  22. . /etc/rc.d/init.d/functions
  23. if [ -L $0 ]; then
  24. initscript=`/bin/readlink -f $0`
  25. else
  26. initscript=$0
  27. fi
  28. sysconfig=`/bin/basename $initscript`
  29. if [ -f /etc/sysconfig/$sysconfig ]; then
  30. . /etc/sysconfig/$sysconfig
  31. fi
  32. nginx=${NGINX-/usr/sbin/nginx}
  33. prog=`/bin/basename $nginx`
  34. conffile=${CONFFILE-/etc/nginx/nginx.conf}
  35. lockfile=${LOCKFILE-/var/lock/subsys/nginx}
  36. #进程文件路径定义
  37. pidfile=${PIDFILE-/var/run/nginx.pid}
  38. SLEEPMSEC=${SLEEPMSEC-200000}
  39. UPGRADEWAITLOOPS=${UPGRADEWAITLOOPS-5}
  40. RETVAL=0
  41. start() {
  42. echo -n $"Starting $prog: "
  43. daemon --pidfile=${pidfile} ${nginx} -c ${conffile}
  44. RETVAL=$?
  45. echo
  46. [ $RETVAL = 0 ] && touch ${lockfile}
  47. return $RETVAL
  48. }
  49. stop() {
  50. echo -n $"Stopping $prog: "
  51. killproc -p ${pidfile} ${prog}
  52. RETVAL=$?
  53. echo
  54. [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
  55. }
  56. reload() {
  57. echo -n $"Reloading $prog: "
  58. killproc -p ${pidfile} ${prog} -HUP
  59. RETVAL=$?
  60. echo
  61. }
  62. upgrade() {
  63. oldbinpidfile=${pidfile}.oldbin
  64. configtest -q || return
  65. echo -n $"Starting new master $prog: "
  66. killproc -p ${pidfile} ${prog} -USR2
  67. echo
  68. for i in `/usr/bin/seq $UPGRADEWAITLOOPS`; do
  69. /bin/usleep $SLEEPMSEC
  70. if [ -f ${oldbinpidfile} -a -f ${pidfile} ]; then
  71. echo -n $"Graceful shutdown of old $prog: "
  72. killproc -p ${oldbinpidfile} ${prog} -QUIT
  73. RETVAL=$?
  74. echo
  75. return
  76. fi
  77. done
  78. echo $"Upgrade failed!"
  79. RETVAL=1
  80. }
  81. configtest() {
  82. if [ "$#" -ne 0 ] ; then
  83. case "$1" in
  84. -q)
  85. FLAG=$1
  86. ;;
  87. *)
  88. ;;
  89. esac
  90. shift
  91. fi
  92. ${nginx} -t -c ${conffile} $FLAG
  93. RETVAL=$?
  94. return $RETVAL
  95. }
  96. rh_status() {
  97. status -p ${pidfile} ${nginx}
  98. }
  99. # See how we were called.
  100. case "$1" in
  101. start)
  102. rh_status >/dev/null 2>&1 && exit 0
  103. start
  104. ;;
  105. stop)
  106. stop
  107. ;;
  108. status)
  109. rh_status
  110. RETVAL=$?
  111. ;;
  112. restart)
  113. configtest -q || exit $RETVAL
  114. stop
  115. start
  116. ;;
  117. upgrade)
  118. rh_status >/dev/null 2>&1 || exit 0
  119. upgrade
  120. ;;
  121. condrestart|try-restart)
  122. if rh_status >/dev/null 2>&1; then
  123. stop
  124. start
  125. fi
  126. ;;
  127. force-reload|reload)
  128. reload
  129. ;;
  130. configtest)
  131. configtest
  132. ;;
  133. *)
  134. echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|upgrade|reload|status|help|configtest}"
  135. RETVAL=2
  136. esac
  137. exit $RETVAL

Ubuntu启动脚本

  1. #!/bin/sh
  2. #
  3. # nginx - this script starts and stops the nginx daemon
  4. #
  5. # chkconfig: - 85 15
  6. # description: Nginx is an HTTP(S) server, HTTP(S) reverse \
  7. # proxy and IMAP/POP3 proxy server
  8. # processname: nginx
  9. # config: /etc/nginx/nginx.conf
  10. # config: /etc/sysconfig/nginx
  11. # pidfile: /var/run/nginx.pid
  12. # Source function library.
  13. . /etc/rc.d/init.d/functions
  14. # Source networking configuration.
  15. . /etc/sysconfig/network
  16. # Check that networking is up.
  17. [ "$NETWORKING" = "no" ] && exit 0
  18. nginx="/usr/sbin/nginx"
  19. prog=$(basename $nginx)
  20. sysconfig="/etc/sysconfig/$prog"
  21. lockfile="/var/lock/subsys/nginx"
  22. pidfile="/var/run/${prog}.pid"
  23. NGINX_CONF_FILE="/etc/nginx/nginx.conf"
  24. [ -f $sysconfig ] && . $sysconfig
  25. start() {
  26. [ -x $nginx ] || exit 5
  27. [ -f $NGINX_CONF_FILE ] || exit 6
  28. echo -n $"Starting $prog: "
  29. daemon $nginx -c $NGINX_CONF_FILE
  30. retval=$?
  31. echo
  32. [ $retval -eq 0 ] && touch $lockfile
  33. return $retval
  34. }
  35. stop() {
  36. echo -n $"Stopping $prog: "
  37. killproc -p $pidfile $prog
  38. retval=$?
  39. echo
  40. [ $retval -eq 0 ] && rm -f $lockfile
  41. return $retval
  42. }
  43. restart() {
  44. configtest_q || return 6
  45. stop
  46. start
  47. }
  48. reload() {
  49. configtest_q || return 6
  50. echo -n $"Reloading $prog: "
  51. killproc -p $pidfile $prog -HUP
  52. echo
  53. }
  54. configtest() {
  55. $nginx -t -c $NGINX_CONF_FILE
  56. }
  57. configtest_q() {
  58. $nginx -t -q -c $NGINX_CONF_FILE
  59. }
  60. rh_status() {
  61. status $prog
  62. }
  63. rh_status_q() {
  64. rh_status >/dev/null 2>&1
  65. }
  66. # Upgrade the binary with no downtime.
  67. upgrade() {
  68. local oldbin_pidfile="${pidfile}.oldbin"
  69. configtest_q || return 6
  70. echo -n $"Upgrading $prog: "
  71. killproc -p $pidfile $prog -USR2
  72. retval=$?
  73. sleep 1
  74. if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then
  75. killproc -p $oldbin_pidfile $prog -QUIT
  76. success $"$prog online upgrade"
  77. echo
  78. return 0
  79. else
  80. failure $"$prog online upgrade"
  81. echo
  82. return 1
  83. fi
  84. }
  85. # Tell nginx to reopen logs
  86. reopen_logs() {
  87. configtest_q || return 6
  88. echo -n $"Reopening $prog logs: "
  89. killproc -p $pidfile $prog -USR1
  90. retval=$?
  91. echo
  92. return $retval
  93. }
  94. case "$1" in
  95. start)
  96. rh_status_q && exit 0
  97. $1
  98. ;;
  99. stop)
  100. rh_status_q || exit 0
  101. $1
  102. ;;
  103. restart|configtest|reopen_logs)
  104. $1
  105. ;;
  106. force-reload|upgrade)
  107. rh_status_q || exit 7
  108. upgrade
  109. ;;
  110. reload)
  111. rh_status_q || exit 7
  112. $1
  113. ;;
  114. status|status_q)
  115. rh_$1
  116. ;;
  117. condrestart|try-restart)
  118. rh_status_q || exit 7
  119. restart
  120. ;;
  121. *)
  122. echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}"
  123. exit 2
  124. esac

配置和使用问题

Nginx提供了很多内置的变量,如:

  1. $arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。
  2. $args 这个变量等于请求行中的参数。
  3. $binary_remote_addr 二进制码形式的客户端地址。
  4. $body_bytes_sent 传送页面的字节数
  5. $content_length 请求头中的Content-length字段。
  6. $content_type 请求头中的Content-Type字段。
  7. $cookie_COOKIE cookie COOKIE的值。
  8. $document_root 当前请求在root指令中指定的值。
  9. $document_uri $uri相同。
  10. $host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。
  11. $is_args 如果$args设置,值为"?",否则为""
  12. $limit_rate 这个变量可以限制连接速率。
  13. $nginx_version 当前运行的nginx版本号。
  14. $query_string $args相同。
  15. $remote_addr 客户端的IP地址。
  16. $remote_port 客户端的端口。
  17. $remote_user 已经经过Auth Basic Module验证的用户名。
  18. $request_filename 当前连接请求的文件路径,由rootalias指令与URI请求生成。
  19. $request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_passfastcgi_pass指令的location中比较有意义。
  20. $request_body_file 客户端请求主体信息的临时文件名。
  21. $request_completion 未知。
  22. $request_method 这个变量是客户端请求的动作,通常为GETPOST
  23. 包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。
  24. $request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI
  25. $scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;
  26. $server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。
  27. $server_name 服务器名称。
  28. $server_port 请求到达服务器的端口号。
  29. $server_protocol 请求使用的协议,通常是HTTP/1.0HTTP/1.1
  30. $uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。

另外:
HTTP_X_FORWARDED_FOR是透过代理服务器取得客户端的真实IP地址,有些用此方法读取到的仍然是代理服务器的IP。还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 HTTP_X_FORWARDED_FOR 取到的值将是空的。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注