1.Ngjinx 簡介
Nginx(發音同 engine x)是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器。其特點是佔有內存少,併發能力強,中國大陸使用nginx網站用戶有:新浪、網易、騰訊等。能夠支持高達 50,000 個併發連接數的響應。
2.編譯安裝Nginx
#添加Nginx運行用戶及組 [root@lab1 ~]# groupadd nginx -r [root@lab1 ~]# useradd -g nginx -r -s /sbin/nologin nginx #解決依賴關係 [root@lab1 ~]# yum install pcre-devel zlib-devel openssl-devel #解壓並安裝 [root@lab1 nginx-1.6.1]# ./configure \ --prefix=/usr/local/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --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@lab1 nginx-1.6.1]# make && make install #創建Nginx臨時目錄 [root@lab1 ~]# mkdir -v /var/tmp/nginx/ mkdir: created directory `/var/tmp/nginx/' 爲Nginx提供啓動腳本,腳本內容如下 [root@lab1 ~]# cat /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 #現在我們已經可以正常啓動並訪問Nginx服務器了 chmod +x /etc/init.d/nginx [root@lab1 nginx-1.6.1]# service nginx start Starting nginx: [ OK ]
3.Nginx配置文件詳解
#文檔結構 ... #運行,性能相關配置 event{ #事件相關配置 } http{ #nginx的http web功能配置段 ... #http server通用配置段 ... server { #虛擬主機配置段 } server { ... } } ###################### #其中相關的配置項有: ###################### #運行相關 user USERNAME [GROUPNAME]; #指定 運行用戶 pid /path/to/pid/file; #指定pid文件位置 worker_rlimit_nofile #; #指定每一個worker進程可以打開的最大文件數 worker_rlomit_sigpending #; #指定沒用用戶能夠發往worker進程的信號數量 #性能相關配置 worker_processes #; #worker進程數,通常爲cpu個數減1 worker_cpu_affinity cpumask ...; #指定每個進程工作在哪個cpu上 如:0001 0010 0100 1000 表示第一個CPU在第一個CPU上運行 表示第二個CPU在第二個CPU上運行 ...... ssl_engine_device ; #在存在ssl硬件加速器的服務器上,指定加速設備 worker_priority nice;指定進程運行優先級 nice爲-20-19之間的值 #事件相關配置 event{} accept_mutex on|off #是否打開負載均衡鎖 lock_file /path... #lock文件位置 multi_accept on|off #是否允許一此性響應多個用戶請求 accept_mutex_delay # #重試獲取accept鎖時間間隔 use epool|rtsig|select|poll #定時使用事件模型,建議nginx自動選擇 #server相關配置 server{} #定義一個虛擬主機 listen #定義監聽地址和端口 default_server #定義默認httpserver,未使用此選項,默認爲第一個 rcvbuf=SIZE :接受緩衝區大小 sndbuf=SIZE:發送緩衝區大小 server_name ;定義主機名 #可以定義多個,也可以使用通配符和正則表達式匹配 #資源定義 location =|~|~*|^~ uri ... {} # =:精確匹配; # ~:正則表達式模式匹配,匹配時區分字符大小寫 # ~*:正則表達式模式匹配,匹配時忽略字符大小寫 # ^~: URI前半部分匹配,不檢查正則表達式 # 匹配優先級: # =, ^~ , ~ , ~* , 空 root path #在location中定義web資源路徑 alias path #定義路徑別名 index file ...; #定義默認主頁 error_page code ... [=[response]] uri; #定義錯誤頁面重定向 try_files path1 [path2].... uri #自左而右嘗試path路徑,均不成功時返回最後一各uri
4.配置使用nginx
#配置基於端口的虛擬主機及添加認證 server { listen 8000; auth_basic "admin area"; auth_basic_user_file /etc/nginx/.htpasswd; location / { root /web/test; index index.html; } } #使用httpd的htpasswd生成用戶名和密碼 [root@lab1 html]# htpasswd -c /etc/nginx/.htpasswd admin #創建網站根錄及文件 [root@lab1 html]# mkdir /web/test -p [root@lab1 html]# echo '172.16.21.101:8000' > /web/test/index.html
未完待續.....