系統環境:RHEL5 [ 2.6.18-8.el5xen ]
軟件環境:
nginx-0.7.17
lighttpd-1.4.20.tar.gz
pcre-6.6-1.1
pcre-devel-6.6-1.1
php-5.1.6-5.el5
參考下載地址:
(最新穩定版為0.6.32)
##########################################################################
一、安裝支持軟件
1、安裝lighttpd以提取spawn-fcgi (如果站點不包含php頁面,可以不安裝spaw-fcgi、PHP)
shell> tar zxvf lighttpd-1.4.20.tar.gz
shell> cd lighttpd-1.4.20/
shell> ./configure && make
shell> cp -p src/spawn-fcgi /usr/sbin/spawn-fcgi
2、安裝pcre和php(以下軟件)
可使用RHEL5自帶的rpm包安裝,過程略。
二、安裝nginx
shell> tar zxvf nginx-0.7.17.tar.gz
shell> cd nginx-0.7.17/
shell> ./configure --prefix=/opt/nginx --with-http_stub_status_module --with-http_ssl_module
shell> make && make install
shell> ln -sf /opt/nginx/sbin/nginx /usr/sbin/
三、nginx運行控制
1、檢查配置文件有無語法錯誤
shell> nginx -t
2、啟動(不帶任何參數直接運行即可)
shell> nginx
3、重新加載nginx配置
shell> killall -s HUP nginx #//或者 killall -1 nginx
4、處理完當前請求后退出nginx
shell> killall -s QUIT nginx #//或者 killall -3 nginx
四、nginx配置用例
1、常規配置
shell> vi /opt/nginx/conf/nginx.conf
worker_processes 1; #//工作進程數
events {
use epoll; #//增加該事件提高I/O性能
work_connections 4096;
}
http {
include mime.types;
default_types application/octet-stream;
sendfile on;
tcp_nodelay on
keepalive_timeout 60;
server {
listen 80; #//設置監聽端口,注意不要和Apache等其他Web程序沖突
server_name ; #//指定使用的主機名
charset utf-8; #//指定站點文件的默認編碼
location / {
root html; #//設置網站根目錄
index index.html index.html;
}
error_page 500 502 503 504 /50x.html
location = /50x.html {
root html;
}
}
}
2、添加狀態監控
shell> vi /opt/nginx/conf/nginx.conf #//增加以下內容
location ~ ^/NginxStatus/ {
stub_status on;
access_log off;
}
shell> killall -1 nginx
#//使用瀏覽器訪問 即可看到狀態統計頁面。(三個數字分別表示:總共處理連接數、成功創建的握手次數、總共處理的請求數)
3、通過FastCGI方式支持PHP語言
1)啟動FastCGI服務(用php-cgi做實際處理php頁面的程序,用spawn-fcgi是便于同時開啟多個php-cgi進程——“-C”選項控制子進程數)
shell>/usr/sbin/spawn-fcgi -a 127.0.0.1 -p 9000 -f /usr/bin/php-cgi -C 10
2)修改/opt/nginx/conf/nginx.conf配置文件,添加以下內容:
location ~ /.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
3)重新加載配置
shell> killall -1 nginx
4、虛擬主機設置
修改nginx.conf文件,增加一個server {……}配置即可,每個虛擬主機的參數可以獨立配置。
http {
server {
listen 80;
server_name ;
access_log logs/vhost1.access.log main;
location / {
index index.html;
root /var/www/vhost1; #//第1個虛擬主機的網頁根目錄
}
}
server {
listen 80;
server_name ;
access_log logs/vhost2.access.log main;
location / {
index index.html;
root /var/www/vhost2; #//第2個虛擬主機的網頁根目錄
}
}
}
5、基于反向代理的負載均衡
修改nginx.conf文件,增加upstream配置,指定對應服務器群的IP和權重,并調整server段中的網頁根目錄配置。使訪問nginx服務器的HTTP請求分散到Web群集中的服務器來處理。
http {
upstream my_web_cluster {
server 192.168.2.11:8000 weight=3;
server 192.168.2.12:8000 weight=3;
server 192.168.2.13:8000 weight=3;
server 192.168.2.14:8000 weight=3;
server 192.168.2.15:8000 weight=3;
}
server {
listen 80;
server_name ;
location / {
proxy_pass ;
proxy_set_header x-real-IP $remote_addr;
}
#//注:其他的location配置段(如關于.php文件的)需注釋掉,否則可能影響該類文件的重定向。
}
}
新聞熱點
疑難解答
圖片精選