各類(lèi)web服務(wù)器的占有率: http://w3techs.com/technologies/overview/web_server/all


Nginx是一個(gè)高性能的web和反向代理服務(wù)器軟件,與Apache相比,Nginx能夠支持更多的并發(fā)連接(針對靜態(tài)的,小文件服務(wù)器),且占用資源更少,效率很高。
作為負載均衡服務(wù)器,Nginx可以作為http server或DB等服務(wù)器的代理服務(wù)器,類(lèi)似專(zhuān)業(yè)的Haproxy軟件功能(Nginx代理功能相對簡(jiǎn)單,代理功能及效率不如Haproxy)。
郵件代理服務(wù)軟件;
緩存服務(wù)器,相當于專(zhuān)業(yè)的緩存軟件,如squid
Nginx安裝簡(jiǎn)單,配置文件簡(jiǎn)潔、配置靈活(支持perl語(yǔ)法)。
如 網(wǎng)易、新浪、趕集、人人網(wǎng)等都在使用Nginx。
Nginx的http服務(wù)器特性:
處理靜態(tài)文件,索引文件及自動(dòng)索引,打開(kāi)文件描述符緩存;
使用緩存加速反向代理;簡(jiǎn)單負載均衡及容錯;
遠程FastCGI服務(wù)的緩存加速支持;簡(jiǎn)單的負載均衡及容錯;
模塊化的架構。過(guò)濾器包括gzip壓縮、ranges支持、chunked相應、xslt,ssi以及圈像縮放,在ssi過(guò)濾器中,一個(gè)包含多個(gè)ssi的頁(yè)面,如果經(jīng)由FastCGI或反向代理處理,可被并行處理;
支持SSL,TLS SNI;
基于名字和IP的虛擬主機;
Keep-alive和pipelined連接支持;
靈活的配置;
重新加載配置以及在線(xiàn)升級時(shí),不需要中斷正在處理的請求;
自定義訪(fǎng)問(wèn)日至格式,帶緩存的日志寫(xiě)操作以及快速日志輪轉;
3xx-5xx錯誤代碼重定向;
重寫(xiě)(rewrite)模塊;
給予客戶(hù)端IP地址和http基本認證機制的訪(fǎng)問(wèn)控制;
支持PUT、DELETE、MKCOL、COPY、以及MOVE方法;
支持FLV流和MP4流;
速度限制;
來(lái)自同一地址的同時(shí)連接數或請求數限制;
以上信息來(lái)自: http://nginx.org/
Nginx有點(diǎn)總結:
1.高并發(fā):能支持1-2萬(wàn)甚至更多的并發(fā)連接(靜態(tài)小文件環(huán)境下);
2.內存消耗少:在3w并發(fā)連接下,開(kāi)啟的10個(gè)Nginx進(jìn)程消耗不到200M內存;
3.可以做HTTP反向代理:即負載均衡功能,相當于專(zhuān)業(yè)的Haproxy軟件或lvs的功能;
4.內置對RS服務(wù)健康檢查功能:如果Nginx proxy后端某臺web機器宕機,不會(huì )影響前端的訪(fǎng)問(wèn),這個(gè)功能還是比較弱,徐后續改進(jìn);
5.通過(guò)cache插件(cache_purge)可以實(shí)現類(lèi)squid等專(zhuān)業(yè)的緩存軟件實(shí)現的功能;
6.Nginx最重要的優(yōu)點(diǎn)是:支持kqueue(FreeBSD4.1+),epoll(Linux 2.6+)等網(wǎng)絡(luò )IO事件模型。由此來(lái)支持高并發(fā)。
Nginx的應用場(chǎng)合:
使用 Nginx 結合FastCGI運行PHP、JSP、Perl等程序;
使用 Nginx 作為反向代理、負載均衡、規則過(guò)濾;
使用 Nginx 運行靜態(tài) HTML 頁(yè)、小圖片等;
使用 Nginx 加 cache 插件實(shí)現對 web 服務(wù)器緩存功能;
提示:近幾年,網(wǎng)上熱炒 Nginx web server,經(jīng)過(guò)實(shí)際測試,對于提供純靜態(tài)小文件HTML頁(yè)面、圖片等服務(wù),Apache確實(shí)也比Nginx遜色點(diǎn)。但在結合PHP引擎提供php服務(wù)及其它更多方面,并不比Nginx差,而apahce的穩定性相對要好一些。
Nginx和其他web服務(wù)器的對比
主流web服務(wù)產(chǎn)品對比說(shuō)明:
1)Apache
2.2版非常穩定強大,據官方說(shuō)明,其2.4版本性能超強;
Prefork模式取消了進(jìn)程創(chuàng )建開(kāi)銷(xiāo),性能很高;
處理動(dòng)態(tài)業(yè)務(wù)數據時(shí),因關(guān)聯(lián)到后端的引擎和數據庫,瓶頸不在于A(yíng)pache本身(PHP及數據的并發(fā)遠遠小于A(yíng)pache的并發(fā),所以處理動(dòng)態(tài)數據時(shí)Apache不比Nginx差,相反Apache的穩定性更好一些);
高并發(fā)時(shí)消耗系統資源相對多一些;
2)Nginx
基于異步IO模型,性能強,能夠支持上萬(wàn)并發(fā);
對小文件支持很好,性能很高(限靜態(tài)小文件);
代碼優(yōu)美,擴展庫必須編譯進(jìn)主程序;
消耗系統資源比較低;
3)lighttpd
基于異步IO模型,性能和Nginx相近;
擴展庫是so模式,比Nginx更靈活;
全球使用率比以前低,安全性沒(méi)有上面兩個(gè)好;
通過(guò)插件(mod_secdownload)可實(shí)現文件URL地址加密;

