前言:
Nginx介紹:
Nginx(engine x)是由俄羅斯人logor sysoev研發的;官方網站:nginx.org;nginx是一個輕量級的高性能的web服務器和反向代理服務器;nginx本身一個處理靜態資源的web服務器,但是通過加裝fastcgi等模塊,可是支持動態資源;可以為IMAP/POP3/SMTP等做代理,是工作在應用層的代理。
一、Nginx功能
1)Nginx的特性
模塊化設計、較好的擴展性; 高可靠性:一個master啟動異或多個worker,每個worker響應多個請求 低內存消耗:10000個keepalive連接在nginx中僅消耗2.5MB 支持熱部署:不停機更新配置文件、更新日志文件、更新服務器程序版本
2)Nginx的基本功能
靜態web資源服務器,能夠緩存打開的文件描述符 支持http/imap/pop3/smtp的反向代理;支持緩存、負載均衡 支持Fastcgi(fpm) 模塊化,非DSO機制,支持過濾器zip壓縮,SSI以及圖像大小調整 支持SSL
3)Nginx的擴展功能
基于名稱和IP的虛擬主機 支持keepalive保持機制 支持平滑升級 定制訪問日志,支持使用日志緩沖區提高日志存儲性能 支持url rewrite 支持路徑別名(root或alias指定) 支持基于IP以及用戶的訪問控制 支持傳輸速率限制,并發限制
4)Nginx的基本架構
一個master進程,生成一個或多個worker進程,每個worker相應多個請求 事件驅動:epoll,kqueue,poll,select,rt signals 支持sendfile,sendfile64 支持AIO 支持mmap
5)Nginx模塊類型
Nginx core module: nginx的核心模塊 Standard HTTP modules:nginx的標準模塊 Optional HTTP modules:nginx的可選模塊 Mail modules :nginx的郵件模塊 3rd party modules:nginx的第三方模塊
二、Nignx安裝配置
1)安裝環境
編譯環境:安裝Development Tools和Server Platform Development(centos6.6) nginx軟件包:nginx-1.6.1.tar.gz pcre-devel:正則表達式依賴包
2)編譯安裝Nignx
# tar xf nginx-1.6.1.tar.gz -C /usr/src # cd /usr/src/nginx-1.6.1/ # groupadd nginx # useradd -r -g nginx nginx # ./configure \ > --prefix=/usr/local/nginx \ > --conf-path=/etc/nginx/nginx.conf \ > --user=nginx --group=nginx \ > --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\ > --with-http_ssl_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 # make;make install 導出二進制文件: # vim /etc/profile.d/nginx.sh export PATH=/usr/local/nginx/sbin/:$PATH # soucre /etc/profile.d/nginx.sh 配置vim,使其編輯nginx配置文件時語法著色,默認沒有 # mkdir .vim # cp -ra /usr/src/nginx-1.6.1/contrib/vim/* .vim/ # ls .vim ftdetect indent syntax 啟動nginx: #mkdir -pv /var/tmp/{client,proxy,fcgi} #nginx 編寫啟動nginx腳本: # vim /etc/rc.d/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) sysconfig="/etc/$prog" lockfile="/var/lock/subsys/nginx" pidfile="/var/run//nginx/${prog}.pid" NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f $sysconfig ] && . $sysconfig start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 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 -p $pidfile $prog retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest_q || return 6 stop start } reload() { configtest_q || return 6 echo -n $"Reloading $prog: " killproc -p $pidfile $prog -HUP echo } configtest() { $nginx -t -c $NGINX_CONF_FILE } configtest_q() { $nginx -t -q -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } # Upgrade the binary with no downtime. upgrade() { local oldbin_pidfile="${pidfile}.oldbin" configtest_q || return 6 echo -n $"Upgrading $prog: " killproc -p $pidfile $prog -USR2 retval=$? sleep 1 if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]]; then killproc -p $oldbin_pidfile $prog -QUIT success $"$prog online upgrade" echo return 0 else failure $"$prog online upgrade" echo return 1 fi } # Tell nginx to reopen logs reopen_logs() { configtest_q || return 6 echo -n $"Reopening $prog logs: " killproc -p $pidfile $prog -USR1 retval=$? echo return $retval } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest|reopen_logs) $1 ;; force-reload|upgrade) rh_status_q || exit 7 upgrade ;; reload) rh_status_q || exit 7 $1 ;; status|status_q) rh_$1 ;; condrestart|try-restart) rh_status_q || exit 7 restart ;; *) echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart|reopen_logs}" exit 2 esac #chkconfig --add /etc/rc.d/init.d/nginx #service nginx restart Stopping nginx: [ OK ] Starting nginx: [ OK ]
3)配置文件介紹
主要有兩部分;分別是: main:主體部分 http{}:虛擬主機配置部分
配置指令主要以分號結尾;配置語法: directive value1 [value2 ....] 支持使用變量: 模塊內置的變量 自定義變量:set var_name value
主配置段的指令類別: 用于調試和定位問題: (1)daemon [on|off]: 是否以守護進程的方式啟動nginx; (2)master_press [on|off]: 是否以master/worker模型來運行nginx; (3)error_log /path/to/error_log level: 指明錯誤日志文件級別,處于調試目的,可以使用debug級別,但次級別只有在編譯nginx時使用了--with-debug選項才有效 ; 正常運行必備的配置: (1)user USERNAME [GROUPNAME]:指定運行worker的用戶和用戶組;例如 user nginx nginx (2)pid /path/to/nginx.pid : 指定pid文件 (3)worker_rlimit_nofile # : 指定一個worker進程能夠打開的最大文件句柄數 (4)worker_rlimit_sigpending # : 指定每個用戶能夠發往worker信號的數量 優化性能相關的配置: (1)worker_processes # :worker進程的個數,通常是cpu核心數減1 (2)worker_cpu_affinity cpuumask :綁定worker進程至指定的CPU上 (3)timer-resolution t :時間解析度,在x86服務器上可以不用配置 (4)worker_priority NICE :調整nice值(-20,19);nice值越大,越優先分配cpu 事件相關的配置; (1)accept_mutex [on|off] :內部調動用戶請求至各worker時的負載均衡鎖;啟用時表示能夠讓多個worker輪流的、序列化的響應請求 (2)lock_file /path/to/lock_file :指定鎖文件 (3)accept_mutex_delay #ms: 取得負載均衡鎖的時間 (4)use [epoll|poll|select|rgsig]:定義使用的事件模型,建議讓nginx自動選擇 (5)worker_connections #:每個worker進程所能夠響應的最大并發請求數
三、nginx的一些基本功能實現
1)基于用戶認證:
(1)修改配置文件 #vim /etc/nginx/nginx.conf server { listen 80; server_name www.mylinux.com; location / { root /www/html; index index.html index.htm; auth_basic "www.magedu.com"; auth_basic_user_file /www/html/.passwd; } } (2)創建文檔根目錄以及使用httpd-tools中的htpasswd工具創建用戶 # mkdir -pv /www/html # echo "Welcome to mylinx" > /www/html/index.html # yum -y install httpd-tools # htpasswd -c -m /www/html/.passwd centos New password: Re-type new password: (3)重新載入配置文件 # nginx -t # service nginx reload
2)基于ip認證:
#vim /etc/nginx/nginx.conf server { listen 80; server_name www.mylinux.com; location / { root /www/html; index index.html index.htm; deny 172.16.2.0/24; allow all; } } #nginx -t #service nginx reload
3)基于gzip壓縮:
# vim /etc/nginx/nginx.conf server { listen 80; server_name www.mylinux.com; location / { root /www/html; gzip on; gzip_http_version 1.0; gzip_min_length 1000; gzip_proxied expired no-cache no-store private auth; gzip_types text/plain applicatioin/xml text/css application/x-javascript text/xml application/xml+rss text/javascript application/javascript application/json; gzip_disable msie6 safari ; } } #nginx -t #cp /etc/rc.d/rc.sysinit /www/html/zip.html #service nginx reload # ll -h /www/html/zip.html -rwxr-xr-x. 1 root root 20K Dec 17 07:46 /www/html/zip.html
4)定制響應頭部
#vim /etc/nginx/nginx.conf server { listen 80; server_name www.mylinux.com; location / { root /www/html; expires 24h; add_header magedu mylinux ; } } #nginx -t #service nginx reload
5)URL重定向
語法格式: rewrite regex replacement [flages] flages last:一旦被當前規則匹配并重寫后立即停止檢查其他后續的rewrite的規則,而后通過重寫后的規則重寫發起請求; break:一旦被當前規則匹配并重寫后立即停止后續的其他rewrite的規則,而后由nginx進行后續操作 ; redirect:返回302臨時重定向; permanent:返回301永久重定向; 例: # vim /etc/nginx/nginx.conf server { listen 80; server_name www.mylinux.com; location / { root /www/html; } location /admin { root /www/html; rewrite ^/admin/(.*)$ /web/$1; } } # mkdir -pv /www/html/{admin,web} # echo "www.magedu.com" > /www/html/web/index.html #nginx -t #service nginx reload
原創文章,作者:馬行空,如若轉載,請注明出處:http://www.www58058.com/5417