Nginx是一個輕量級并有着高性能的http和反向代理伺服器,它對高并發靜态内容的請求有着高效的處理性能。采用epoll屬于非阻塞過程調用模型,一個工作程序可以響應多個請求。
Nginx會按需同時運作多個程序:一個主程序(master)和幾個工作程序(worker),配置了緩存時還會有緩存加載器程序(cache loader)和緩存管理器程序(cache manager)等。所有程序均是僅含有一個線程,并主要通過“共享記憶體”的機制實作程序間通信。主程序以root使用者身份運作,而worker、cache loader和cache manager均應以非特權使用者身份運作。官網位址:nginx.org
主程序主要完成如下工作:
讀取并驗正配置資訊;
建立、綁定及關閉套接字;
啟動、終止及維護worker程序的個數;
無須中止服務而重新配置工作特性;
控制非中斷式程式更新,啟用新的二進制程式并在需要時復原至老版本;
重新打開日志檔案,實作日志滾動;
編譯嵌入式perl腳本;
worker程序主要完成的任務包括:
接收、傳入并處理來自用戶端的連接配接;
提供反向代理及過濾功能;
nginx任何能完成的其它任務;
cache loader程序主要完成的任務包括:
檢查緩存存儲中的緩存對象;
使用緩存中繼資料建立記憶體資料庫;
cache manager程序的主要任務:
緩存的失效及過期檢驗;
安裝Nginx:
先安裝pcre-devel:yum install pcre-devel -y
tar xf nginx-1.6.0.tar.gz
cd nginx-1.6.0
開始編譯:
# ./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_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
編寫服務腳本,放置/etc/init.d目錄,前面文章已經有很多服務腳本的例子,這裡省略。
而後為此腳本賦予執行權限:
chmod +x /etc/rc.d/init.d/nginx
添加至服務管理清單,并讓其開機自動啟動:
chkconfig --add nginx
chkconfig nginx on
而後就可以啟動服務并測試了:
service nginx start
來看看nginx的主配置檔案/etc/nginx/nginx.conf
全局配置的常用指令
worker_processes 1 定義worker程序的個數,一般為cpu的兩倍。
events{
use epoll
worker_connections number; 一個Worker程序的最大連接配接數
}
#clients= worker_connections * work_processes
worker_cpu_affinity 0001 0010 0100 1000; 綁定CPU核心,避免了上下文切換。
worker_rlimit_nofile number; 51200 調整每個使用者所能打開的檔案數量。
/etc/security/limits.conf 也可以通過修改配置檔案調整。
worker_priority -5; 定義程序優先級
http {
include mime.types; #檔案擴充名與檔案類型映射表
default_type application/octet-stream; #預設檔案類型
#charset utf-8; #預設編碼
server_names_hash_bucket_size 128; #伺服器名字的hash表大小
sendfile on; #開啟高效檔案傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出檔案,對于普通應用設為 on,如果用來進行下載下傳等應用磁盤IO重負載應用,可設定為off,以平衡磁盤與網絡I/O處理速度,降低系統的負載。注意:如果圖檔顯示不正常把這個改成off。
autoindex on; #開啟目錄清單通路,合适下載下傳伺服器,預設關閉。
tcp_nopush on; #防止網絡阻塞
tcp_nodelay on; #防止網絡阻塞
keepalive_timeout 5; 持久連接配接逾時時間
server{
listen 80;
index index.html index.htm index.php; 定義首頁
root /web; 定義主目錄
Server_name 定義虛拟主機名
= 精确比對
location = /images/ {
~ 模式比對,區分模式中的字元大小寫
~* 模式比對,不區分大小寫
location ~* \.jpg$ {
} 任何以.jpg結尾的都比對。
^~ 不做模式比對
autoindex on 開啟目錄索引
autoindex_exact_size on 顯示精确大小
autoindex_localtime on 顯示檔案的通路時間
Error_page 404 /404.html 自定義錯誤頁面檔案
Error_page 500 502 504 /50x.html
Log_format 日志格式
Access_log 通路日志
}
}
配置一個基于名稱的虛拟主機。
server {
listen 80;
server_name www.tuchao.com;
root '/web/tuchao';
index index.html;
配置對/web/tuchao/secret/路徑做通路控制。
location /secret {
auth_basic "tuchao's secret";
auth_basic_user_file '/etc/nginx/.htpasswd';
htpasswd -c -s /etc/nginx/.htpasswd tuchao 生成使用者檔案
service nginx reload
通路下試試:
<a href="http://s3.51cto.com/wyfs02/M01/3C/B6/wKioL1PCWnuylOiwAAINTka0Gac937.jpg" target="_blank"></a>
輸入正确的使用者名密碼後,就可以看到内容了。
<a href="http://s3.51cto.com/wyfs02/M00/3C/B6/wKioL1PCWvvDOuT5AAEQcAQBA4g104.jpg" target="_blank"></a>
配置路徑别名(alias)
<a href="http://s3.51cto.com/wyfs02/M01/3C/B6/wKioL1PCXbHDUZpvAADZiBu73Ro581.jpg" target="_blank"></a>
添加自定義響應首部(add_header 子產品)
<a href="http://s3.51cto.com/wyfs02/M01/3C/B6/wKioL1PCYuLh-FMlAAFTOZmljsU313.jpg" target="_blank"></a>
用google浏覽器通路,按F12打開開發人員工具,檢視下。
<a href="http://s3.51cto.com/wyfs02/M00/3C/B6/wKiom1PCY1CT3YxVAAHoQGaw110736.jpg" target="_blank"></a>
Rewrite URL重寫,修改原本請求的位置
控制标志:
Last:當我們把URL重寫完成後,它會重新開機檢查子產品,從頭再檢查一遍
Break:重寫完成後,停止目前的程序,繼續檢查下一個,以避免循環重寫。
Redirect:臨時重定向,顯示重定向。重定向完成,從頭再檢查。
Permanent:傳回永久重定向。
example:www.tuchao.com/bbs/index.php ----> www.tuchao.com/www/html/index.html
我們現在通路下:http://www.tuchao.com/bbs/index.html,在伺服器上,是沒有bbs這個目錄的,通路當然是not found。
<a href="http://s3.51cto.com/wyfs02/M00/3C/B6/wKioL1PCa2HBbnS2AADyRkamfTA328.jpg" target="_blank"></a>
現在我們去配置URL重寫
<a href="http://s3.51cto.com/wyfs02/M01/3C/B6/wKioL1PCbJbjFD7iAAFxiimj6pQ852.jpg" target="_blank"></a>
然後reload服務,再試試看。
<a href="http://s3.51cto.com/wyfs02/M00/3C/B6/wKioL1PCbiLTXbYZAAGXmjNAdlg287.jpg" target="_blank"></a>
啟用status功能:
<a href="http://s3.51cto.com/wyfs02/M00/3C/B7/wKioL1PCj3zy5XEwAAGWlCw0mUI690.jpg" target="_blank"></a>
通路www.tuchao.com/status
<a href="http://s3.51cto.com/wyfs02/M02/3C/B8/wKiom1PCj_TzCG6AAAE7c7L_RWY577.jpg" target="_blank"></a>
建構LNMP環境,先安裝MySQL,然後把php編譯成fpm.
安裝MySQL過程略。。
編譯PHP之前,先安裝依賴包。
bzip2-1.0.5-7.el6_0.i686.rpm
bzip2-devel-1.0.5-7.el6_0.i686.rpm
libmcrypt-2.5.8-4.el5.centos.i386.rpm
libmcrypt-devel-2.5.8-4.el5.centos.i386.rpm
mhash-0.9.9-1.el5.centos.i386.rpm
mhash-devel-0.9.9-1.el5.centos.i386.rpm
mcrypt-2.6.8-1.el5.i386.rpm
libxml2-devel
libcurl-devel
libcurl
./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --with-mysqli=/usr/local/mysql/bin/mysql_config --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib-dir --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-curl
make && make install
為php提供配置檔案:
cp php.ini-production /etc/php.ini
為php-fpm提供Sysv init腳本,并将其添加至服務清單:
cp sapi/fpm/init.d.php-fpm /etc/rc.d/init.d/php-fpm
chmod +x /etc/rc.d/init.d/php-fpm
chkconfig --add php-fpm
chkconfig php-fpm on
為php-fpm提供配置檔案:
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
編輯php-fpm的配置檔案:
vim /usr/local/php/etc/php-fpm.conf
配置fpm的相關選項為你所需要的值,并啟用pid檔案(如下最後一行):
pm.max_children = 150
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pid = /usr/local/php/var/run/php-fpm.pid
接下來可以啟動php-fpm了:
service php-fpm start
<a href="http://s3.51cto.com/wyfs02/M00/3C/B8/wKiom1PCmfaAGBNsAAIyxVYeTYY214.jpg" target="_blank"></a>
整合nginx和php5
1、編輯/etc/nginx/nginx.conf,啟用如下選項:
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
include fastcgi_params;
}
2、編輯/etc/nginx/fastcgi_params,将其内容更改為如下内容:
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
并在所支援的首頁面格式中添加php格式的首頁,類似如下:
location / {
root html;
index index.php index.html index.htm;
而後重新載入nginx的配置檔案:
通過浏覽器通路此測試頁面。php頁面執行成功。
<a href="http://s3.51cto.com/wyfs02/M02/3C/B8/wKioL1PCnlmgSGkNAAMB1scuXdA469.jpg" target="_blank"></a>
配置反向代理伺服器(proxy)
<a href="http://s3.51cto.com/wyfs02/M01/3C/B9/wKiom1PCq9XD15nhAAFTDVVGhW0005.jpg" target="_blank"></a>
成功轉發至192.168.18.100
<a href="http://s3.51cto.com/wyfs02/M00/3C/B8/wKioL1PCq-mz_jHKAAC55JOjItI397.jpg" target="_blank"></a>
檢視192.168.18.100主機的日志
編輯nginx.conf檔案
<a href="http://s3.51cto.com/wyfs02/M01/3C/B9/wKiom1PCsxnD6CRgAAD39QVsjv4993.jpg" target="_blank"></a>
編輯192.168.18.100主機的httpd.conf檔案
<a href="http://s3.51cto.com/wyfs02/M00/3C/B8/wKioL1PCsxuQeVg9AAIaX4UwZFw381.jpg" target="_blank"></a>
重新整理幾次頁面,再看下日志,現在是記錄請求端的IP了。
<a href="http://s3.51cto.com/wyfs02/M01/3C/B8/wKioL1PCs2Lg4Z_cAAUP2YvIxT4105.jpg" target="_blank"></a>
使用反向代理實作動靜分離
<a href="http://s3.51cto.com/wyfs02/M02/3C/B9/wKioL1PCuCvgzfMGAAEGMRCRZ5c132.jpg" target="_blank"></a>
我們通路192.168.18.200/11.jpg,由于這個請求是靜态的,會轉到192.168.18.100。
當我們請求192.168.18.200/index.php時,這個請求會轉發至127.0.0.1:9000,當然這也可以是一個遠端主機的位址,這就實作了,反向代理動靜分離。
<a href="http://s3.51cto.com/wyfs02/M01/3C/B9/wKioL1PCuP-wayTBAAJ9-v6-IfI710.jpg" target="_blank"></a>
調用upstream子產品,配置負載均衡。
<a href="http://s3.51cto.com/wyfs02/M00/3C/F0/wKioL1PDlsLBrx3IAAEwSfER8-E616.jpg" target="_blank"></a>
然後我們仍然通路,nginx伺服器位址192.168.18.200,請求就會輪詢的轉至不同的伺服器上去了。
<a href="http://s3.51cto.com/wyfs02/M00/3C/F1/wKioL1PDmC2RNNZQAADk6Y-SiAc683.jpg" target="_blank"></a>
<a href="http://s3.51cto.com/wyfs02/M01/3C/F1/wKioL1PDmDuyJ3imAADgaUZ1N6A551.jpg" target="_blank"></a>
本來做負載均衡兩台伺服器應該是要一樣的内容,我們這裡實驗,為了更好驗證效果是以設定了不同的内容。
有問題歡迎與我交流QQ1183710107
本文轉自qw87112 51CTO部落格,原文連結:http://blog.51cto.com/tchuairen/1437530