Nginx設計架構圖:linux
2、安裝nginx
2.1 Nginx依賴關係web
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 installcookie
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配置文件結構:
3.2 配置文件詳解
3.3 平常維護技巧
配置正確性檢查
nginx啓動、關閉、重啓
3.4 開啓目錄瀏覽功能
例如:
localtion /soft {
# 開啓Nginx目錄瀏覽功能
autoindex on;
# 文件大小從KB開始顯示
autoindex_exact_size off;
# 顯示文件修改時間爲服務器本地時間
autoindex_localtime on;
}
4、nginx經常使用功能
反向代理
1、多域名跳轉應用實例
2、Nginx重定向實現新舊域名過渡
rewrite
if ($host != 'www.sharelinux.cn') {
rewrite ^/(.*)$ http://www.sharelinux.com/$1permanent;
}
3、alias 和root 區別 舉例說明
4、Location命令應用配置
=
/
^~
~*
匹配模式、匹配優先級舉例
URL重寫
1、if命令
2、rewrite命令
3、set命令
4、break命令
模塊
5、案例:web緩存服務器
nginx_ngx_cache-2.1.tar.gz
--add-module
6、案例:負載均衡器
負載均衡算法
輪詢、weight、ip_hash、fair、url_hash、
HTTP Upstream
down、backup、max_fails、fail_timeout
調度器算法爲ip_hash時,後端服務器在負載均衡器調度中的狀態不能是weight、backup
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,、502、503、504、和執行超時等錯誤時,自動將請求轉發到upstream負載均衡組中的另外一臺服務器,實現故障轉移。
7、nginx性能優化技巧
1、減少編譯後的文件大小
關閉debug模式
2、爲特定CPU指定CPU類型編譯優化
在編譯Nginx時,默認的FCC編譯參數是"-o",要優化GCC編譯,能夠使用如下兩個參數
--with-cc-opt='-O3'
--with-cpu-opt=CPU
# 爲特定的CPU編譯,有效的值包括:
# pentium、pentiumpro、pentium3、pentium4、athlon、opteron、amd64、sparc32、sparc64、ppc64
肯定CPU類型:cat /proc/cpuinfo |grep "model name"
3、TCMalloc優化Nginx的性能 (Thread-Caching Malloc)
開源工具 google-perftools
與標準的glibc庫的malloc相比,TCMalloc庫在內存分配效率和速度上要高不少,這在很大程度上提升了服務器在高併發狀況向的性能,從而減低系統負載。爲Nginx添加TCMalloc庫支持。
安裝TCMalloc庫,須要安裝libunwind(32位操做系統不須要安裝)和google-perftools兩個軟件包,libunwind庫爲基於64位CPU和操做系統的程序提供了基本函數調用鏈和函數調用寄存器功能。
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.2、google-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
4、Nginx內核參數優化
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