nginx編譯安裝與配置使用

第一部分----nginx基本應用

源碼編譯安裝nginx

1、安裝pcre軟件包(使nginx支持http rewrite模塊)

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

2、安裝openssl-devel(使nginx支持ssl)

yum install -y openssl-devel

3、創建用戶nginx

useradd nginx
passwd nginx

4、安裝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

5、修改配置文件/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
        }
    }
 }

6、檢測nginx配置文件是否正確

nginx -t

7、啓動nginx服務

nginx

8、通過nginx -s控制nginx服務

nginx -s stop     #停止服務
nginx -s quit     #退出服務
nginx -s reopen    #重新打開日誌文件
nginx -s reload    #重新加載配置文件

9、實現nginx開機自啓

    1、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

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

    2、添加權限

chmod +x /etc/init.d/nginx

    3、設置開機自啓

chkconfig nginx on

10、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高級應用

    1、使用alias實現虛擬目錄

location /lzs {
    alias /var/www/lzs;
    index index.html;        #訪問http://x.x.x.x/lzs時實際上訪問的是/var/www/lzs/index/html

    2、通過stub_status模塊監控nginx的工作狀態

        1、通過nginx -V命令查看是否已安裝stnb_status模塊

wKioL1fGzOPyw-y4AACpRynSLkA956.png

(可以發現已經安裝了~~~)

        

        2、編輯/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;        #指定認證的密碼文件
       }

        3、創建認證口令文件並添加用戶lzs和zsgg,密碼用md5加密

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

        4、重啓服務

        5、客戶端訪問http://x.x.x.x/nginx-status即可


    3、使用limit_rate限制客戶端傳輸數據的速度

           1、編輯/etc/nginx/nginx.conf

location / {
    root    /var/www/nginx;
    index    index.html;
    limit_rate    2k;        #對每個連接的限速爲2k/s

            2、重啓服務

注意要點:

    1、配置文件中的每個語句要以;結尾

    2、使用htpasswd命令需要先安裝httpd

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章