總結:當處理動(dòng)態(tài)數據時(shí),三者的差距不大,從測試結果來(lái)看,Apache更有優(yōu)勢點(diǎn),這是因為處理動(dòng)態(tài)數據的能力取決于 php 和后端數據庫的提供服務(wù)能力,也就是說(shuō)瓶頸不在于web服務(wù)器上。一般php支持的并發(fā)參考值300--1000,java引擎并發(fā)300--1000.
Nginx使用最新的epoll(Linux2.6內核)和kqueue(freebsd)網(wǎng)絡(luò )IO模型,而Apache則使用的是傳統的select模型。目前Linux能夠承受高并發(fā)訪(fǎng)問(wèn)的Squid、Memcached都采用的是epoll網(wǎng)絡(luò )的I/O模型。
Nginx安裝配置
1)主要配置文件介紹
配置文件:../nginx/nginx.conf
主目錄:../nginx/html
2)主要命令:
查看版本信息:/usr/sbin/nginx -v
配置文件語(yǔ)法檢查:/usr/sbin/nginx -t
啟動(dòng):/etc/init.d/nginx start
停止: /etc/init.d/nginx stop
修改配置,平滑重啟: /etc/init.d/nginx reload
注意:重啟也可以使用發(fā)信號的方式:
kill -HUP `cat /var/run/nginx.pid` #向master進(jìn)程的PID發(fā)送信號。
Nginx支持的信號:
TEM,INT:快速關(guān)閉
QUIT:正常關(guān)閉
HUP:平滑重啟reload,重新加載配置文件
USR1:重新打開(kāi)日志文件,在切割日志時(shí)有用
USR2:平滑升級
WINCH:從容關(guān)閉工作worker進(jìn)程(只剩主進(jìn)程master)
Nginx 的工作模式:1個(gè) master 進(jìn)程 +N 個(gè) worker 進(jìn)程
編譯安裝 Nginx
1)安裝 Nginx 所需的 pcre 庫
pcre 全稱(chēng)(Perl Compatible Regular Expressions),中文 perl 兼容正則表達式,官方網(wǎng)址:http://www.pcre.org/,安裝pcre 庫是為了使Nginx支持 HTTP Rewrite 模塊。安裝過(guò)程如下:
tar xzf pcre-8.36.tar.gz
cd pcre-8.36
./configure
make; make install
cd ..
附:pcre介紹
2)安裝Nginx
軟件下載:wget
useradd nginx -s /sbin/nologin -M
tar xzf nginx-1.4.7.tar.gz
cd nginx-1.4.7
./configure --user=nginx --group=nginx --prefix=/usr/local/ nginx-1.4.7 --with-http_ssl_module --with-http_stub_status_module
編譯安裝的一些參數的獲?。?/span>./configure --help
make && make install 或者 make; make install
ln -s /usr/local/nginx-1.4.7 /usr/local/nginx
做軟連接的目的是隱藏自己軟件的版本號,一方面是安全考慮,另一方面是使用的時(shí)候方便,而且升級時(shí),在升級后,把軟連接刪除,重新做個(gè)軟連接即可,其他地方不需要動(dòng)就能使用新版本的軟件。
3)啟動(dòng)并檢查安裝結果
/usr/local/nginx/sbin/nginx -t
如果遇到錯誤,解決方法見(jiàn)下面的描述
啟動(dòng)服務(wù):
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
編譯安裝的Nginx啟動(dòng),不用加start
如果是rpm包安裝的Nginx,需要按如下方式啟動(dòng):
/usr/local/nginx/sbin/nginx start
查看端口:
lsof -i :80
ss -lnutp |grep 80
ps -ef |grep nginx
錯誤解決:

