linux系統爲Centos 64位
第一步:從http://nginx.org/download/上下載相應的版本(或者wget http://nginx.org/download/nginx-1.9.4.tar.gz直接在Linux上用命令下載)
第二步:解壓 tar -zxvf nginx-1.9.4.tar.gz
第三步:設置一下配置信息 ./configure --prefix=/usr/local/nginx ,或者不執行此步,直接默認配置
第四步:
make 編譯 (make的過程是把各種語言寫的源碼文件,變成可執行文件和各種庫文件)
make install 安裝 (make install是把這些編譯出來的可執行文件和庫文件複製到合適的地方)
在配置信息的時候,也就是在第三步,出現了一下錯誤:
錯誤爲:./configure: error: the HTTP rewrite module requires the PCRE library.
安裝pcre-devel解決問題
yum -y install pcre-devel
還有可能出現:
安裝zlib-devel解決問題
yum -y install zlib-devel
還有可能出現:
錯誤提示:./configure: error: the HTTP cache module requires md5 functions
from OpenSSL library. You can either disable the module by using
--without-http-cache option, or install the OpenSSL library into the system,
or build the OpenSSL library statically from the source with nginx by using
--with-http_ssl_module --with-openssl=<path> options.
解決辦法:
yum -y install openssl openssl-devel
安裝後在linux下啓動和關閉nginx:
啓動操作
/usr/nginx/sbin/nginx (/usr/nginx/sbin/nginx -t 查看配置信息是否正確)
web界面:http://192.168.189.136:80/
停止操作是通過向nginx進程發送信號(什麼是信號請參閱linux文 章)來進行的
步驟1:查詢nginx主進程號
ps -ef | grep nginx
在進程列表裏 面找master進程,它的編號就是主進程號了。
步驟2:發送信號
從容停止Nginx:
kill -QUIT 主進程號
快速停止Nginx:
kill -TERM 主進程號
強制停止Nginx:
pkill -9 nginx
另外, 若在nginx.conf配置了pid文件存放路徑則該文件存放的就是Nginx主進程號,如果沒指定則放在nginx的logs目錄下。有了pid文 件,我們就不用先查詢Nginx的主進程號,而直接向Nginx發送信號了,命令如下:
kill -信號類型 '/usr/nginx/logs/nginx.pid'
平滑重啓
如果更改了配置就要重啓Nginx,要先關閉Nginx再打開?不是的,可以向Nginx 發送信號,平滑重啓。
平滑重啓命令:
kill -HUP 住進稱號或進程號文件路徑
或者使用
/usr/nginx/sbin/nginx -s reload
利用nginx配置Web Server的負載均衡,需要修改的地方有:
a) 在http標籤下,配置upstream屬性,如:
upstream mysvr {
#weigth參數表示權值,權值越高被分配到的機率越大
server 192.168.189.136:8080 weight=5;
server 192.168.189.137:8080 weight=1;
server 192.168.189.138:8080 weight=6;
}
其中,weight是可選項;若不配置weight,則默認所有的Web Server具有相同的權重。
b) 在http標籤下的server標籤中,做適當的修改,如:
server {
listen 83;
server_name localhost;
location / {
root /root;
index index.jsp;
proxy_pass http://mysvr;
proxy_set_header Host $host:83;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
……
}
其中需要說明的是,server_name和listen分別設置的是用戶訪問的地址和端口;proxy_pass指定轉向的服務器列表(在upstream中定義);proxy_set_header Host設置跳轉的http報文中的地址爲proxy_pass中指定的地址,同時可以在此處指定端口號,若不指定,則默認會跳到80端口。
本例服務器採用tomcat
輸入如下網址測試:
http://192.168.189.136:80/examples/
http://192.168.189.136:8080/examples/
http://192.168.189.137:8080/examples/
http://192.168.189.138:8080/examples/