構建基於Nginx的web伺服器

2021-09-20 23:40:16 字數 3547 閱讀 3582

一、簡介

nginx("engine x") 是乙個高效能的http 和反向**伺服器,也是乙個imap/pop3/smtp**伺服器。 nginx 是由

igor sysoev

為俄羅斯訪問量第二的

rambler.ru

二、系統環境

系統平台:rhel 5.4

nginx版本:nginx-1.0.15

三、安裝及配置nginx

1.安裝pcre軟體包,pcre的作用為nginx提供相容perl的正規表示式庫。

預設情況下,nginx只處理靜態的網頁請求,也就是html.如果是來自動態的網頁請求,比如*.php,那麼nginx就要根據正規表示式查詢路徑,然後把*.php交給php去處理,

[root@localhost~]# rpm -ivh pcre-6.6-2.el5_1.7

[root@localhost~]# rpm -ivh pcre-devel-6.6-2.el5_1.7

2.安裝nginx

[root@localhost~]# tar zxvf nginx-1.0.15.tar.gz

[root@localhost~]# cd nginx-1.0.15

[root@localhost nginx-1.0.15]# ./configure

更多的安裝配置

./configure --prefix=/usr/local/nginx

--with-openssl=/usr/include (啟用ssl)

--with-pcre=/usr/include/pcre/ (啟用正規表示式)

–with-pcre=dir (set path to pcre library sources)

注意:set path to pcre library sources是讓你設定到原始碼目錄,而不是編譯安裝後的目錄。

--with-http_stub_status_module (安裝可以檢視nginx狀態的程式)

--with-http_memcached_module (啟用memcache快取)

--with-http_rewrite_module (啟用支援url重寫)

\\其他更多配置選項可以使用./configure --help

命令進行檢視

[root@localhost nginx-1.0.15]# make && make install

四、nginx服務的執行控制

1.新增nginx執行的使用者組:

[root@localhost nginx-1.0.15]# useradd -s /sbin/nologin nginx

[root@localhost nginx-1.0.15]# ln -sf /usr/local/nginx/sbin/nginx  /usr/sbin

3.使用nginx -t命令檢查nginx配置檔案是否有語法錯誤:

[root@linux nginx-1.0.15]# nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

[root@linux nginx-1.0.15]#

執行nginx -t後出現上述提示表示配置檔案語法正確。

4.使用nginx啟動服務,然後使用netstat命令進行檢視:

[root@linux nginx-1.0.15]# netstat -anpt|grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   listen      16787/nginx         

[root@linux nginx-1.0.15]#

5.nginx啟動成功後,可以在瀏覽器中檢視初始的web頁面:

在客戶端瀏覽器中執行:伺服器ip位址)進行檢視:

另外在伺服器命令列下使用文字瀏覽器工具elink進行檢視:

6.使用系統訊號控制nginx程序:

啟動:nginx

重啟:kill -hup `cat /usr/local/nginx/logs/nginx.pid`

[root@localhost~]# kill -s quit nginx  //安全退出,等同於「kill -3 nginx」

[root@localhost~]# kill -s term nginx //快速退出,不等待處理完當前連線

另外,為了方便管理,可以新增乙個nginx服務指令碼,使用chkconfig和service命令管理nginx服務:

[root@localhost~]# vi /etc/init.d/nginx

1 #!/bin/bash2 #description: nginx service control script3

case"$1

"in4 start)5 /usr/sbin/nginx 6 ;;7 stop)8 /usr/bin/killall -s quit nginx9 ;;10 restart)11 $0 stop12 $0 start13 ;;14 reload)15 /usr/bin/killall -s hup nginx16 ;;17 *)18 echo "

usage:$0

"19 exit 1

20 esac21 exit 0

[root@localhost~]# chmod a+x /etc/init.d/nginx    為nginx指令碼賦予可執行許可權

[root@localhost~]# chkconfig --add nginx

[root@localhost~]# chkconfig --level 2345 nginx on

接下來就可以使用service nginx stop|start|restart|reload對nginx服務進行控制:

[root@linux nginx-1.0.15]# service nginx restart

[root@linux nginx-1.0.15]# !nets

netstat -anpt|grep 80

tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   listen      16787/nginx         

[root@linux nginx-1.0.15]#

Dockerfile構建nginx服務

1 dockerfile內容如下 cat opt docker file nginx dockerfile this is my first dockerfile version 1.0 author ljx base images from centos maintainer maintainer...

基於Nginx構建高併發協議解析服務 原創

正文 img 1.nginx服務通過內嵌的perl指令碼動態解析url引數.提取key資訊.perl set storm key new use mime base64 perl use uri escape sub 2.設定storm perl的負載均衡服務 upstream storm perl...

基於HttpListener的web伺服器

前面兩篇文章分別介紹了基於原始socket的web伺服器和基於tcplistener的web伺服器,本篇文章將繼續介紹另外一種基於httplistener的。httplistener進一步的簡化了http協議的監聽,僅需通過字串的方法提供監聽的位址和埠號以及虛擬路徑,就可以開始監聽工作了。設定字首,...