Nginx的安裝及使用

本節我們要講的是關於nginx的內容,nginx是一個免費,開源,高性能的http服務器以及反向代理服務器,也是一個IMAP/POP3代理服務器。nginx因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。那麼下面我們就來詳細介紹下它吧。

 

nginx特性

    基本功能:

        1、能夠實現服務於靜態文件,也就是靜態資源的web服務器,能自動緩存打開的文件描述符;

        2、反向代理服務器,能夠實現簡單的負載均衡和冗餘

        3、能夠支持FastCGI協議

        4、有模塊化話功能,但非DSO(動態裝卸載)機制,支持多種過濾器gzip,SSI和完成圖像大小調整等

        5、支持SSL功能

    擴展功能:

        1、能夠基於名稱和IP做虛擬主機

        2、支持keepalive

        3、支持平滑的配置更新或程序版本升級

        4、支持定製訪問日誌,支持使用日誌緩存以提高性能

        5、支持url rewrite(地址重寫)   

        6、支持路徑別名

        7、支持基於IP及用戶的認證

        8、支持速率限制,併發限制等

nginx的編譯安裝

編譯安裝之前必須準備好編譯環境

[root@www ~]# yum install gcc openssl-devel pcre-devel zlib-devel

創建運行所需要的用戶

[root@www ~]# groupadd -r nginx

[root@www ~]# useradd -r -g nginx -s /bin/false -M nginx

[root@www ~]# tar xf nginx-1.6.1.tar.gz

image

[root@www ~]# cd nginx-1.6.1
[root@www nginx-1.6.1]# ./configure \   
  --prefix=/usr \    
  --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 \    
  --pid-path=/var/run/nginx/nginx.pid  \    
  --lock-path=/var/lock/nginx.lock \    
  --user=nginx \    
  --group=nginx \    
  --with-http_ssl_module \    
  --with-http_flv_module \    
  --with-http_stub_status_module \    
  --with-http_gzip_static_module \    
  --http-client-body-temp-path=/var/tmp/nginx/client/ \    
  --http-proxy-temp-path=/var/tmp/nginx/proxy/ \    
  --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ \    
  --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \    
  --http-scgi-temp-path=/var/tmp/nginx/scgi \    
  --with-pcre
[root@www nginx-1.6.1]# make && make install

提供啓動腳本:/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/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

創建需要的目錄並啓動服務

[root@www tmp]# mkdir -v /var/tmp/nginx/   

mkdir: created directory `/var/tmp/nginx/'    

[root@www tmp]# service nginx start    

Starting nginx:                                            [  OK  ]

 

配置文件解讀

nginx的配置文件分三段

main配置段

image

http配置段

image

server配置段

clipboard

一些格式:

listen配置的格式:(只能在server中進行設置)

listen address:port[ default [ backlog=num | rcvbuf=size接收緩存大小 | sndbuf=size 發送緩存大小 | accept_filter=filter | deferred | bind | ssl 只能使用443端口]]

listen 127.0.0.1:8000;

listen 127.0.0.1;

listen 8000;

listen *:8000;

listen localhost:8000;

 

location配置的格式:(只能在server中進行設置)

location [=|~|~*|^~|@] /url/ {...}

image

例子

 image

 

下面是一個示例配置

#/usr/local/ngnix/conf/ngnix.conf

user nginx;                       運行服務的用戶名
worker_processes 4;         運行的進程數
#error_log logs/error.log;       
error_log /data/applogs/nginx/error.log notice;    錯誤日誌
#error_log logs/error.log info;
pid /data/appdatas/nginx.pid;                            進程文件
events {
    worker_connections 65535;                           最大連接數
}
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 logs/access.log main;
    sendfile on;                                                   
    #tcp_nopush on;
    #keepalive_timeout 0;
    keepalive_timeout 65;                              持久連接超時時間
  gzip on;                                                   是否壓縮
  gzip_http_version 1.1;
  gzip_min_length 1024;
  gzip_comp_level 6;
  gzip_buffers 16 8k;
  gzip_proxied expired no-cache no-store private auth no_last_modified no_etag;
  gzip_types text/plain application/x-javascript text/css application/xml application/json;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";
   include nginx_app.conf;                                  包含配置文件,定義server
}

   

   

nginx_app.conf

server {   
    listen       80 ;                                    定義端口    
    server_name  www.mwj.com;              定義主機名
    #charset koi8-r;                                設定字符集
    #access_log  logs/host.access.log  main;     訪問日誌
    location / {   
        root   /usr/share/nginx/html;          設置服務器根目錄    
        index  index.html index.htm;  
       auth_basic   "input password";                        用戶認證功能   
       auth_basic_user_file  /etc/nginx/userfile ;         密碼文件,自己定義
                 
    }
    error_page  404              /404.html;      404錯誤頁面   
    location = /404.html {                          
        root   /usr/share/nginx/html;    
    }
    # redirect server error pages to the static page /50x.html   
    #    
    error_page   500 502 503 504  /50x.html;       50x錯誤頁面    
    location = /50x.html {    
        root   /usr/share/nginx/html;    
    }
}

 

 

創建密碼文件密碼

[root@www ~]# htpasswd -c /etc/nginx/userfile admin    創建admin用戶   

New password:     

Re-type new password:     

Adding password for user admin

 

好了,下面就可以測試下了。

image

輸入賬號密碼,成功登陸

image

本節就講到這裏,nginx的其他方面的功能我們會在後面用到的地方再進行闡述,謝謝!

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