nginx编译安装与配置使用

第一部分----nginx基本应用html

源码编译安装nginxnginx

一、安装pcre软件包(使nginx支持http rewrite模块)vim

yum install -y pcre
yum install -y pcre-devel

二、安装openssl-devel(使nginx支持ssl)浏览器

yum install -y openssl-devel

三、建立用户nginxbash

useradd nginx
passwd nginx

四、安装nginx
服务器

[root@localhost ~]tar -vzxf nginx-1.11.3.tar.gz -C /usr/local
[root@localhost ~]cd nginx-1.11.3/
[root@localhost nginx-1.11.3]# ./configure \
> --group=nginx \
> --user=nginx \
> --prefix=/usr/local/nginx \
> --sbin-path=/usr/sbin/nginx \
> --conf-path=/etc/nginx/nginx.conf \
> --error-log-path=/var/log/nginx/error.log \
> --http-log-path=/var/log/nginx/access.log \
> --http-client-body-temp-path=/tmp/nginx/client_body \
> --http-proxy-temp-path=/tmp/nginx/proxy \
> --http-fastcgi-temp-path=/tmp/nginx/fastcgi \
> --pid-path=/var/run/nginx.pid \
> --lock-path=/var/lock/nginx \
> --with-http_stub_status_module \
> --with-http_ssl_module \
> --with-http_gzip_static_module \
> --with-pcre
[root@localhost nginx-1.11.3]# make &&make install

五、修改配置文件/etc/nginx/nginx.conf并发

#全局参数设置
worker_processes  1;     #设置nginx启动进程的数量,通常设置成与逻辑cpu数量相同
error_log  logs/error.log;    #指定错误日志
worker_rlimit_nofile 102400;    #设置一个nginx进程能打开的最大文件数
pid        /var/run/nginx.pid;
    
events {
    worker_connections  1024;   #设置一个进程的最大并发链接数
}

#http服务相关设置
http {
    include       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; #是否调用sendfile函数输出文件,通常设置为on,若nginx是用来进行磁盘IO负载应用时,能够设置为off,下降系统负载
    gzip            on; #是否开启gzip压缩
    keepalive_timeout  65;      #设置长链接的超时时间
#虚拟服务器的相关设置
    server {
        listen       80;        #设置监听的端口
        server_name  localhost;         #设置绑定的主机名、域名或ip地址

        charset koi8-r;    #设置编码字符

        location / {
            root   /var/www/nginx;      #设置服务器默认网站的根目录位置
            index  index.html index.htm;        #设置默认打开的文档
             }

        error_page   500 502 503 504  /50x.html;        #设置错误信息返回页面
        location = /50x.html {
            root   html;        #这里的绝对位置是/var/www/nginx/html
        }
    }
 }

六、检测nginx配置文件是否正确app

nginx -t

七、启动nginx服务
ide

nginx

八、经过nginx -s控制nginx服务
函数

nginx -s stop     #中止服务
nginx -s quit     #退出服务
nginx -s reopen    #从新打开日志文件
nginx -s reload    #从新加载配置文件

九、实现nginx开机自启

    一、vim /etc/init.d/nginx

#!/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/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

--------------------------------------------------------------------------

    二、添加权限

chmod +x /etc/init.d/nginx

    三、设置开机自启

chkconfig nginx on

十、nginx日志文件详解

    nginx日志文件分为log_format和access_log两部分

    log_format定义记录的格式,其语法格式为

        log_format        样式名称        样式详情

    配置文件中默认有

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
变量 说明

$remote_addr和$http_x_forwarded_for

客户端的ip
$remote_user 客户端的名称
$time_local 访问时的本地时间
$request 请求的URL和http协议
$status 访问的状态码
$body_bytes_sent 发送给客户端的主体内容大小
$http_referer 记录客户端是从哪一个页面连接访问过来的,若没有连接,则访问‘-’
$http_user_agent 记录客户端使用的浏览器的相关信息


    access_log主要指定使用哪一种格式记录和日志文件的位置,其语法格式为

access_log    日志文件路径        样式名称

    如:

access_log    /var/log/nginx/access.log    main;

下面是日志内容的截图示例

wKioL1fGyqWTbNdrAAAqomXK2kE843.png


第二部分-----nginx高级应用

    一、使用alias实现虚拟目录

location /lzs {
    alias /var/www/lzs;
    index index.html;        #访问http://x.x.x.x/lzs时实际上访问的是/var/www/lzs/index/html

    二、经过stub_status模块监控nginx的工做状态

        一、经过nginx -V命令查看是否已安装stnb_status模块

wKioL1fGzOPyw-y4AACpRynSLkA956.png

(能够发现已经安装了~~~)

        

        二、编辑/etc/nginx/nginx.conf配置文件

#添加如下内容~~
location /nginx-status {
       stub_status on;
       access_log    /var/log/nginx/nginxstatus.log;    #设置日志文件的位置
       auth_basic    "nginx-status";    #指定认证机制(与location后面的内容相同便可)
       auth_basic_user_file    /etc/nginx/htpasswd;        #指定认证的密码文件
       }

        三、建立认证口令文件并添加用户lzs和zsgg,密码用md5加密

htpasswd -c -m /etc/nginx/htpasswd lzs
htpasswd -m /etc/nginx/htpasswd zsgg

        四、重启服务

        五、客户端访问http://x.x.x.x/nginx-status便可


    三、使用limit_rate限制客户端传输数据的速度

           一、编辑/etc/nginx/nginx.conf

location / {
    root    /var/www/nginx;
    index    index.html;
    limit_rate    2k;        #对每一个链接的限速为2k/s

            二、重启服务

注意要点:

    一、配置文件中的每一个语句要以;结尾

    二、使用htpasswd命令须要先安装httpd

相关文章
相关标签/搜索