nginx介紹
php
nginx是一個輕量級高性能的HTTP服務、反向代理服務和電子郵件(IMAP\POP3)代理服務器。nginx
優勢:c++
做爲Web服務器:相比Apache而言,nginx使用更少的資源,支持更多的併發鏈接,體現更高的效率;nignx採用epoll and kqueue做爲開發模型,可以支持高達50000個併發鏈接數的響應。數據庫
做爲負載均衡器:Nginx既能夠在內部直接支持Rails和php,也能夠支持做爲HTTP代理服務器和對外進行服務vim
做爲郵件代理服務器:Nginx同時也是一個很是優秀的郵件代理服務器(最先開發這個產品的目的之一也是做爲郵件代理服務器)瀏覽器
Nginx安裝簡單,配置文件簡介(支持perl語法)bash
使用yum安裝nginx服務器
#關閉防火牆 [root@y-nginx ~]# systemctl stop firewalld [root@y-nginx ~]# setenforce 0 #配置yum源 [root@y-nginx ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo [root@y-nginx ~]# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo [root@y-nginx ~]# yum repolist [root@y-nginx ~]# yum install -y nginx [root@y-nginx ~]# systemctl start nginx [root@y-nginx ~]# systemctl enable nginx
#確認ip,在瀏覽器訪問該ip地址,出現如下頁面表示安裝成功併發
#查看nginx安裝版本 [root@y-nginx ~]# nginx -V nginx version: nginx/1.12.2 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) built with OpenSSL 1.0.2k-fips 26 Jan 2017 TLS SNI support enabled
nginx編譯安裝與配置使用
負載均衡
#安裝編譯環境 [root@b-nginx ~]# yum install -y gcc gcc-c++ #安裝pcre軟件包(使nginx支持http rewrite模塊) yum install -y pcre pcre-devel #安裝openssl-devel(使nginx支持ssl) yum install -y openssl openssl-devel #安裝zlib yum install -y zlib zlib-devel #建立用戶nginx [root@b-nginx ~]# useradd nginx [root@b-nginx ~]# passwd nginx Changing password for user nginx. New password: BAD PASSWORD: The password is shorter than 8 characters Retype new password: passwd: all authentication tokens updated successfully.
安裝nginx
[root@b-nginx ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz
[root@b-nginx ~]# ls anaconda-ks.cfg nginx-1.14.2.tar.gz [root@b-nginx ~]# tar xf nginx-1.14.2.tar.gz -C /usr/local [root@b-nginx ~]# cd /usr/local [root@b-nginx local]# cd nginx-1.14.2/
[root@b-nginx nginx-1.14.2]# ./configure \--group=nginx \--user=nginx \--prefix=/usr/local/nginx \--sbin-path=/usr/sbin/nginx \--conf-path=/etc/nginx/nginx.conf \--error-log-path=/var/log/nginx/error.log \--http-log-path=/var/log/nginx/access.log \--http-client-body-temp-path=/tmp/nginx/client_body \--http-proxy-temp-path=/tmp/nginx/proxy \--http-fastcgi-temp-path=/tmp/nginx/fastcgi \--pid-path=/var/run/nginx.pid \--lock-path=/var/lock/nginx \--with-http_stub_status_module \--with-http_ssl_module \--with-http_gzip_static_module \--with-pcre
#模塊功能具體參數 --prefix=/usr/local/nginx //指向安裝目錄 --conf-path=/etc/nginx/nginx.conf //指定配置文件 --http-log-path=/var/log/nginx/access.log //指定訪問日誌 --error-log-path=/var/log/nginx/error.log //指定錯誤日誌 --lock-path=/var/lock/nginx.lock //指定lock文件 --pid-path=/run/nginx.pid //指定pid文件 --http-client-body-temp-path=/var/lib/nginx/body //設定http客戶端請求臨時文件路徑 --http-fastcgi-temp-path=/var/lib/nginx/fastcgi //設定http fastcgi臨時文件路徑 --http-proxy-temp-path=/var/lib/nginx/proxy //設定http代理臨時文件路徑 --http-scgi-temp-path=/var/lib/nginx/scgi //設定http scgi臨時文件路徑 --http-uwsgi-temp-path=/var/lib/nginx/uwsgi //設定http uwsgi臨時文件路徑 --with-debug //啓用debug日誌 --with-pcre-jit //編譯PCRE包含「just-in-time compilation」 --with-ipv6 //啓用ipv6支持 --with-http_ssl_module //啓用ssl支持 --with-http_stub_status_module //獲取nginx自上次啓動以來的狀態 --with-http_realip_module //容許從請求標頭更改客戶端的IP地址值,默認爲關 --with-http_auth_request_module //實現基於一個子請求的結果的客戶端受權。若是該子請求返回的2xx響應代碼,所述接入是容許的。若是它返回401或403中,訪問被拒絕與相應的錯誤代碼。由子請求返回的任何其餘響應代碼被認爲是一個錯誤。 --with-http_addition_module //做爲一個輸出過濾器,支持不徹底緩衝,分部分響應請求 --with-http_dav_module //增長PUT,DELETE,MKCOL:建立集合,COPY和MOVE方法 默認關閉,需編譯開啓 --with-http_geoip_module //使用預編譯的MaxMind數據庫解析客戶端IP地址,獲得變量值 --with-http_gunzip_module //它爲不支持「gzip」編碼方法的客戶端解壓具備「Content-Encoding: gzip」頭的響應。 --with-http_gzip_static_module //在線實時壓縮輸出數據流 --with-http_image_filter_module //傳輸JPEG/GIF/PNG 圖片的一個過濾器)(默認爲不啓用。gd庫要用到) --with-http_spdy_module //SPDY能夠縮短網頁的加載時間 --with-http_sub_module //容許用一些其餘文本替換nginx響應中的一些文本 --with-http_xslt_module //過濾轉換XML請求 --with-mail //啓用POP3/IMAP4/SMTP代理模塊支持 --with-mail_ssl_module //啓用ngx_mail_ssl_module支持啓用外部模塊支持
[root@b-nginx nginx-1.14.2]# make && make install
#查看nginx版本和安裝的模塊信息
#查看配置文件/etc/nginx/nginx.conf
#檢測nginx配置文件 [root@b-nginx ~]# /usr/sbin/nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful #啓動nginx服務 [root@b-nginx ~]# /usr/sbin/nginx
#nginx控制命令
#修改配置後從新加載生效 nginx -s reload #從新打開日誌文件 nginx -s reopen #中止nginx服務 nginx -s stop #完整有序的中止nginx nginx -s quit #測試當前配置文件是否正確 nginx -t
#實現nginx開機自啓
#添加啓動腳本 [root@b-nginx ~]# vim /etc/init.d/nginx #!/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/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/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:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` 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 } 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 #添加腳本權限 [root@b-nginx ~]# chmod +x /etc/init.d/nginx #重載系統啓動文件 [root@b-nginx ~]# systemctl daemon-reload #設置開機自啓 [root@b-nginx ~]# systemctl start nginx