Nginx 編譯安裝

簡介

Nginx ("engine x") 是一個高性能的HTTP和反向代理服務器,也是一個IMAP/POP3/SMTP服務器。Nginx是由Igor Sysoev為俄羅斯訪問量第二的Rambler.ru站點開發的,第一個公開版本0.1.0發布于2004104日。其將源代碼以類BSD許可證的形式發布,因它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名。201161日,nginx 1.0.4發布。

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,并在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是占有內存少,并發能力強,事實上nginx的并發能力確實在同類型的網頁服務器中表現較好,中國大陸使用nginx網站用戶有:百度、新浪、網易、騰訊等。

 

基本功能:

         靜態資源的web服務器,能緩存打開的文件 描述符

         http, smtp, pop3協議的反向代理服務器,緩存、負載均衡;

         支持FastCGI (fpm)

         模塊化,非DSO機制,過濾器zipSSI及圖像大小調整;

         支持SSL

        

擴展功能:

         基于名稱和IP的虛擬主機;

         支持keepalive

         支持平滑升級

         定制訪問日志 ,支持使用日志緩沖區提高日志存儲性能

         支持url rewrite

         支持路徑別名

         支持基于IP及用戶的訪問控制

         支持速率限制,支持并發數限制

 

Nginx的特性:

         模塊化設計、較好擴展性

         高可靠性

                   master–>worker

         低內存消耗

                   10000keep-alive連接在Nginx僅消耗2.5MB

         支持熱部署

                   不停機而更新配置文件、更換日志文件、更新服務器程序版本

 

         支持事件驅動,AIO,MMAP

 

軟件獲取和幫助文檔

         官方地址:http://nginx.org

         目前穩定版本是:http://nginx.org/download/nginx-1.8.1.tar.gz

         幫助文檔:http://nginx.org/en/docs

編譯參數說明:http://nginx.org/en/docs/configure.html

 

安裝前裝備

操作系統:CentOS 6.7 64

Nginx 版本:nginx-1.8.1

Web服務器ip:172.28.0.59

 

1.  配置好IPDNS 、網關,確保使用遠程連接工具能夠連接服務器

2.  配置防火墻,iptables –F 清理防火墻規則或者關閉iptables

3.  關閉SELINUX, setenforce 0  #立即生效(實際是寬容模式)

 

安裝配置

1、  解決依賴關系

編譯安裝nginx需要事先需要安裝開發包組"Development Tools" "Development Libraries"。同時,還需要專門安裝pcre-devel包:

yum -y groupinstall "Development tools" "Server Platform Development"

yum -y install pcre-devel  openssl-devel

 

2、安裝

首先添加用戶nginx,實現以之運行nginx服務進程:

groupadd -r nginx    #創建系統組nginx

useradd -g nginx -r nginx  -s /sbin/nologin #創建用戶nginx并添加進nginx

 

3、接著開始編譯和安裝:

./configure –prefix=/usr/local/nginx –conf-path=/etc/nginx/nginx.conf –user=nginx –group=nginx –error-log-path=/var/log/nginx/error.log –http-log-path=/var/log/nginx/access.log –pid-path=/var/run/nginx/nginx.pid –lock-path=/var/lock/nginx.lock –with-http_ssl_module –with-http_stub_status_module –with-http_gzip_static_module –with-http_flv_module –with-http_mp4_module –http-client-body-temp-path=/var/tmp/nginx/client –http-proxy-temp-path=/var/tmp/nginx/proxy –http-fastcgi-temp-path=/var/tmp/nginx/fastcgi

 

make && make install

mkdir -pv /var/tmp/nginx/{client,fastcgi,proxy,uwsgi}   #創建以上指定的幾個目錄

 

   4、部分編譯參數說明

    –conf-path=/etc/nginx/nginx.conf                   #主進程安裝的路徑

–user=nginx –group=nginx                      #啟動程序的用戶

–error-log-path=/var/log/nginx/error.log      #錯誤日志的路徑

–http-log-path=/var/log/nginx/access.log    #正常日志的文件路徑

–pid-path=/var/run/nginx/nginx.pid              #pid 文件路徑

–lock-path=/var/lock/nginx.lock             #鎖文件路徑

–with-http_ssl_module                                      #支持https

–with-http_stub_status_module                     #支持狀態監控

–with-http_gzip_static_module                       #支持gzip壓縮

–with-http_flv_module                             #流媒體支持

–with-http_mp4_module                                  #mp4支持

–http-client-body-temp-path=/var/tmp/nginx/client      #設定http客戶端請求臨時文件路徑

