高性能web服務器nginx---實戰篇

 

Nginx設計架構圖:

wKioL1SgwNfikAbnAAMJnMvqxbA698.jpg


2、安裝

  2.1 Nginx依賴關係

yum install -y openssl-devel pcre-devel libevent

 

  2.2 安裝nginx依賴pcre,使Nginx支持HTTP Rewrite模塊

tar xf pcre-VERSION.tar.gz

cd pcre-VERSION

./configure

make && make install

 

 2.3  Nginx編譯安裝

 

# 添加Nginx系統用戶

useradd -r -s /sbin/nologin -M nginx

 

tar xf nginx-1.6.2.tar.gz

cd nginx-1.6.2

./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 \

--pid-path=/var/run/nginx/nginx.pid  \

--lock-path=/var/lock/nginx.lock \

--user=nginx \

--group=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 \

--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \

--http-scgi-temp-path=/var/tmp/nginx/scgi \

--with-pcre

 

make && make install

 

# Nginx PATH環境變量

echo 'export PATH=/usr/local/nginx/sbin:$PATH' > /etc/profile.d/nginx.sh

. /etc/profile.d/nginx.sh


3、配置

3.1 Nginx配置文件結構:

wKioL1SgwNuzl7VfAACcz4nmrH0257.jpg

 

3.2 配置文件詳解


3.3 日常維護技巧

配置正確性檢查

nginx啓動、關閉、重啓

3.4 開啓目錄瀏覽功能

例如:

localtion /soft {

# 開啓Nginx目錄瀏覽功能

autoindex on;

# 文件大小從KB開始顯示

autoindex_exact_size off;

# 顯示文件修改時間爲服務器本地時間

autoindex_localtime on;

}

4nginx常用功能

反向代理

1、多域名跳轉應用實例

2Nginx重定向實現新舊域名過渡

rewrite

if ($host != 'www.sharelinux.cn') {

rewrite ^/(.*)$ http://www.sharelinux.com/$1permanent;

}

3alias root 區別 舉例說明

4Location命令應用配置

=

/

^~

~*

匹配模式、匹配優先級舉例

 

URL重寫

1if命令

2rewrite命令

3set命令

4break命令

模塊

5、案例:web緩存服務器

nginx_ngx_cache-2.1.tar.gz

--add-module

6、案例:負載均衡器

負載均衡算法

輪詢、weightip_hashfairurl_hash

HTTP Upstream

downbackupmax_failsfail_timeout

調度器算法爲ip_hash時,後端服務器在負載均衡器調度中的狀態不能是weightbackup

 

upstream myserver{

server 192.168.1.11:80 weight=3 max_fails=3 fail_timeout=20s;

server 192.168.1.12:80 weight=1 max_fails=3 fail_timeout=20s;

server 192.168.1.13:80 weight=4 max_fails=3 fail_timeout=20s;

}

 

location / {

proxy_pass http://myserver;

proxy_next_upstream http_500 http_502 http_503 error timeout invalid_header;

include conf/proxy.conf;

}

proxy_next_upstream 參數定義故障轉移策略,當後端服務器節點返回500,502503504、和執行超時等錯誤時,自動將請求轉發到upstream負載均衡組中的另一臺服務器,實現故障轉移。

 

7nginx性能優化技巧

1、減小編譯後的文件大小

關閉debug模式

2、爲特定CPU指定CPU類型編譯優化

在編譯Nginx時,默認的FCC編譯參數是"-o",要優化GCC編譯,可以使用以下兩個參數

--with-cc-opt='-O3'

--with-cpu-opt=CPU 

# 爲特定的CPU編譯,有效的值包括:

# pentiumpentiumpropentium3pentium4athlonopteronamd64sparc32sparc64ppc64

確定CPU類型:cat /proc/cpuinfo |grep "model name"

3TCMalloc優化Nginx的性能 Thread-Caching Malloc

開源工具 google-perftools

與標準的glibc庫的malloc相比,TCMalloc庫在內存分配效率和速度上要高很多,這在很大程度上提高了服務器在高併發情況向的性能,從而減低系統負載。爲Nginx添加TCMalloc庫支持。

 

安裝TCMalloc庫,需要安裝libunwind(32位操作系統不需要安裝)google-perftools兩個軟件包,libunwind庫爲基於64CPU和操作系統的程序提供了基本函數調用鏈和函數調用寄存器功能。

 

3.1、按libunwind

下載:http://download.savannah.gnu.org/releases/libunwind/

http://download.savannah.gnu.org/releases/libunwind/libunwind-1.1.tar.gz

tar xf libunwind-1.1.tar.gz

cd libunwind-1.1

CFLAGS=-fPIC ./configure

make CFLAGS=-fPIC

make CFLAGS=-fPIC install

 

3.2google-perftools

https://code.google.com/p/gperftools/

https://googledrive.com/host/0B6NtGsLhIcf7MWxMMF9JdTN3UVk/gperftools-2.2.tar.gz

tar xf gperftools-2.2.tar.gz

cd gperftools-2.2

./configure

make && make install

echo '/usr/local/lib'/etc/ld.so.conf.d/usr_local_lib.conf

ldconfig

 

3.3、重新編譯Nginx,在編譯安裝過程中添加"--with-google_perftools_module"選項重新編譯Nginx

./configure --prefix=/usr/local/nginx --with-google_perftools_module --with-http_stub_status_module

make && make install

 

3.4、爲google-perftools添加線程目錄

mkdir -p /tmp/tcmalloc

chmod 0777 /tmp/tcmalloc

 

3.5、修改Nginx主配置文件,在pid這一行添加如下代碼:

# pid logs/nginx.pid;

google_perftools_profiles /tmp/tcmalloc;

 

3.6、驗證運行狀態

lsof -n | grep tcmall

 

4Nginx內核參數優化

net.ipv4.tcp_max_tw_buckets = 6000

net.ipv4.tcp.ip_local_port_range = 1024 65000

net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_syncookies = 1

net.core.somaxconn = 262144

net.core.netudv_max_backlog = 262144

net.ipv4.tcp_max_orphans = 262144

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_keepalive_time = 30

 


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