一、NGINX介紹:
Nginx 是一個高性能的 Web 和反向代理服務器, 它具有有很多非常優越的特性:
作為 Web 服務器:相比 Apache,Nginx 使用更少的資源,支持更多的并發連接,體現更高的效率,這點使 Nginx 尤其受到虛擬主機提供商的歡迎。能夠支持高達 50,000 個并發連接數的響應,感謝 Nginx 為我們選擇了 epoll and kqueue 作為開發模型.
作為負載均衡服務器:Nginx 既可以在內部直接支持 Rails 和 PHP,也可以支持作為 HTTP代理服務器 對外進行服務。Nginx 用 C 編寫, 不論是系統資源開銷還是 CPU 使用效率都比 Perlbal 要好的多。
作為郵件代理服務器: Nginx 同時也是一個非常優秀的郵件代理服務器(最早開發這個產品的目的之一也是作為郵件代理服務器),Last.fm 描述了成功并且美妙的使用經驗。
Nginx 安裝非常的簡單,配置文件 非常簡潔(還能夠支持perl語法),Bugs非常少的服務器: Nginx 啟動特別容易,并且幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啟動。你還能夠在 不間斷服務的情況下進行軟件版本的升級
二、軟件獲得及幫助文檔
官方網址:http://nginx.org/
下載穩定版本:http://nginx.org/download/nginx-1.8.0.tar.gz
幫助文檔:http://nginx.org/en/docs/
編譯參數說明:http://nginx.org/en/docs/configure.html
三、軟件安裝
3.1環境準備:
操作系統:Centos6.6
編譯環境:Development tools
安裝相關依賴包:yum install pcre-devel openssl-devel libxml2-devel libxslt-devel gd-devel GeoIP-devel -y
3.2解壓安裝:
tar zxvf nginx-1.8.0.tar.gz -C /root/nginx
cd /root/nginx/nginx-1.8.0/
./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-http_spdy_module –with-http_realip_module –with-http_addition_module –with-http_xslt_module –with-http_image_filter_module –with-http_geoip_module –with-http_sub_module –with-http_dav_module –with-http_flv_module –with-http_mp4_module –with-http_gunzip_module –with-http_gzip_static_module –with-http_auth_request_module –with-http_random_index_module –with-http_secure_link_module –with-http_degradation_module –with-http_stub_status_module
make && make install
四、配置文件
vi /usr/local/nginx/conf/nginx.conf
#運行用戶
user nobody;
#啟動進程,通常設置成cpu的(核數-1),留一核給操作系統使用
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;優化CPU性能,使一個nginx進程綁定一棵CPU,減少進程間切換
worker_rlimit_nofile 65535;
#全局錯誤日志及PID文件
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#工作模式及連接數上限
events {
#epoll是多路復用IO(I/O Multiplexing)中的一種方式,
#僅用于linux2.6以上內核,可以大大提高nginx的性能
use epoll;
#單個后臺worker process進程的最大并發鏈接數
worker_connections 1024;
# 并發總數是 worker_processes 和 worker_connections 的乘積
# 即 max_clients = worker_processes * worker_connections
# 在設置了反向代理的情況下,max_clients = worker_processes * worker_connections / 4 為什么
# 為什么上面反向代理要除以4,個人理解:由于設置了代理,每個客戶請求將會發給代理,代理轉給后臺服務器,后臺響應代理,代理返回給客戶,而每次處理都對應相應的進程,所以每個客戶請求將對應4個代理服務器進程(即:代理接受客戶端請求的進程、代理將客戶端請求轉給后臺服務器的進程、代理接受后臺服務器響應數據的進程、代理響應客戶端的進程)
# 根據以上條件,正常情況下的Nginx Server可以應付的最大連接數為:4 * 8000 = 32000
# worker_connections 值的設置跟物理內存大小有關
# 因為并發受IO約束,max_clients的值須小于系統可以打開的最大文件數
# 而系統可以打開的最大文件數和內存大小成正比,一般1GB內存的機器上可以打開的文件數大約是10萬左右
# 我們來看看360M內存的VPS可以打開的文件句柄數是多少:
# $ cat /proc/sys/fs/file-max
# 輸出 34336
# 32000 < 34336,即并發連接總數小于系統可以打開的文件句柄總數,這樣就在操作系統可以承受的范圍之內
# 所以,worker_connections 的值需根據 worker_processes 進程數目和系統可以打開的最大文件總數進行適當地進行設置
# 使得并發總數小于操作系統可以打開的最大文件數目
# 其實質也就是根據主機的物理CPU和內存進行配置
# 當然,理論上的并發總數可能會和實際有所偏差,因為主機還有其他的工作進程需要消耗系統資源。
# ulimit -SHn 65535
}
http {
#設定mime類型,類型由mime.type文件定義
include 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"';
access_log logs/access.log main;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,用于優化內核態與用戶態間數據傳輸,減少兩態之間的切換
#對于普通應用,必須設為 on,
#如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,
#以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
#tcp_nopush on;
#連接超時時間
#keepalive_timeout 0;
keepalive_timeout 65;長連接,在客戶端請求完成后保持連接65s,之后強制斷開
tcp_nodelay on;
#開啟gzip壓縮
gzip on;
gzip_disable "MSIE [1-6].";
#設定請求緩沖
client_header_buffer_size 128k;
large_client_header_buffers 4 128k;
#設定虛擬主機配置
server {
#偵聽80端口
listen 80;
#定義使用 www.nginx.cn訪問
server_name www.nginx.cn;
#定義服務器的默認網站根目錄位置
root html;
#設定本虛擬主機的訪問日志
access_log logs/nginx.access.log main;
#默認請求
location / {
#定義首頁索引文件的名稱
index index.php index.html index.htm;
}
# 定義錯誤提示頁面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
#靜態文件,nginx自己處理
location ~ ^/(images|javascript|js|css|flash|media|static)/ {
#過期30天,靜態文件不怎么更新,過期可以設大一點,
#如果頻繁更新,則可以設置得小一點。
expires 30d;
}
#PHP 腳本請求全部轉發到 FastCGI處理. 使用FastCGI默認配置.
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
#禁止訪問 .htxxx 文件
location ~ /.ht {
deny all;
}
}
}
五、通過實驗深刻理解配置參數
5.1將主配置文件nginx.conf中的server段提出為單獨文件,以便配置管理
[root@desktop-vi3t7oc nginx]# cat conf/nginx.conf
…………
http {
…………
include server.conf;
}
5.2驗證GZIP的壓縮比
將以下配置根據實驗需求配置于server.conf文件中的server段中:
gzip on;
gzip_min_length 1000;壓縮的最小頁面1000k起
gzip_comp_level 1;級別1-9
實驗一:未開gzip壓縮時:
實驗二:開啟gzip壓縮時,且壓縮比為1:
實驗三:開啟gzip壓縮時,且壓縮比為9:
結論:開啟gzip壓縮可節約帶寬22%以上
5.3注意默認情況下第一個server將做為默認服務器,所有到達服務器的請求都有可能被默認服務器處理
5.4location段中匹配規則及匹配順序
配置文件修改:
目錄文件:
測試:http://www.cosa.com/----root
http://www.cosa.com/index.html——–nginx
http://www.cosa.com/oracle/————-oracle
http://www.cosa.com/img/——————img
http://www.cosa.com/rr.gif————–圖片
結論:
(location =) > (location 完整路徑) > (location ^~ 路徑) > (location ~,~* 正則順序) > (location 部分起始路徑) > (/)
5.4虛擬主機的配置:
server {
server_name ~^(www\.)?(.+)$;
location / {
root /sites/$2;
}
}
5.5UPSTREAM
upstream backend {
server ftpp.cosa.com weight=2;
server node1.cosa.com;
server node2.cosa.com;
}
server {
listen 80;
server_name www.cosa.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location = / {
root html/root/;
index index.html index.htm;
}
#location / {
# root /usr/local/nginx/html;
# index index.html index.htm;
#}
location / {
proxy_pass http://backend;
}
location /oracle/ {
root /usr/local/nginx/html;
index index.html index.htm;
}
location ^~ /img/ {
root /usr/local/nginx/html/;
index index.html index.htm;
備注:node1.cosa.com—172.16.1.20
node2.cosa.com—–172.16.1.21
實驗現象:精確匹配到根(www.cosa.com)的訪問,其結果是請求被分發到backend后端主機組,且按權重進行輪換;在后端主機組配置成員--基于本機虛擬域名的虛擬主機(ftpp.cosa.com),其沒有被有效加入后端主機組,不能提供服務;
原創文章,作者:sn_home,如若轉載,請注明出處:http://www.www58058.com/10086
內容應該是word編寫好后復制上來的吧。到博客上展示效果挺差