–http-proxy-temp-path=/var/tmp/nginx/proxy             #設定http代理臨時文件路徑

–http-fastcgi-temp-path=/var/tmp/nginx/fastcgi            #設定http fastcgi臨時文件路徑

        

  5、為nginx提供SysV init腳本:

   新建文件/etc/rc.d/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/local/nginx/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

 

而后為此腳本賦予執行權限:

chmod +x /etc/rc.d/init.d/nginx

添加至服務管理列表,并讓其開機自動啟動:

chkconfig –add nginx

chkconfig nginx on

 

設置一個連接方便以后檢查語法使用

ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx

 

而后就可以啟動服務并測試:

service nginx start            #啟動nginx

netstat –nltp                       #查看是否監聽80端口

blob.png

blob.png

 

6、配置主頁面

         /etc/nginx/nginx.conf                #主配置文件

         /usr/local/nginx/html                #網站根目錄

         /usr/local/nginx/sbin/nginx              #nginx主程序

 

         cd /usr/local/nginx/html

         vi index.html

         <h1> welcome to china! </h1>

This is test site

 

Service nginx reload         #重新加載配置文件

blob.png

 

nginx 基本功能實現

1、  配置虛擬主機

mkdir -pv /vhosts/web1

vi /vhosts/web1/index.html

           www.web1.com

vi /etc/nginx/nginx.conf

   server {

        listen  8080;    

        server_name www.test.com;

        location  / {

                root /vhosts/web1;

                index  index.html index.htm;

        }

          }

 

nginx –t   #檢查語法錯誤

blob.png

service nginx reload  #重新加載配置文件

blob.png

2、  基于用戶認證

基于用戶的basic認證配置:

           auth_basic "test"    #顯示名字是test,可以隨意填寫

           auth_basic_user_file      #賬號文件路徑

                    htpasswd命令創建用戶賬號文件;

 

                    例子:

vi /etc/nginx/nginx.conf

     server {

        listen  8080;

        server_name www.stu1.com;

               location  / {

               root /vhosts/web1;

               auth_basic "www.stu1.com";  

               auth_basic_user_file /vhosts/web1/.passwd;

        }

}

安裝httpd-tools 工具

yum -y install httpd-tools

htpasswd -c -m /www/html/.passwd centos    #創建centos用戶

nginx -t    #檢查Nginx配置文件語法

service nginx reload

blob.png

3、  基于IP認證訪問控制

http, server, location        #這幾個里面都可以配置

allow         IP/Network

deny        IP/Network

例子:

vi /etc/nginx/nginx.conf

     server {

        listen  8080;

        server_name www.stu1.com;

               location  / {

               root /vhosts/web1;

deny 172.28.1.0/24;

               allow all;

       }

}

 

service nginx reload  重新加載配置文件

blob.png

4、  重定向錯誤頁面

error_page code […] [=code] URI | @name

根據http狀態碼重定向錯誤頁面

error_page  404   /404.html

=[code]: 以指定的響應碼進行響應;省略code表示以新資源的響應碼為響應碼;

 

例子:

vi /etc/nginx/nginx.conf

     server {

        listen  8080;

        server_name www.stu1.com;

               location  / {

               root /vhosts/web1;

                               error_page 404 =200  /404_customed.html;

       }

}

vi /vhosts/web1/404_customed.html

<h1>o_o,Wrong.</h1>

 

blob.png

 

5、  基于gzip壓縮

vi /etc/nginx/nginx.conf

 

server {

listen  8080;

server_name www.stu1.com;

       location  / {

       root /vhosts/web1;

       gzip on;

       gzip_comp_level 6;

         }

}

 

Service nginx reload

blob.png

blob.png

 

6、  定制響應首部

add_header name value [always];

expires

例子:

vi /etc/nginx/nginx.conf

 

             server {

        listen 8080;

        server_name www.stu1.com;

        location / {

        root "/vhosts/web1";

        expires 24h;              #控制頁面緩存時間的作用

        add_header stu1 admin;          #響應首部

 

        }

    }

 

service nginx reload

blob.png

7、  URL 重定向

     rewrite regex replacement [flag];

         last            #rewrite規則重寫完成后,就不在后面其他的rewrite規則進行處理,而是由User Agent 重新對重寫后的URL再一次發起請求,并從頭開始執行類似的過程

         break        #一旦此rewrite規則重寫完成后,由User Agent 對新的URL重新發起請求,且不再會被當前location內的任何rewrite 規則所檢查

         redirect             #302響應碼(臨時重定向)返回新的URL

                   permanent       #301響應碼(永久重定向)返回新的URL

例子:

vi /etc/nginx/nginx.conf

 

    server {

        listen 8080;

        server_name www.stu1.com;

        location / {

        root "/vhosts/web1";

        rewrite ^/images/(.*)$ /imgs/$1 break;

        }

}

         mkdir -pv /vhosts/web1/{images,imgs}

         vi /vhosts/web1/images/index.htm

                   /vhosts/web1/images/

         vi /vhosts/web1/imgs/index.htm

                  /vhosts/web1/imgs

 

service nginx reload

blob.png

 

8、  開啟Nginx狀態監控的功能

vi /etc/nginx/nginx.conf

 

    server {

        listen 8080;

        server_name www.stu1.com;

        location / {

        root "/vhosts/web1";

      }

        location /status {

                stub_status on;

                access_log off;

                allow 192.168.1.0/24;

                deny all;

      }                 

}

blob.png

 

9、  防盜鏈

   server {

        listen 80;

        server_name www.stu1.com;

        location / {

        root "/vhosts/web1";

      }

        location ~*\.(jpg|gif|gpeg|png)$ {

                valid_referers none blocked www.stu1.com;

                if ($invalid_referer) {

                rewrite ^/ http://www.stu1.com/403.html;

            }

       }

}

 

vi /vhosts/web1/index.html

Test Page

<img src="http://www.stu1.com/123.jpg">

 

 service nginx reload

blob.png

10、         https 服務

a)         創建私有CA并自簽證書

cd /etc/pki/CA

(umask 077;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)

openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 7300

touch serial index.txt

echo 01 > serial

b)        創建nginx證書

 cd /etc/nginx/

mkdir ssl

cd ssl/

(umask 077; openssl genrsa -out nginx.key 1024)

openssl req -new -key nginx.key -out nginx.csr

c)   簽署證書

openssl ca -in nginx.csr -out nginx.crt -days 365

d)        修改nginx主配置文件

server {

        listen       443 ssl;

        server_name  www.stu1.com;

        ssl_certificate      /etc/nginx/ssl/nginx.crt;

        ssl_certificate_key  /etc/nginx/ssl/nginx.key;

        location / {

            root   /vhosts/web1;

            index  index.html index.htm;

        }

 

nginx –t

service nginx reload

blob.png

原創文章,作者:liangkai,如若轉載,請注明出處:http://www.www58058.com/61406

(0)
liangkailiangkai
上一篇 2016-12-01
下一篇 2016-12-01

相關推薦

  • nginx+keepalived構建負載均衡代理服務器

    實驗環境: 1、centos7.3 centos6.82、兩臺VS提供nginx代理和keepalived3、兩臺RS提供httpd服務并部署wordpress4、另外一臺提供Mysql數據庫服務 實驗拓撲圖: 拓撲圖 實驗步驟 1、在vs1(172.18.251.4)上配置,用yum安裝keepalived和nginx yum install -y kee…

    Linux干貨 2017-05-21
  • ifconfig命令學習

    ifconfig命令 網絡配置 ifconfig命令被用于配置和顯示Linux內核中網絡接口的網絡參數。用ifconfig命令配置的網卡信息,在網卡重啟后機器重啟后,配置就不存在。要想將上述的配置信息永遠的存的電腦里,那就要修改網卡的配置文件了。 語法 ifconfig(參數) 參數 add<地址>:設置網絡設備IPv6的ip地址; del&lt…

    Linux干貨 2017-07-02
  • 計劃任務管理

        Linux系統計劃任務有兩種:A、一次性任務;B、定時循環任務。     一次性任務:at命令。at的安裝包名字就是at,可以使用命令"yum install at"進行安裝.安裝完成后,啟用atd服務就可以運行at命令了.運行格式為:at [opti…

    Linux干貨 2016-11-27
  • 網絡管理

    網絡概念 網絡應用程序 Web 瀏覽器(Chrome、IE、Firefox等) 即時消息(QQ、微信、釘釘等) 電子郵件(Outlook、foxmail 等) 協作(視頻會議、VNC、Netmeeting、WebEx 等) web網絡服務(apache,nginx,IIS) 文件網絡服務(ftp,nfs,samba) 數據庫服務( MySQL,MariaDB…

    Linux干貨 2017-05-06
  • 用戶權限過濾練習(w4)

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其他用戶均沒有任何訪問權限。 [root@keyou ~]# cp -r /etc/skel/ /home/tuser1 | chmod -R g-rwx,o-rw…

    系統運維 2016-11-20
  • 網絡工具

    測試網絡 顯示主機名     hostname     centos6 /etc/sysconfig/network     更改主機名        &nbs…

    Linux干貨 2016-09-09
欧美性久久久久