Nginx是一款高性能的HTTP和反向代理服務器軟件
Nginx的優點
- 作爲web服務器,處理靜態文件、索引文件,自動索引的效率非常高
- Nginx可以實現無緩存的反向代理加速,提高網站運行速度
- 可以支持Rails和PHP,支持簡單的容錯和利用算法進行負載均衡
- Nginx專爲性能優化而開發,最大可支持50000個併發連接數的響應,只佔用很低的內存資源
- Nginx對CPU和內存的佔用率非常低
- Nginx支持熱部署,啓動非常迅速
Nginx由內核和模塊組成
模塊的分類:
- Handlers(處理器模塊)
- Filters(過濾器模塊)
- Proxies(代理類模塊)
Nginx的安裝與配置
1.安裝gcc,openssl-devel,pcre-devel和zlib-devel軟件庫
zlib-devel軟件庫,爲了gzip
[root@localhost ~]# yum install gcc openssl-devel zlib-devel
從網上下載pcre-devel軟件,爲了重寫rewrite
[root@localhost src]#tar zxvf pcre-8.39.tar.gz
[root@localhost src]# cd pcre-8.39
[root@localhost pcre-8.39]# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.39]#make
[root@localhost pcre-8.39]#make install
2.安裝Nginx
添加用戶和組
[root@localhost src]# groupadd -r nginx
[root@localhost src]# useradd -r -g nginx nginx
下載nginx
[root@localhost src]# tar nginx-1.10.1.tar.gz
[root@localhost src]# cd nginx-1.10.1
[root@localhost nginx-1.10.1]#./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx/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_mp4_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=/usr/local/src/pcre-8.39
[root@localhost nginx-1.10.1]#make
[root@localhost nginx-1.10.1]#make install
[root@localhost nginx-1.10.1]#mkdir -p /var/tmp/nginx/{client,proxy,fastcgi,uwsgi,scgi}
[root@localhost nginx-1.10.1]#/usr/local/nginx/sbin/nginx -V #查看安裝nginx時的編譯選項
編譯選項解釋
--prefix=/usr/local/nginx:安裝目錄
--sbin-path=/usr/local/nginx/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:pid文件
--lock-path=/var/lock/nginx.lock:lock鎖定文件
--user=nginx:程序運行時的非特權用戶
--group=nginx:程序運行時的非特權用戶組
--with-http_ssl_module:啓用ngx_http_ssl_module支持,支持https請求
--with-http_flv_module:提供尋求內存使用基於時間的偏移量文件
--with-http_mp4_module:支持mp4
--with-http_stub_status_module:獲取nginx自上次啓動以來的工作狀態
--with-http_gzip_static_module:在線實時壓縮輸出數據流
--http-client-body-temp-path=/var/tmp/nginx/client/:設定http客戶端請求臨時文件路徑
--http-proxy-temp-path=/var/tmp/nginx/proxy/:設定http代理臨時文件路徑
--http-fastcgi-temp-path=/var/tmp/nginx/fcgi/:設定http fastcgi臨時文件路徑
--http-uwsgi-temp-path=/var/tmp/nginx/uwsgi:設定http uwsgi臨時文件路徑
--http-scgi-temp-path=/var/tmp/nginx/scgi:設定http scgi臨時文件路徑
--with-pcre:啓用pcre庫
其他編譯選項
–with-pcre= 指向pcre庫文件目錄
–with-md5= 指向md5庫文件目錄
–with-sha1= 指向sha1庫目錄
–with-zlib= 指向zlib庫目錄
–with-openssl= 指向openssl安裝目錄
–with-debug 啓用debug日誌
–with-mail 啓用POP3/IMAP4/SMTP代理模塊支持
–with-http_secure_link_module:計算和檢查要求所需的安全鏈接網址
–with-http_degradation_module:允許在內存不足的情況下返回204或444碼
3.Nginx的配置文件
Nginx主配置文件,/etc/nginx/nginx.conf
Nginx的主配置文件分爲4個部分:main(全局設置),server(主機設置),upstream(負載均衡服務器設置),location(URL匹配特定位置的設定)
(1)Nginx的全局配置
user nobody;
worker_processes 4;
error_log logs/error.log notice;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 1024;
}
user nobody nobody; 指定Nginx Worker進程運行用戶以及用戶組,默認爲nobody
worker_processes 4; 指定Nginx要開啓的進程數
error_log; 定義全局錯誤日誌文件
pid; 指定進程id的存儲文件位置
worker_rlimit_nofile 65535; 用於綁定worker進程和cpu
events 用來設定Nginx的工作模式及連接數上限
use 用來指定nginx的工作模式,select和poll是標準的工作模式,kqueue和epoll是高效的工作模式,kqueue用在BSD系統,linux系統epoll是首選
worker_connections 用於定義nginx每個進程的最大連接數,默認是1024
(2)HTTP服務器的配置
http {
include conf/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"';
client_max_body_size 20m;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
client_header_timeout 10;
client_body_timeout 10;
send_timeout 10;
include 設定mime類型,類型由mime.type文件定義
default_type 默認類型爲二進制流
log_format 指定nginx日誌的輸出格式
$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址;
$remote_user:用來記錄客戶端用戶名稱;
$time_local: 用來記錄訪問時間與時區;
$request: 用來記錄請求的url與http協議;
$status: 用來記錄請求狀態;成功是200,
$body_bytes_sent :記錄發送給客戶端文件主體內容大小;
$http_referer:用來記錄從那個頁面鏈接訪問過來的;
$http_user_agent:記錄客戶瀏覽器的相關信息;
client_max_body_size 設置允許客戶端請求的最大單個文件字節數
client_header_buffer_size 指定來自客戶端請求頭的headerbuffer大小
large_client_header_buffers 指定客戶端請求中較大的消息頭的緩存最大數量和大小
sendfile 開啓高效文件傳輸模式
tcp_nopush 防止網絡堵塞
tcp_nodelay 防止網絡堵塞
keepalive_timeout 客戶端連接保持活動的超時時間
client_header_timeout 客戶端請求頭讀取超時時間
client_body_timeout 客戶端請求主體讀取超時時間
send_timeout 響應客戶端的超時時間
(3)HttpGzip模塊,支持在線實時壓縮輸出數據流
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
gzip 用於設置開啓或者關閉gzip
gzip_min_length 允許壓縮的頁面最小字節數
gzip_buffers 申請4個單位爲16k的內存作爲壓縮結果流緩存
gzip_http_version 識別HTTP協議版本,默認1.1
gzip_comp_level 指定gzip壓縮比
gzip_types 指定壓縮的類型
gzip_vary 讓前端的緩存服務器緩存經過gzip壓縮的頁面
(4)負載均衡配置
upstream ixdba.net{
ip_hash;
server 192.168.1.123:80;
server 192.168.1.124:80 down;
server 192.168.1.125:8009 max_fails=3 fail_timeout=20s;
server 192.168.1.126:8080;
}
通過upstream 指定一個負載均衡器的名稱ixdba.net
負載均衡模塊調度算法
輪詢:默認,每個請求按時間順序逐一分配到不同的後端服務器
weight:weight越大,分配到的訪問機率越高
ip_hash:每個請求按訪問ip的hash結果分配
fair:依據頁面大小和加載時間長短智能的進行負載均衡
url_hash:按訪問url的hash結果來分配請求
服務器在負載均衡調度中的狀態
down:表示暫時不參與負載均衡
backup:預留的備份機器
max_fails:允許請求失敗的次數
fail_timeout:在經歷了請求失敗的次數後,暫停服務的時間
(5)虛擬主機的配置
server{
listen 80;
server_name 192.168.0.100 www.hyundai.com;
index index.html index.htm index.jsp;
root /web/wwwroot/www.hyundai.com
charset gb2312;
listen:監聽端口
server_name:指定ip或域名
index:默認首頁地址
root:網頁根目錄
charset:設置網頁的默認編碼格式
(6)URL匹配配置
location - .*\.(gif|jpg|jpeg|png|bmp|swf)${
root /web/wwwroot/www.hyundai.com;
expires 30d;
}
把以上這些靜態文件交給Nginx處理,expires指定靜態文件過期時間爲30天
location - ^/(upload|html)/{
root /web/wwwroot/www.hyundai.com;
expires 30d;
}
把upload和html下的所有文件都交給nginx處理
location ~ .*.jsp${
index index.jsp;
proxy_pass http://localhost:8080;
}
所有.jsp文件都交給本機的8080端口處理
4.Nginx的啓動和關閉
檢查nginx配置文件的正確性
/usr/local/nginx/sbin/nginx -t -c /etc/nginx/nginx.conf
顯示如下信息,則說明正確
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
顯示nginx的版本信息
/usr/local/nginx/sbin/nginx -v
nginx的啓動
/usr/local/nginx/sbin/nginx
查看nginx的啓動進程
ps -ef |grep nginx
5.Nginx開機自動啓動
vim /etc/init.d/nginx
chmod a+x /etc/init.d/nginx #添加執行權限
chkconfig --add nginx #添加到服務管理列表
chkconfig nginx on #開機啓動
service nginx start
/usr/local/nginx/html #nginx網頁根目錄
Nginx性能優化技巧
1.減小Nginx編譯後的文件大小
編譯nginx時,默認以debug模式進行,會插入很多跟蹤和ASSERT之類的信息,在編譯之前,修改源碼,取消debug模式
源碼目錄下vim auto/cc/gcc,刪除或註釋以下幾行
# debug
CFLAGS="$CFLAGS -g"
2.爲特定的CPU指定CPU類型編譯優化
優化GCC編譯
--with-cc-opt='-O3'
--with-cpu-opt=CPU #爲特定的CPU編譯
cat /proc/cpuinfo | grep "model name" #確定CPU類型