然后再執行命令:ldconfig
不要還怕錯誤,要想辦解決錯誤,錯誤才能使你進(jìn)步。
Welcome to Nginx !
如果不能出現 welcome to Nginx,請按如下步驟檢查:
防火墻是否關(guān)閉;
/etc/init.d/iptables stop
iptables -I INPUT -p tcp --dport 80 -j ACCEPT ---增加防火墻策略,允許80端口的訪(fǎng)問(wèn)
chkconfig iptables off
/etc/init.d/iptables status
selinux是否關(guān)閉
setenfore 0
vi /etc/selinux/config |grep SELINUX=distabled
查看80端口是否被占用了
netstat -lnt |grep 80
查看是否已經(jīng)有Nginx進(jìn)程存在了
ps -ef |grep nginx
本地進(jìn)行測試
wget http://本機IP地址,看下本機是否可以訪(fǎng)問(wèn),如果本地可以訪(fǎng)問(wèn),而瀏覽器訪(fǎng)問(wèn)不了,說(shuō)明被防火墻阻止了。
查看Nginx日志,看下是否有特殊異?;驁箦e
要學(xué)會(huì )看服務(wù)的日志,對于解決問(wèn)題,非常有幫助。
less /usr/local/nginx/logs/error.log
tips:遇到問(wèn)題的時(shí)候,我們要第一時(shí)間看屏幕返回的提示和Nginx服務(wù)的錯誤日志,獲取有效的信息和解決問(wèn)題。這是運維工程師的基本技能!養成看屏幕輸出和看錯誤日志的習慣,這才是正確的發(fā)展路線(xiàn)。
部署一個(gè)簡(jiǎn)單的Nginx web站點(diǎn)
Nginx默認站點(diǎn)目錄:Nginx安裝目錄下的html目錄(即:/usr/local/nginx/html),這個(gè)信息可以在Nginx的配置文件中找到。

