1、Nginx 簡介php
Nginx("engine x")是一款是由俄羅斯的程序設計師Igor Sysoev所開發高性能的 Web 和反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。nginx
2、Nginx 功能特色c++
1、Nginx 作反向代理服務器vim
2、Nginx 作正向代理服務器瀏覽器
3、Nginx 作 Web 服務器,同 Apache 功能相同服務器
4、Nginx 作負載均衡服務器併發
3、Nginx 安裝部署負載均衡
1、在安裝 Nginx 前,咱們先安裝一下下面的支持軟件。ide
[root@nginx ~]# yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel性能
2、在官網下載 Nginx 最新的安裝包
[root@nginx ~]# wget -c http://nginx.org/download/nginx-1.14.1.tar.gz
3、解壓並編譯
[root@nginx ~]# tar -zxvf nginx-1.14.1.tar.gz && cd nginx-1.14.1/
[root@nginx nginx-1.14.1]# ./configure --prefix=/usr/local/nginx --user=www --group=www --error-log-path=/var/log/wwwlogs/nginx_error.log --http-log-path=/var/log/wwwlogs --conf-path=/usr/local/nginx/conf/nginx.conf --pid-path=/usr/local/nginx/var/nginx.pid --user=www --group=www --with-http_degradation_module --with-http_ssl_module --with-http_v2_module --with-http_sub_module --with-http_stub_status_module --with-http_gzip_static_module --with-google_perftools_module --with-pcre
[root@nginx nginx-1.14.1]# make && make install
4、將 Nginx 命令作軟鏈接到 /usr/bin 路徑
[root@nginx nginx-1.14.1]# ln -sf /usr/local/nginx/sbin/nginx /usr/bin/nginx
5、修改 Nginx 配置文件
如上圖:
1、爲 Nginx 服務工做用戶和用戶組。
2、爲 Nginx 工做進程數,這個須要根據服務器的 CPU 核心數來設置,通常設置爲和 CPU 的核心數相等便可。
3、參數 worker_rlimit_nofile 爲最大文件打開數(鏈接),可設置爲系統優化後的ulimit -HSn的結果。
4、爲 Nginx 單個後臺進程的最大併發連接數(最大鏈接數=鏈接數*進程數)。
5、爲 Nginx 設置 keep-alive 客戶端鏈接在服務器端保持開啓的超時值(默認75s);值爲0會禁用 keep-alive 客戶端鏈接。
這裏須要修改的主要爲這幾個參數,其餘的參數可在實際環境中按需調整,這裏不作統一設置。
如上圖:
1、爲 Nginx 監聽的端口,server_name 這裏填寫網站域名,index 這裏寫 Nginx 支持的網站後綴,root 爲網站路徑
2、爲開啓 Nginx 可監控模塊,一般用於接第三方監控系統
3、爲配置 Nginx 可監控頁面路徑以及將監控信息發送給本機的 9000 來處理
4、同 3 同樣,一個意思
五、爲配置網站的日誌
4、Nginx 啓動與驗證
1、編寫 Nginx 啓動腳本,這裏直接使用官方的啓動腳本(下載地址:官網腳本地址,此地址供 redhat 系統使用,若是是其餘系統,請到 官網腳本地址,下載),腳本內容以下:
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig: - 85 15
# description: NGINX is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# config: /etc/nginx/nginx.conf
# config: /etc/sysconfig/nginx
# pidfile: /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/bin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/usr/local/nginx/conf/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
make_dirs() {
# make required directories
user=`$nginx -V 2>&1 | grep "configure arguments:.*--user=" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
if [ -n "$user" ]; then
if [ -z "`grep $user /etc/passwd`" ]; then
useradd -M -s /bin/nologin $user
fi
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
fi
}
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
echo -n $"Starting $prog: "
daemon $nginx -c $NGINX_CONF_FILE
retval=$?
echo
[ $retval -eq 0 ] && touch $lockfile
return $retval
}
stop() {
echo -n $"Stopping $prog: "
killproc $prog -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
restart() {
configtest || return $?
stop
sleep 1
start
}
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
force_reload() {
restart
}
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
rh_status() {
status $prog
}
rh_status_q() {
rh_status >/dev/null 2>&1
}
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac
1、將該腳本保存到 /etc/init.d 路徑下,並受權可執行
[root@nginx ~]# vim /etc/init.d/nginx
[root@nginx ~]# chmod 755 /etc/init.d/nginx
2、啓動 Nginx 服務
[root@nginx ~]# service nginx start
3、查看服務進程和端口號
[root@nginx ~]# ps -ef | grep nginx
如圖:
[root@nginx ~]# netstat -tpnl | grep 80
如圖:
4、進行網頁驗證
a、在網站根目錄下建立 phpinfo.php 文件
[root@nginx www]# vim phpinfo.php
以下圖:
b、在瀏覽器中輸入 http://IP/phpinfo.php 地址進行訪問
以下圖: 到此,咱們的 Nginx 服務器部署完成。