码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx安装配置实现用户认证、反向代理、隐藏版本号

时间:2015-09-19 06:21:02      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:nginx安装配置实现用户认证、反向代理、隐藏版本号

一.Nginx安装

       1.检查并且安装依赖组件
           检查安装nginx的模块需要第三方库的支持,检查是否安装下列库:zlib、zlib-devel、openssl、openssl-devel、pcre、pcre-devel如果没有,则全部装上
          # rpm -qa | grep pcre  ##没有任何信息则没安装

      2.安装pcre,pcre-devel

         # tar -zxvf pcre-6.6.9.tar.gz 
         # cd pcre-6.6.9/
         # ./configure
         # make && make install

     3.安装google-perftools
         # tar -zxvf google-perftools-1.8.3.tar.gz 
         # cd google-perftools-1.8.3/
         # ./configure
         # make && make install

PS:  google-perftools 是一款针对 C/C++ 程序的性能分析工具,它是一个遵守 BSD 协议的开源项目。使用该工具可以对 CPU 时间片、内存等系统资源的分配和使用进行分析,本文将重点介绍如何进行 CPU 时间片的剖析。 google-perftools 对一个程序的 CPU 性能剖析包括以下几个步骤。

        1. 编译目标程序,加入对 google-perftools 库的依赖。
        2. 运行目标程序,并用某种方式启动 / 终止剖析函数并产生剖析结果。
        3. 运行剖结果转换工具,将不可读的结果数据转化成某种格式的文档(例如 pdf,txt,gv 等)。

     4.编译安装Nginx

        首先添加用户nginx,实现以之运行nginx服务进程

        #groupadd -r -g 108 nginx
        # useradd -r -g 108 -u 108 –s /sbin/nologin nginx
        # tar -zxvf nginx-1.8.0.tar.gz 
        # cd   nginx-1.8.0/
        # ./configure  \
           --with-cc-opt=‘-O3‘ \                             ##注意整个不是零,是大写英文字母O
           --with-google_perftools_module \        ##可选组件
           --prefix=/usr/local/nginx \                    ##nginx安装目录
           --with-openssl=/usr/lib
           --with-http_stub_status_module
           --with-http_image_filter_module
           --user=nginx
           --group=nginx
       # make && make install
       要点:禁止DEBUG模式
       # vi auto/cc/gcc
       # debug    //注释下面
                 CFLAGS="$CFLAGS -g"
      5.nginx的信号控制(有关nginx的启动与关闭)
            TERM,INT  快速关闭
            QUIT  从容关闭
            HUP  平滑重启,重新加载配置文件
            USR1  重新打开日志文件,在切割日志时用途较大
            USR2  平滑升级可执行程序
            WINCH  从容关闭工作进程
        我们可以直接通过以下命令来完成平滑重启,省下寻找nginx主进程号的步骤:
        #kill -"信号类型”`cat   /usr/local/nginx/logs/nginx.pid`或者nginx的主进程号
        ( 1)、从容停止nginx
              kill -QUIT 6019        #nginx主进程号
              kill  -QUIT  `
cat /usr/local/nginx/logs/nginx.pid`
        ( 2 )、快速停止nginx
              kill -TERM /INT nginx主进程号
              kill  -TERM /INT   `
cat /usr/local/nginx/logs/nginx.pid`
       ( 3 )、强制停止所有的nginx进程
              pkill -9 nginx
       ( 4 )、平滑重启nginx
              kill -HUP  `
cat /usr/local/nginx/logs/nginx.pid`
       nginx的平滑重启
             kll HUP Nginx主进程号   或者   kll HUP nginx.pid文件按存放路径

     6.给/usr/local/nginx/sbin/nginx 脚本执行权限

      #chmod o+x /usr/local/nginx/sbin/nginx

      修改了nginx的配置文件要重启nginx;重启之前要检查配置文件是否正确

      # /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

     7.验证是否成功启动nginx服务

      #netstat -tnlp | grep :80   并且在浏览器中访问

     8. 为nginx提供SysV init脚本:

#!/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 0
 