F:\老男孩初級到高級最完整版本\老男孩運維初級\09-LAMP-APACHE-NGINX/L09-10老男孩Linux運維實(shí)戰培訓-Nginx服務(wù)生產(chǎn)實(shí)戰應用指南03..avi
Nginx的目錄結構說(shuō)明:
[root@test nginx]# tree /usr/local/nginx
/usr/local/nginx
├── client_body_temp
├── conf #這是 Nginx 的所有配置文件的目錄,及其重要。
│ ├── fastcgi.conf #fastcgi的配置文件
│ ├── fastcgi.conf.default
│ ├── fastcgi_params #fastcgi的參數文件
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf #Nginx默認的主配置文件
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── fastcgi_temp #臨時(shí)目錄
├── html #編譯安裝時(shí),Nginx的默認站點(diǎn)目錄,類(lèi)似Apache的htdocs目錄
│ ├── 50x.html #錯誤頁(yè)面優(yōu)雅替代顯示文件,例如:出現502錯誤會(huì )調用此頁(yè)面
│ └── index.html #默認的首頁(yè)文件,在實(shí)際環(huán)境中,大家習慣用(注意:不是必須)index.html、index.jsp來(lái)作為網(wǎng)站的首頁(yè)文件。首頁(yè)文件名字是在nginx.conf中事先定義好的。具體參數為:index index.html index.htm;注意:與Apache參數的不同DirectoryIndex index.html
├── logs #日志目錄,包括訪(fǎng)問(wèn)日志和錯誤日志
│ ├── access.log #這是Nginx的默認訪(fǎng)問(wèn)日志文件,使用tail -f access.log來(lái)實(shí)時(shí)觀(guān)看網(wǎng)站的用戶(hù)訪(fǎng)問(wèn)情況信息。
│ ├── error.log #這是Nginx的錯誤日志文件,如果Nginx出現啟動(dòng)故障等問(wèn)題,一定要查看這個(gè)錯誤日志。
│ └── nginx.pid #Nginx的pid文件,Nginx進(jìn)程啟動(dòng)后,會(huì )把所有進(jìn)程的ID號寫(xiě)到此文件
├── proxy_temp
├── sbin #這是Nginx命令的目錄,如Nginx的啟動(dòng)命令nginx
│ └── nginx #
├── scgi_temp #臨時(shí)目錄
└── uwsgi_temp #臨時(shí)目錄
9 directories, 21 files
Nginx主配置文件nginx.conf
nginx.conf是Nginx的最重要的配置文件之一,必須搞定它,下面針對默認主配置文件nginx.conf的參數做詳細的中文解釋?zhuān)?/p>
[ root@test ~]# cat -n /usr/local/nginx/conf/nginx.conf
1
2 #user nobody;
3 worker_processes 1;
4
5 #error_log logs/error.log;
6 #error_log logs/error.log notice;
7 #error_log logs/error.log info;
8
9 #pid logs/nginx.pid;
10
11
12 events {
13 worker_connections 1024;
14 }
15
16
17 http {
18 include mime.types;
19 default_type application/octet-stream;
20
21 #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
22 # '$status $body_bytes_sent "$http_referer" '
23 # '"$http_user_agent" "$http_x_forwarded_for"';
24
25 #access_log logs/access.log main;
26
27 sendfile on;
28 #tcp_nopush on;
29
30 #keepalive_timeout 0;
31 keepalive_timeout 65;
32
33 #gzip on;
34
35 server {
36 listen 80;
37 server_name localhost;
38
39 #charset koi8-r;
40
41 #access_log logs/host.access.log main;
42
43 location / {
44 root html;
45 index index.html index.htm;
46 }
47
48 #error_page 404 /404.html;
49
50 # redirect server error pages to the static page /50x.html
51 #
52 error_page 500 502 503 504 /50x.html;
53 location = /50x.html {
54 root html;
55 }
56
57 # proxy the PHP scripts to Apache listening on 127.0.0.1:80
58 #
59 #location ~ \.php$ {
60 # proxy_pass http://127.0.0.1;
61 #}
62
63 # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
64 #
65 #location ~ \.php$ {
66 # root html;
67 # fastcgi_pass 127.0.0.1:9000;
68 # fastcgi_index index.php;
69 # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
70 # include fastcgi_params;
71 #}
72
73 # deny access to .htaccess files, if Apache's document root
74 # concurs with nginx's one
75 #
76 #location ~ /\.ht {
77 # deny all;
78 #}
79 }
80
81
82 # another virtual host using mix of IP-, name-, and port-based configuration
83 #
84 #server {
85 # listen 8000;
86 # listen somename:8080;
87 # server_name somename alias another.alias;
88
89 # location / {
90 # root html;
91 # index index.html index.htm;
92 # }
93 #}
94
95
96 # HTTPS server
97 #
98 #server {
99 # listen 443 ssl;
100 # server_name localhost;
101
102 # ssl_certificate cert.pem;
103 # ssl_certificate_key cert.key;
104
105 # ssl_session_cache shared:SSL:1m;
106 # ssl_session_timeout 5m;
107
108 # ssl_ciphers HIGH:!aNULL:!MD5;
109 # ssl_prefer_server_ciphers on;
110
111 # location / {
112 # root html;
113 # index index.html index.htm;
114 # }
115 #}
116
117 }
去掉注釋和空格后:
[ root@test conf]# egrep -v "#|^$" nginx.conf
worker_processes 1; #工作進(jìn)程
events {
worker_connections 1024; #事件
}
http { #http的標簽
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { #虛擬主機的配置
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
server標簽相當于A(yíng)pache中的一個(gè)虛擬主機vhost;
所有的server標簽都要放在http的標簽的里面;
Nginx虛擬主機配置實(shí)戰:
1)基于域名的虛擬主機
生產(chǎn)環(huán)境中最常用到的。必須熟練掌握
在主配置文件的第一行增加Nginx運行用戶(hù):user nginx nginx
調整啟動(dòng)進(jìn)程,根據cpu的數量調節(進(jìn)程數可以是CPU的數量乘2):worker_processes 8 ;
在events標簽里面第一行增加:use epoll
修改server標簽
修改server_name 為:www.etiantian.org etiantian.org
修改配置文件過(guò)程中,要注意大括號的對應關(guān)系,一定要成對出現。低級錯誤。
nginx.conf中的配置,都要以分號結束。
user nginx nginx;
worker_processes 2;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.etiantian.org etiantian.org;
location / {
root html;
index index.html index.htm;
}
}
}
創(chuàng )建站點(diǎn)目錄:
mkdir /data/www/{www,bbs,blog} -p
授權:chown -R nginx.nginx /data/www
創(chuàng )建日志文件目錄:
mkdir /app/log -p
授權:chown -R nginx.nginx /app/log
修改nginx.conf中的默認站點(diǎn)目錄:
root /data/www/www;
echo www > /data/www/www/index.html
檢查語(yǔ)法
/usr/local/nginx/sbin/nginx -t
平滑重啟Nginx
/usr/local/nginx/sbin/nginx -s reload
Windows本機解析配置:
開(kāi)始 -- 運行-- %systemroot%\system32\drivers\etc\hosts
完整的虛擬主機配置如下:
[ root@test conf]# cat -n nginx.conf
1 user nginx nginx;
2 worker_processes 4;
3 error_log logs/error.log crit;
4 pid logs/nginx.pid;
5
6 events {
7 use epoll;
8 worker_connections 1024;
9 }
10 http {
11 include mime.types;
12 default_type application/octet-stream;
13 log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" '
14 '$status $body_bytes_sent "$http_referer" '
15 '"$http_user_agent" "$http_x_forwarded_for"';
16 sendfile on;
17 keepalive_timeout 65;
18 server {
19 listen 80;
20 server_name www.etiantian.org etiantian.org;
21 location / {
22 root /data/www/www;
23 index index.html index.htm;
24 access_log /app/log/www_access.log commonlog;
25 }
26 }
27 ############
28 server {
29 listen 80;
30 server_name bbs.etiantian.org;
31 location / {
32 root /data/www/bbs;
33 index index.html index.htm;
34 access_log /app/log/bbs_access.log commonlog;
35 }
36 }
37 ############
38 server {
39 listen 80;
40 server_name blog.etiantian.org;
41 location / {
42 root /data/www/blog;
43 index index.html index.htm;
44 access_log /app/log/blog_access.log commonlog;
45 }
46 }
47 }
Nginx配置文件的目錄優(yōu)化
如果虛擬主機比較多的時(shí)候,把所有虛擬主機都放一個(gè)配置文件里面,就不太好查看和管理了??梢阅MApache的虛擬目錄的方式,包含虛擬目錄的配置文件。
首先創(chuàng )建虛擬目錄配置文件存放目錄:
mkdir /usr/local/nginx/extra
修改后的配置文件包括兩個(gè)文件,分別如下:
主配置文件:
[ root@test ~]# cat -n /usr/local/nginx/conf/nginx.conf
1 user nginx nginx;
2 worker_processes 4;
3 error_log logs/error.log crit;
4 pid logs/nginx.pid;
5
6 events {
7 use epoll;
8 worker_connections 1024;
9 }
10 http {
11 include mime.types;
12 default_type application/octet-stream;
13 log_format commonlog '$remote_addr - $remote_user [$time_local] "$request" '
14 '$status $body_bytes_sent "$http_referer" '
15 '"$http_user_agent" "$http_x_forwarded_for"';
16 sendfile on;
17 keepalive_timeout 65;
18 include ../extra/nginx_vhosts.conf;
19 }
虛擬主機配置文件:
[ root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf
1 server {
2 listen 80;
3 server_name www.etiantian.org etiantian.org;
4 location / {
5 root /data/www/www;
6 index index.html index.htm;
7 access_log /app/log/www_access.log commonlog;
8 }
9 }
10 ############
11 server {
12 listen 80;
13 server_name bbs.etiantian.org;
14 location / {
15 root /data/www/bbs;
16 index index.html index.htm;
17 access_log /app/log/bbs_access.log commonlog;
18 }
19 }
20 ############
21 server {
22 listen 80;
23 server_name blog.etiantian.org;
24 location / {
25 root /data/www/blog;
26 index index.html index.htm;
27 access_log /app/log/blog_access.log commonlog;
28 }
29 }
甚至我們還可以將每個(gè)虛擬主機定義一個(gè)vhost配置文件,這樣簡(jiǎn)單清晰,可以分別定義,方便維護和管理;
包含的時(shí)候,只需要修改主配置文件中的: include ../extra/nginx_vhosts.conf; 為:include ../extra/*.conf;
也可以分別寫(xiě)一行;
配置文件優(yōu)化:
對nginx_vhosts.conf配置文件拆分:
sed -n '1,9p' nginx_vhosts.conf > www.etiantian.org.conf
sed -n '10,19p' nginx_vhosts.conf > bbs.etiantian.org.conf
sed -n '20,29p' nginx_vhosts.conf > blog.etiantian.org.conf
編輯主配置文件,將include ../extra/nginx_vhosts.conf;替換為:
include ../extra/www.etiantian.org.conf;
include ../extra/bbs.etiantian.org.conf;
include ../extra/blog.etiantian.org.conf;
配置Nginx狀態(tài)信息虛擬主機:
cat >>/usr/local/nginx/conf/extra/nginx_vhosts.conf <<EOF #將下面這個(gè)server追加到Nginx的虛擬主機配置文件中。
######
server
{
listen 80;
server_name status.etiantian.org;
location / {
stub_status on;
access_log off;
}
}
EOF
2)基于端口的虛擬主機配置
只需修改配置文件: nginx_vhosts.conf 中的每個(gè)虛擬主機中的端口即可,即,將默認的80端口改成別的端口,如8060、8070、8080等;
然后重新加載配置文件。
3)基于IP地址的虛擬主機配置
此種比較少用,了解下即可
如果要測試此類(lèi)虛擬主機,需要在測試機上增加幾個(gè)IP地址(在負載均衡配置里,此IP也被稱(chēng)為VIP):
ifconfig eth0:192.168.64.101 netmask 255.255.255.0 up
ifconfig eth0:192.168.64.102 netmask 255.255.255.0 up
ifconfig eth0:192.168.64.103 netmask 255.255.255.0 up
修改配置文件: nginx_vhosts.conf
把server_name 改成這幾個(gè)IP地址,將每個(gè)端口前面加上對應的IP地址;
實(shí)例:
[root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf
1 server {
2 listen 192.168.64.101:8060;
3 server_name 192.168.64.101;
4 location / {
5 root /data/www/www;
6 index index.html index.htm;
7 access_log /app/log/www_access.log commonlog;
8 }
9 }
批量替換:將80替換為:IP加端口
:g/80/s//192.168.64.101:80/g
后面兩個(gè)與之類(lèi)似
修改完成后,檢查配置文件語(yǔ)法,重新加載配置文件,進(jìn)行測試;
檢查語(yǔ)法 : /usr/local/nginx/sbin/nginx -t
平滑重啟Nginx : /usr/local/nginx/sbin/nginx -s reload
Nginx虛擬主機配置別名:
[root@test ~]# cat -n /usr/local/nginx/extra/nginx_vhosts.conf
1 server {
2 listen 80;
3 server_name www.etiantian.org etiantian.org;
4 location / {
5 root /data/www/www;
6 index index.html index.htm;
7 access_log /app/log/www_access.log commonlog;
8 }
9 }
說(shuō)明:別名,就寫(xiě)在域名后面就行了,以空格分開(kāi)即可;
應用場(chǎng)景:
1)如輸入baidu.com會(huì )自動(dòng)跳轉到www.baidu.com,輸入taobao.com會(huì )自動(dòng)跳轉到www.taobao.com,這里我們就是要實(shí)現輸入etiantian.org和訪(fǎng)問(wèn)www.etiantian.org一樣顯示我們的網(wǎng)站。
2)在生產(chǎn)環(huán)境中,利用別名來(lái)監控集群環(huán)境下面的RS的URL是否正常。
如:ServerAlias etiantian.org 21www.etiantian.org 22www.etiantian.org
可以在監控服務(wù)里面配置hosts來(lái)監控RS 21www.etiantian.org 22www.etiantian.org等地址是否正常,進(jìn)而判斷每臺機器的www.etiantian.org是否正常。
如不使用別名,則很難通過(guò)URL方式檢測判斷下面的機器是否正常(因為域名是同一個(gè))。
當然,Keepalived和Haproxy等軟件也有對于RS使用URL的健康檢查措施,這方面的案例見(jiàn)相關(guān)文檔。
配置多個(gè)Nginx實(shí)例
Nginx安裝后的啟動(dòng)命令在“/usr/local/nginx/sbin/nginx”,我們可以通過(guò) -h 參數查看相關(guān)參數的用法:
[ root@test ~]# /usr/local/nginx/sbin/nginx -h
nginx version: nginx/1.8.0
Usage: nginx [-?hvVtq] [-s signal] [-c filename] [-p prefix] [-g directives]
Options:
-?,-h : this help
-v : show version and exit #顯示Nginx版本號后退出
-V : show version and configure options then exit #顯示Nginx版本號和配置選項(配置選項即:編譯安裝Nginx的時(shí)候所用的參數)后退出
-t : test configuration and exit #測試配置文件是否正確,在運行時(shí)需要重新加載配置的時(shí)候,此命令非常重要,用來(lái)檢測所修改的配置文件是否存在語(yǔ)法錯誤。
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload #發(fā)送信號給一個(gè)master進(jìn)程,這里的reload參數很重要,是優(yōu)雅重啟Nginx的參數,類(lèi)似Apache的graceful參數。
-p prefix : set prefix path (default: /usr/local/nginx-1.8.0/)
-c filename : set configuration file (default: conf/nginx.conf) #使用制定的Nginx配置文件,而不是conf目錄下的nginx.conf。借此可以啟動(dòng)多個(gè)Nginx實(shí)例。
-g directives : set global directives out of configuration file
[ root@test ~]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.8.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx-1.8.0 --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
/usr/local/nginx/sbin/nginx
/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf #此處的nginx.conf和之前默認的nginx.conf不能沖突,否則起不來(lái)的,這樣可以單獨開(kāi)啟nginx實(shí)例。
cp -af:
殺掉nginx:pkill nginx
啟動(dòng)多實(shí)例:
/usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
應用場(chǎng)景:
服務(wù)器資源有限,且單個(gè)服務(wù)器的性能又很空余,又希望各http相對獨立,此時(shí)就可以采用nginx多實(shí)例的方式;
本文出自 “冷水泡茶” 博客,請務(wù)必保留此出處http://miaocbin.blog.51cto.com/689091/1692772
聯(lián)系客服