Nginx之開門見山
目錄:
一、簡介
二、安裝
三、服務(wù)控制
四、配置文件說明
五、應(yīng)用實(shí)例
一、簡介
Nginx是一款開放源代碼的高性能HTTP服務(wù)器、反向代理服務(wù)器,同時(shí)支持IMAP/POP3代理服務(wù),這是一款自由的軟件。
Igor Ysyoev在2002年開始開發(fā)該軟件,于2004年發(fā)布第一個公開版本。
Nginx以高性能、高可用、豐富的功能模塊、簡單明了的配置文檔以及低資源占用而著稱。Nginx采用最新的網(wǎng)絡(luò)I/O模型,支持高達(dá)50 000個并發(fā)連接。
近年來Nginx在國內(nèi)取得了突飛猛進(jìn)的發(fā)展,很多門戶網(wǎng)站開始提供Nginx解決方案。
二、安裝
Nginx軟件可以到官網(wǎng)下載:http://nginx.org/en/download.html,目前最新的穩(wěn)定版為1.2.2。本文運(yùn)行環(huán)境為Nginx-1.2.2,CentOS6.2。
1. 二進(jìn)制包安裝:
(nginx for CentOS/Red Hat):
[root@centos6 ~]# cat > /etc/yum.repos.d/nginx.repo <<EOF
- >[nginx]
- >name=nginx rpm
- >baseurl=http://nginx.org/packages/centos/6/x86_64/
- >gpgcheck=0
- >enabled=1
- >EOF
[root@centos6 ~]# yum clean all
[root@centos6 ~]# yum install nginx-1.2.2
注意:http://nginx.org/packages/centos/6/x86_64/,路徑需要根據(jù)你自己的實(shí)際系統(tǒng)版本與架構(gòu)填寫。 |
(nginx for Debain/Ubuntu):
jacob@ubuntu ~$ sudo cat >> /etc/apt/sources.list <<EOF
- >deb http://nginx.org/packages/ubuntu/ lucid nginx
- >deb-src http://nginx.org/packages/ubuntu/ lucid nginx
- >EOF
jacob@ubuntu ~$ sudo atp-get update
jacob@ubuntu ~$ sudo apt-get install nginx
2. 源碼安裝:
[root@centos6 ~]# wget http://nginx.org/download/nginx-1.2.2.tar.gz
[root@centos6 ~]# tar -xzf nginx-1.2.2.tar.gz -C /usr/src
[root@centos6 ~]# cd /usr/src/nginx-1.2.2
[root@centos6 ~]# ./configure --prefix=/usr/local/nginx \
> --with-ipv6 \
> --with-http_ssl_module \
> --with-http_realip_module \
> --with-http_addition_module \
> --with-http_image_filter_module \
> --with-http_dav_module \
> --with-http_flv_module \
> --with-http_mp4_module \
> --with-http_gzip_static_module \
> --with-http_perl_module \
> --with-mail \
> --with-mail_ssl_module
注意:
configure執(zhí)行時(shí)會依賴與一些其他程序,如會出現(xiàn)報(bào)錯信息(./configure: error: C compiler gcc is not found)可根據(jù)提示安裝相應(yīng)的軟件包即可。 常見的軟件包有(gcc,pcre,pcre-devel,openssl,openssl-devel,gd,gd-devel,perl- 5.10.1-127,perl-ExtUtils),其中g(shù)d-devel在CentOS6.2光盤中沒有,需要從互聯(lián)網(wǎng)安裝。 configure的選項(xiàng)可以根據(jù)的自己需求輸入,最簡單的就是./configure不添加任何選項(xiàng),所有設(shè)置均為默認(rèn)。 |
[root@centos6 ~]# make
[root@centos6 ~]# make install
3.進(jìn)階:
Nginx核心模塊包括主模塊、事件模塊,以下這些模塊將被configure自動編譯,除非configure時(shí)指定相應(yīng)的--without。
模塊名稱 描述 禁用選項(xiàng)
Core 控制端口,錯誤頁面別名等核心功能 --without-http
Access 基于IP的訪問控制 --without-http_access_module
Auth Basic HTTP用戶認(rèn)證模塊 --without-http_auth_basic_module
Auto Index 自動目錄索引 --without-http_autoindex_module
Browser 描述用戶代理 --without-http_charset_module
Charset 重新編碼網(wǎng)頁 --without-http_charset_module
Empty GIF 內(nèi)存中存放一個圖片 --without-http_empty_gif_module
FastCGI FastCGI支持 --without-http_fastcgi_module
Geo 支持IP變量設(shè)置 --without-http_geo_module
Gzip Gzip壓縮 --without-http_gzip_module
Headers 設(shè)置http響應(yīng)的頭部信息
Index 首頁
Limit Requests 限制客戶端連接頻率 --without-http_limit_req_module
Limit Conn 會話的并發(fā)連接 --without-http_limit_conn_module
Log 自定義日志
Map 設(shè)置變量 --without-http_map_module
Memcached Memcache支持 --without-http_memcached_module
Referer 基于Referer頭部信息過濾 --without-http_referer_module
Rewrite 使用正則表達(dá)式重寫請求 --without-http_rewrite_module
SCGI 支持SGCI協(xié)議 --without-http_scgi_module
Upstream 負(fù)載均衡 --without-http_upstream_ip_hash_module
接下來的這些模塊屬于附加模塊,編譯軟件時(shí)需要通過configure指定開啟:
模塊名稱 描述 開啟選項(xiàng)
Embedded Perl 支持Perl --with-http_perl_module
FLV 支持flash視頻 --with-http_flv_module
GeoIP 通過IP變量實(shí)現(xiàn)負(fù)載均衡 --with-http_geoip_module
Google Perftools 支持谷歌的性能優(yōu)化工具 --with-google_perftools_module
Gzip Precompression 壓縮靜態(tài)文件 --with-http_gzip_static_module
Image Filter 轉(zhuǎn)換圖形的過濾器 --with-http_image_filter_module
MP4 支持MP4 --with-http_mp4_module
Real IP 使用Nginx作為后端服務(wù)器 --with-http_realip_module
Secure Link 使用密鑰保護(hù)頁面 --with-http_secure_link_module
SSL 支持HTTPS/SSL --with-http_ssl_module
Stub Status 查看服務(wù)器狀態(tài) --with-http_stub_status_module
WebDAV 支持WebDAV --with-http_dav_module
郵件模塊:
模塊名稱 描述 啟用/禁用選項(xiàng)
Core 郵件代理功能 --with-mail
--without-mail_pop3_module
--without-mail_imap_module
--without-mail_smtp_module
Auth 郵件認(rèn)證
Proxy 郵件代理
SSL 支持SSL/TLS郵件協(xié)議 --with-mail_ssl_module
第三方模塊:當(dāng)需要添加第三方模塊時(shí),同樣需要在configure時(shí)指定需要添加的模塊,格式如下。
./configure --add-module=/path/module1 \
--add-module=/path/module2
... ...
在此介紹的模塊僅為Nginx常用模塊及描述,具體使用方法可期待后期文章... ...
三、控制Nginx服務(wù)
1. 啟動服務(wù):
[root@centos6 ~]# /usr/local/nginx/sbin/nginx
[root@centos6 ~]# ps aux | grep nginx //查看Nginx進(jìn)程信息
2. 停止服務(wù):
[root@centos6 ~]# kill -QUIT `cat /usr/local/nginx/logs/nginx.pid`
我們可以通過傳輸信號給主進(jìn)程ID號來管理Nginx服務(wù),Nginx主進(jìn)程ID號默認(rèn)存儲在/usr/local/nginx/logs/nginx.pid文件中,
該文件名可以通過configure修改,或者在Nginx主配置文件中通過pid指定。
Nginx支持以下管理信號:
信號 描述
TERM,INT 快速關(guān)閉
QUIT 優(yōu)雅關(guān)閉
HUP 重新加載配置文件(等同于重啟)
USR1 重啟加載日志文件
USR2 升級Nginx程序
WINCH 優(yōu)雅地關(guān)閉子工作進(jìn)程
為了重新讀取配置文件,HUP信號必須發(fā)送給Nginx主進(jìn)程。
主進(jìn)程首先檢查配置文件語法是否有效,再嘗試應(yīng)用新的配置,這里會重新打開日志文件并建立新的監(jiān)聽套接字,如果這個過程失敗Nginx可以回滾使用就配置文件繼續(xù)工作。
如果成功Nginx會啟動新的工作子進(jìn)程,并發(fā)送消息給舊的工作進(jìn)程(QUIT信號),這樣舊的進(jìn)程停止監(jiān)聽服務(wù)但會繼續(xù)為當(dāng)前正在進(jìn)行的連接服務(wù),
當(dāng)所有的就客戶連接終止后,就工作進(jìn)程被關(guān)閉。
3. Nginx本身并未附帶啟動腳本,本著長久使用的便利性,建議將對Nginx的控制寫入腳本。篇幅有些,下一篇博客文章會專門講解這樣的啟動腳本。
四、基本配置
以下為Nginx典型主配置文檔:/usr/local/nginx/conf/nginx
- #user nobody; //設(shè)置用戶與組
- worker_processes 1; //啟動進(jìn)程數(shù)
- error_log logs/error.log; //錯誤日志
- pid logs/nginx.pid; //Nginx進(jìn)程號
- events {
- worker_connections 1024; //每個進(jìn)程的連接數(shù)
- }
- http {
- include mime.types; //定義文件類型
- default_type application/octet-stream; //默認(rèn)文件類型
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- //以上被注釋的log_format可以定義自己的日志格式
- #access_log logs/access.log main; //使用log_format定義的main日志格式產(chǎn)生access.log日志文件,注這里默認(rèn)是被注釋的
- sendfile on; //是否調(diào)用sendfile()進(jìn)行數(shù)據(jù)的拷貝,sendfile()拷貝數(shù)據(jù)是在內(nèi)核級別完成,所以會比一般的read,write更高效
- #tcp_nopush on; //開啟后服務(wù)器的響應(yīng)頭部信息產(chǎn)生獨(dú)立的數(shù)據(jù)包發(fā)送,即一個響應(yīng)頭信息一個包
- #keepalive_timeout 0; //保持連接的超時(shí)時(shí)間
- keepalive_timeout 65;
- #gzip on; //是否啟用壓縮功能,將頁面壓縮后傳輸更節(jié)省流量
- server { //定義虛擬主機(jī)
- listen 80; //服務(wù)器監(jiān)聽的端口
- server_name www.jacob.com; //訪問域名
- #charset koi8-r; //頁面編碼,如果網(wǎng)頁的編碼與charset不同將被自動轉(zhuǎn)碼(轉(zhuǎn)碼后可能顯示為亂碼)
- #access_log logs/host.access.log main; //設(shè)置服務(wù)器www.jacob.com的訪問日志
- location / { //匹配URL地址
- root html; //網(wǎng)頁根路徑,如果Nginx安裝在/usr/local/nginx目錄下,則網(wǎng)頁在/usr/local/nginx/html目錄下
- index index.html index.htm; //默認(rèn)首頁
- }
- #error_page 404 /404.html; //錯誤頁面
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- # proxy the PHP scripts to Apache listening on 127.0.0.1:80
- #
- #location ~ \.php$ {
- # proxy_pass http://127.0.0.1;
- #} //以上注釋行用來設(shè)置代理,當(dāng)客戶端訪問php頁面時(shí),自動將請求轉(zhuǎn)發(fā)給后端服務(wù)器
- # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
- #
- #location ~ \.php$ {
- # root html;
- # fastcgi_pass 127.0.0.1:9000;
- # fastcgi_index index.php;
- # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
- # include fastcgi_params;
- #} //以上注釋行用來設(shè)置代理,當(dāng)客戶端訪問php頁面時(shí),自動將請求轉(zhuǎn)發(fā)給后端服務(wù)器
- # deny access to .htaccess files, if Apache's document root
- # concurs with nginx's one
- #
- #location ~ /\.ht {
- # deny all; //拒絕所有人訪問.ht頁面
- #}
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server { //定義虛擬主機(jī)(此處設(shè)置與上面一個server虛擬主機(jī)格式一樣
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server { //定義虛擬主機(jī)
- # listen 443;
- # server_name localhost;
- # ssl on; //開啟SSL
- # ssl_certificate cert.pem; //指定證書文件
- # ssl_certificate_key cert.key; //指定私鑰文件
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
五、示例(虛擬主機(jī)、HTTPS)
注意:實(shí)現(xiàn)SSL需要configure時(shí)添加--with-http_ssl_module模塊
1. 修改配置文件部分內(nèi)容如下:
- http {
- include mime.types;
- default_type application/octet-stream;
- sendfile on;
- keepalive_timeout 65;
- gzip on;
- server {
- listen 80;
- server_name www.demo.com;
- charset utf-8;
- access_log logs/demo.access.log;
- location / {
- root demo;
- index index.html index.htm;
- }
- }
- # HTTPS server
- server {
- listen 443;
- server_name www.test.comm;
- ssl on;
- ssl_certificate cert.cer; //證書路徑同nginx.conf在一個目錄
- ssl_certificate_key test.pem; //私鑰路徑同nginx.conf在一個目錄
- ssl_session_timeout 5m;
- ssl_protocols SSLv2 SSLv3 TLSv1;
- ssl_ciphers HIGH:!aNULL:!MD5;
- ssl_prefer_server_ciphers on;
- location / {
- root test;
- index index.html index.htm;
- }
- }
- }
2. 生成證書的方法如下:
[root@centos6 ~] openssl genrsa -out test.pem 2048 //生成私鑰
[root@centos6 ~] openssl req -new -x509 -key test.pem -out cert.cer //根據(jù)私鑰生成根證書
[root@centos6 ~] cp {test.pem,cert,cer} /usr/local/nginx/conf/ //拷貝證書
效果如圖:
提示:關(guān)于密鑰與證書的操作可以參考http://manual.blog.51cto.com/3300438/788691與http://manual.blog.51cto.com/3300438/788681兩篇博文 |
2. 創(chuàng)建測試用網(wǎng)頁
[root@centos6 ~] mkdir /usr/local/nginx{demo,test}
[root@centos6 ~] cat > /usr/local/nginx/demo/index.html <<EOF
> <html>
> <title>Hello The World</title>
> <body>
> This is a demo page!
> </body>
> </html>
> EOF
[root@centos6 ~] cat > /usr/local/nginx/test/index.html <<EOF
> <html>
> <title>Hello The World</title>
> <body>
> This is a demo page!
> </body>
> </html>
> EOF
3. 重啟Nginx
[root@centos6 ~] kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
4. 客戶端訪問(需要DNS解析或hosts文件解析)
效果如圖:
不早了,休息了各位!
本文出自 “丁丁歷險(xiǎn)” 博客,請務(wù)必保留此出處http://manual.blog.51cto.com/3300438/948204