nginx="/usr/local/nginx/sbin/nginx"
prog=$(basename $nginx)
 
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
   # make required directories
   user=`nginx -V 2>&1 | grep "configure arguments:" | sed ‘s/[^*]*--user=\([^ ]*\).*/\1/g‘ -`
   options=`$nginx -V 2>&1 | grep ‘configure arguments:‘`
   for opt in $options; do
       if [ `echo $opt | grep ‘.*-temp-path‘` ]; then
           value=`echo $opt | cut -d "=" -f 2`
           if [ ! -d "$value" ]; then
               # echo "creating" $value
               mkdir -p $value && chown -R $user $value
           fi
       fi
   done
}
 
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    make_dirs
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
 
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
 
restart() {
    configtest || return $?
    stop
    sleep 1
    start
}
 
reload() {
    configtest || return $?
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    RETVAL=$?
    echo
}
 
force_reload() {
    restart
}
 
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
    status $prog
}
 
rh_status_q() {
    rh_status >/dev/null 2>&1
}
 
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
        exit 2
esac

    9.为此脚本提供执行权限

      #chmod o+x /etc/rc.d/init.d/nginx

   10.将配置文件复制到/etc/nginx/目录下

     #mkdir /etc/nginx

     #cp /usr/local/nginx/conf/*   /etc/nginx/

   11.添加服务至服务管理列表,并让其开机自动启动

     #chkconfig --add nginx

     #chkconfig nginx on

  12.重新启动服务

     #service nginx restart

二.用户认证功能

    1.HttpAuthBasic模块模块实现只有输入正确的用户密码才允许访问web内容。默认情况下nginx已经安装了 HttpAuthBasic模块模块

     auth_basic:

        语法:     auth_basic [  text  | off  ]

        默认值: auth_basic off

        作用域: http, server, location, limit_except

     该指令包含用于 HTTP 基本认证 的测试名和密码。分配的参数用于认证领域。值 "off" 可以使其覆盖来自上层指令的继承性。

      auth_basic_user_file:

         语法:     auth_basic_user_file  the_file

         默认值: no

         作用域: http, server, location, limit_except

      该指令为某认证领域指定 htpasswd 文件名。

location  /  {
: auth_basic            "Restricted";
: auth_basic_user_file  conf/htpasswd;
}

     2.文件内容格式如下:

         用户名1:加密后的密码

         用户名2:加密后的密码

     3.安装httpd服务不启动,用htpasswd命令来创建用户密码:

     #htpasswd -c -m /etc/nginx/.htpasswd  用户名

     #htpasswd  -m /etc/nginx/.htpasswd  用户名    ##第二次不能再使用-c 选项否则会覆盖文件内容

     4.编辑/etc/nginx/nginx.conf文件实现用户验证:

          在location段中添加

               auth_basic "Restricted";

               auth_basic_user_file  ".htpasswd";

三.反向代理

    HttpProxy模块:

location / {
: proxy_pass        http://localhost:8000;
: proxy_set_header  X-Real-IP  $remote_addr;
}

   #vim /etc/nginx/nginx.conf 添加如下内容:

           location  /forum/  {

                    proxy_pass  http://172.16.100.6/bbs/;      

                    proxy_set_header X-Real_IP $remote_addr;

             }       


   172.16.100.6   /var/html/forum

   //正则表达式会将forum映射到后端服务器上

           location ~*^/forum {

                   proxy_pass http://172.16.100.6;     ##相当于访问172.16.100.6/forum

                   proxy_set_header X-Real_IP $remote_addr; 

              } 

    在后端服务器上日志文件中显示客户端地址:

         proxy_set_header X-Real_IP $remote_addr;

    修改后端主机上的httpd.conf

        日志格式:

        %(X-Real-IP)i

     将整个网站的访问都转向后端  只保持一个location /  

四.隐藏版本号

    1.编辑/etc/nginx/nginx.conf文件在

          server_tokens off;

    2.编辑php-fpm的配置文件fastcgi.conf

         fastcgi_param  SERVER_SOFTWARE    nginx;








本文出自 “珞辰的博客” 博客,请务必保留此出处http://luochen2015.blog.51cto.com/9772274/1696178

Nginx安装配置实现用户认证、反向代理、隐藏版本号

标签:nginx安装配置实现用户认证、反向代理、隐藏版本号

原文地址:http://luochen2015.blog.51cto.com/9772274/1696178

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!