一、Nginx簡介
Nginx ("engine x") 是一個高性能的 HTTP 和反向代理 服務器,主要代理 IMAP/POP3/SMTP 等。Nginx因爲它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。可以運行在大多數版本的操作系統上,佔有內存少,併發能力強,事實上nginx的併發能力確實在同類型的網頁服務器中表現較好,能夠支持高達 50,000 個併發連接數的響應,Nginx採用C進行編寫,幾乎可以做到7*24不間斷運行,即使運行數個月也不需要重新啓動。你還能夠不間斷服務的情況下進行軟件版本的升級。
Nginx 作爲http服務處理靜態文件,索引文件以及自動索引,反向代理加速(無緩存),簡單的負載均衡和容錯,以fastcgi的方式支持PHP,不像apache一樣以模塊的方式支持php
二、nginx安裝配置
gzip模快需要zlib 庫
rewrite模塊需要 pcre 庫
ssl 功能需要openssl庫
1、安裝 pcre-8.32.tar.gz
tar xf pcre-8.32.tar.gz
cd pcre-8.32
./configure --prefix=/usr/local/pcre
make&& make install
2、編譯安裝nginx-1.5.4.tar.gz
./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module
make &&make install
添加軟連接ln -s /usr/local/lib/libpcre.so.1 /lib64/
配置文件測試 /usr/local/nginx/sbin/nginx –t
啓動Nginx /usr/local/nginx/sbin/nginx
瀏覽器訪問驗證:
三、nginx負載均衡
1、配主置文檔nginx.conf (http模塊添加以下)
upstream webserver{
server 192.168.126.133:80 weight=2;
server 192.168.126.131:80 weight=2;
}
server {
listen80;
server_namelocalhost;
location / {
proxy_pass http://webserver;
2、客戶端一配置
echo “this isreal-server1” >/var/www/html/index.html
/etc/init.d/iptablesstop
vim/etc/sysctl.conf
net.ipv4.ip_forward=1
service httpd start
3、客戶端二配置
Echo “this isreal-server2” >/var/www/html/index.html
/etc/init.d/iptablesstop
Vim/etc/sysctl.conf
net.ipv4.ip_forward=1
service httpdstart
4、驗證
四、反向代理
1、配主置文檔nginx.conf (server模塊添加以下)
location / {
proxy_pass http://192.168.126.131;
2、客戶端配置
Echo “this isproxy server ” >/var/www/html/index.html
/etc/init.d/iptablesstop
Vim/etc/sysctl.conf
net.ipv4.ip_forward=1
service httpdstart
3、驗證