日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Nginx技術(shù)手冊

 老鶴閑聊 2016-01-16

各類web服務(wù)器的占有率: http:///technologies/overview/web_server/all

spacer.gifwkiom1xuid6glcbsaafkloaq8ye362.jpg

Nginx是一個高性能的web和反向代理服務(wù)器軟件,與Apache相比,Nginx能夠支持更多的并發(fā)連接(針對靜態(tài)的,小文件服務(wù)器),且占用資源更少,效率很高。

作為負載均衡服務(wù)器,Nginx可以作為http server或DB等服務(wù)器的代理服務(wù)器,類似專業(yè)的Haproxy軟件功能(Nginx代理功能相對簡單,代理功能及效率不如Haproxy)。

郵件代理服務(wù)軟件;

緩存服務(wù)器,相當(dāng)于專業(yè)的緩存軟件,如squid

Nginx安裝簡單,配置文件簡潔、配置靈活(支持perl語法)。

如 網(wǎng)易、新浪、趕集、人人網(wǎng)等都在使用Nginx。

Nginx的http服務(wù)器特性:

  • 處理靜態(tài)文件,索引文件及自動索引,打開文件描述符緩存;

  • 使用緩存加速反向代理;簡單負載均衡及容錯;

  • 遠程FastCGI服務(wù)的緩存加速支持;簡單的負載均衡及容錯;

  • 模塊化的架構(gòu)。過濾器包括gzip壓縮、ranges支持、chunked相應(yīng)、xslt,ssi以及圈像縮放,在ssi過濾器中,一個包含多個ssi的頁面,如果經(jīng)由FastCGI或反向代理處理,可被并行處理;

  • 支持SSL,TLS SNI;

  • 基于名字和IP的虛擬主機;

  • Keep-alive和pipelined連接支持;

  • 靈活的配置;

  • 重新加載配置以及在線升級時,不需要中斷正在處理的請求;

  • 自定義訪問日至格式,帶緩存的日志寫操作以及快速日志輪轉(zhuǎn);

  • 3xx-5xx錯誤代碼重定向;

  • 重寫(rewrite)模塊;

  • 給予客戶端IP地址和http基本認證機制的訪問控制;

  • 支持PUT、DELETE、MKCOL、COPY、以及MOVE方法;

  • 支持FLV流和MP4流;

  • 速度限制;

  • 來自同一地址的同時連接數(shù)或請求數(shù)限制;

以上信息來自: http:///

Nginx有點總結(jié):

  • 1.高并發(fā):能支持1-2萬甚至更多的并發(fā)連接(靜態(tài)小文件環(huán)境下);

  • 2.內(nèi)存消耗少:在3w并發(fā)連接下,開啟的10個Nginx進程消耗不到200M內(nèi)存;

  • 3.可以做HTTP反向代理:即負載均衡功能,相當(dāng)于專業(yè)的Haproxy軟件或lvs的功能;

  • 4.內(nèi)置對RS服務(wù)健康檢查功能:如果Nginx proxy后端某臺web機器宕機,不會影響前端的訪問,這個功能還是比較弱,徐后續(xù)改進;

  • 5.通過cache插件(cache_purge)可以實現(xiàn)類squid等專業(yè)的緩存軟件實現(xiàn)的功能;

  • 6.Nginx最重要的優(yōu)點是:支持kqueue(FreeBSD4.1+),epoll(Linux 2.6+)等網(wǎng)絡(luò)IO事件模型。由此來支持高并發(fā)。

Nginx的應(yīng)用場合:

  • 使用 Nginx 結(jié)合FastCGI運行PHP、JSP、Perl等程序;

  • 使用 Nginx 作為反向代理、負載均衡、規(guī)則過濾;

  • 使用 Nginx 運行靜態(tài) HTML 頁、小圖片等;

  • 使用 Nginx 加 cache 插件實現(xiàn)對 web 服務(wù)器緩存功能;

提示:近幾年,網(wǎng)上熱炒 Nginx web server,經(jīng)過實際測試,對于提供純靜態(tài)小文件HTML頁面、圖片等服務(wù),Apache確實也比Nginx遜色點。但在結(jié)合PHP引擎提供php服務(wù)及其它更多方面,并不比Nginx差,而apahce的穩(wěn)定性相對要好一些。

Nginx和其他web服務(wù)器的對比

主流web服務(wù)產(chǎn)品對比說明:

1)Apache

  • 2.2版非常穩(wěn)定強大,據(jù)官方說明,其2.4版本性能超強;

  • Prefork模式取消了進程創(chuàng)建開銷,性能很高;

  • 處理動態(tài)業(yè)務(wù)數(shù)據(jù)時,因關(guān)聯(lián)到后端的引擎和數(shù)據(jù)庫,瓶頸不在于Apache本身(PHP及數(shù)據(jù)的并發(fā)遠遠小于Apache的并發(fā),所以處理動態(tài)數(shù)據(jù)時Apache不比Nginx差,相反Apache的穩(wěn)定性更好一些);

  • 高并發(fā)時消耗系統(tǒng)資源相對多一些;

2)Nginx

  • 基于異步IO模型,性能強,能夠支持上萬并發(fā);

  • 對小文件支持很好,性能很高(限靜態(tài)小文件);

  • 代碼優(yōu)美,擴展庫必須編譯進主程序;

  • 消耗系統(tǒng)資源比較低;

3)lighttpd

  • 基于異步IO模型,性能和Nginx相近;

  • 擴展庫是so模式,比Nginx更靈活;

  • 全球使用率比以前低,安全性沒有上面兩個好;

  • 通過插件(mod_secdownload)可實現(xiàn)文件URL地址加密;

spacer.gif

總結(jié):當(dāng)處理動態(tài)數(shù)據(jù)時,三者的差距不大,從測試結(jié)果來看,Apache更有優(yōu)勢點,這是因為處理動態(tài)數(shù)據(jù)的能力取決于 php 和后端數(shù)據(jù)庫的提供服務(wù)能力,也就是說瓶頸不在于web服務(wù)器上。一般php支持的并發(fā)參考值300--1000,java引擎并發(fā)300--1000.

Nginx使用最新的epoll(Linux2.6內(nèi)核)和kqueue(freebsd)網(wǎng)絡(luò)IO模型,而Apache則使用的是傳統(tǒng)的select模型。目前Linux能夠承受高并發(fā)訪問的Squid、Memcached都采用的是epoll網(wǎng)絡(luò)的I/O模型。

Nginx安裝配置

1)主要配置文件介紹

配置文件:../nginx/nginx.conf

主目錄:../nginx/html

2)主要命令:

查看版本信息:/usr/sbin/nginx -v

配置文件語法檢查:/usr/sbin/nginx -t

啟動:/etc/init.d/nginx start

停止: /etc/init.d/nginx stop

修改配置,平滑重啟: /etc/init.d/nginx reload

注意:重啟也可以使用發(fā)信號的方式:

kill  -HUP  `cat  /var/run/nginx.pid`  #向master進程的PID發(fā)送信號。

Nginx支持的信號:

TEM,INT:快速關(guān)閉

QUIT:正常關(guān)閉

HUP:平滑重啟reload,重新加載配置文件

USR1:重新打開日志文件,在切割日志時有用

USR2:平滑升級

WINCH:從容關(guān)閉工作worker進程(只剩主進程master)

Nginx 的工作模式:1個 master 進程 +N 個 worker 進程

編譯安裝 Nginx 

1)安裝 Nginx 所需的 pcre 庫

pcre 全稱(Perl Compatible Regular Expressions),中文 perl 兼容正則表達式,官方網(wǎng)址:http://www./,安裝pcre 庫是為了使Nginx支持 HTTP Rewrite 模塊。安裝過程如下:

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 

編譯安裝的一些參數(shù)的獲?。?/span>./configure --help


make && make install  或者  make; make install

ln -s /usr/local/nginx-1.4.7 /usr/local/nginx

做軟連接的目的是隱藏自己軟件的版本號,一方面是安全考慮,另一方面是使用的時候方便,而且升級時,在升級后,把軟連接刪除,重新做個軟連接即可,其他地方不需要動就能使用新版本的軟件。



3)啟動并檢查安裝結(jié)果

/usr/local/nginx/sbin/nginx -t

如果遇到錯誤,解決方法見下面的描述

啟動服務(wù):

/usr/local/nginx/sbin/nginx

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

編譯安裝的Nginx啟動,不用加start

如果是rpm包安裝的Nginx,需要按如下方式啟動:

/usr/local/nginx/sbin/nginx start

查看端口:

lsof -i :80

ss -lnutp |grep 80

ps -ef |grep nginx

錯誤解決:

spacer.gif

然后再執(zhí)行命令:ldconfig

不要還怕錯誤,要想辦解決錯誤,錯誤才能使你進步。

Welcome to Nginx !

如果不能出現(xiàn) welcome to Nginx,請按如下步驟檢查:

  • 防火墻是否關(guān)閉;

    • /etc/init.d/iptables stop

    • iptables -I INPUT -p tcp --dport 80 -j ACCEPT  ---增加防火墻策略,允許80端口的訪問

    • 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進程存在了

    • ps -ef |grep nginx

  • 本地進行測試

    • wget http://本機IP地址,看下本機是否可以訪問,如果本地可以訪問,而瀏覽器訪問不了,說明被防火墻阻止了。

  • 查看Nginx日志,看下是否有特殊異?;驁箦e

    • 要學(xué)會看服務(wù)的日志,對于解決問題,非常有幫助。

    • less /usr/local/nginx/logs/error.log

  • tips:遇到問題的時候,我們要第一時間看屏幕返回的提示和Nginx服務(wù)的錯誤日志,獲取有效的信息和解決問題。這是運維工程師的基本技能!養(yǎng)成看屏幕輸出和看錯誤日志的習(xí)慣,這才是正確的發(fā)展路線。

部署一個簡單的Nginx web站點

Nginx默認站點目錄:Nginx安裝目錄下的html目錄(即:/usr/local/nginx/html),這個信息可以在Nginx的配置文件中找到。

spacer.gif

F:\老男孩初級到高級最完整版本\老男孩運維初級\09-LAMP-APACHE-NGINX/L09-10老男孩Linux運維實戰(zhàn)培訓(xùn)-Nginx服務(wù)生產(chǎn)實戰(zhàn)應(yīng)用指南03..avi

Nginx的目錄結(jié)構(gòu)說明:

[root@test nginx]# tree /usr/local/nginx

/usr/local/nginx 
├── client_body_temp 
├── conf  #這是 Nginx 的所有配置文件的目錄,及其重要。
│   ├── fastcgi.conf  #fastcgi的配置文件
│   ├── fastcgi.conf.default 
│   ├── fastcgi_params  #fastcgi的參數(shù)文件
│   ├── 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  #臨時目錄
├── html  #編譯安裝時,Nginx的默認站點目錄,類似Apache的htdocs目錄
│   ├── 50x.html  #錯誤頁面優(yōu)雅替代顯示文件,例如:出現(xiàn)502錯誤會調(diào)用此頁面
│   └── index.html  #默認的首頁文件,在實際環(huán)境中,大家習(xí)慣用(注意:不是必須)index.html、index.jsp來作為網(wǎng)站的首頁文件。首頁文件名字是在nginx.conf中事先定義好的。具體參數(shù)為:index index.html index.htm;注意:與Apache參數(shù)的不同DirectoryIndex index.html
├── logs  #日志目錄,包括訪問日志和錯誤日志
│   ├── access.log  #這是Nginx的默認訪問日志文件,使用tail -f access.log來實時觀看網(wǎng)站的用戶訪問情況信息。
│   ├── error.log  #這是Nginx的錯誤日志文件,如果Nginx出現(xiàn)啟動故障等問題,一定要查看這個錯誤日志。
│   └── nginx.pid  #Nginx的pid文件,Nginx進程啟動后,會把所有進程的ID號寫到此文件
├── proxy_temp 
├── sbin  #這是Nginx命令的目錄,如Nginx的啟動命令nginx
│   └── nginx  #
├── scgi_temp  #臨時目錄
└── uwsgi_temp  #臨時目錄

9 directories, 21 files 

Nginx主配置文件nginx.conf

nginx.conf是Nginx的最重要的配置文件之一,必須搞定它,下面針對默認主配置文件nginx.conf的參數(shù)做詳細的中文解釋:

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; #工作進程
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標簽相當(dāng)于Apache中的一個虛擬主機vhost;

所有的server標簽都要放在http的標簽的里面;

Nginx虛擬主機配置實戰(zhàn):

1)基于域名的虛擬主機

生產(chǎn)環(huán)境中最常用到的。必須熟練掌握

  • 在主配置文件的第一行增加Nginx運行用戶:user nginx nginx

  • 調(diào)整啟動進程,根據(jù)cpu的數(shù)量調(diào)節(jié)(進程數(shù)可以是CPU的數(shù)量乘2):worker_processes 8 ;

  • 在events標簽里面第一行增加:use epoll

  • 修改server標簽

    • 修改server_name 為:www. 

  • 修改配置文件過程中,要注意大括號的對應(yīng)關(guān)系,一定要成對出現(xiàn)。低級錯誤。

  • nginx.conf中的配置,都要以分號結(jié)束。

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. ; 
        location / { 
            root   html; 
            index  index.html index.htm; 
        } 
    } 
}

創(chuàng)建站點目錄:

mkdir /data/www/{www,bbs,blog} -p

授權(quán):chown -R nginx.nginx  /data/www

創(chuàng)建日志文件目錄:

mkdir /app/log -p

授權(quán):chown -R nginx.nginx /app/log

修改nginx.conf中的默認站點目錄:

root   /data/www/www;

echo www > /data/www/www/index.html

檢查語法

/usr/local/nginx/sbin/nginx -t

平滑重啟Nginx

/usr/local/nginx/sbin/nginx -s reload

Windows本機解析配置:

開始 -- 運行-- %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. ; 
    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.; 
    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.; 
    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)化

如果虛擬主機比較多的時候,把所有虛擬主機都放一個配置文件里面,就不太好查看和管理了。可以模擬Apache的虛擬目錄的方式,包含虛擬目錄的配置文件。

首先創(chuàng)建虛擬目錄配置文件存放目錄:

mkdir /usr/local/nginx/extra

修改后的配置文件包括兩個文件,分別如下:

主配置文件:

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. ; 
     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.; 
    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.; 
    24          location / { 
    25              root   /data/www/blog; 
    26              index  index.html index.htm; 
    27              access_log /app/log/blog_access.log commonlog; 
    28          } 
    29      }

甚至我們還可以將每個虛擬主機定義一個vhost配置文件,這樣簡單清晰,可以分別定義,方便維護和管理;

包含的時候,只需要修改主配置文件中的: include ../extra/nginx_vhosts.conf; 為:include ../extra/*.conf; 

也可以分別寫一行;

配置文件優(yōu)化:

nginx_vhosts.conf配置文件拆分:

sed -n '1,9p' nginx_vhosts.conf > www..conf

sed -n '10,19p' nginx_vhosts.conf > bbs..conf

sed -n '20,29p' nginx_vhosts.conf > blog..conf

編輯主配置文件,將include ../extra/nginx_vhosts.conf;替換為:

include ../extra/www..conf;

include ../extra/bbs..conf;

include ../extra/blog..conf;

配置Nginx狀態(tài)信息虛擬主機:

cat  >>/usr/local/nginx/conf/extra/nginx_vhosts.conf <<EOF  #將下面這個server追加到Nginx的虛擬主機配置文件中。

######

server

{

listen 80;

server_name status.;

location / {

stub_status on;

access_log off;

}

}

EOF

2)基于端口的虛擬主機配置

只需修改配置文件: nginx_vhosts.conf 中的每個虛擬主機中的端口即可,即,將默認的80端口改成別的端口,如8060、8070、8080等;

然后重新加載配置文件。

3)基于IP地址的虛擬主機配置

此種比較少用,了解下即可

如果要測試此類虛擬主機,需要在測試機上增加幾個IP地址(在負載均衡配置里,此IP也被稱為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 改成這幾個IP地址,將每個端口前面加上對應(yīng)的IP地址;

實例:

[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

后面兩個與之類似

修改完成后,檢查配置文件語法,重新加載配置文件,進行測試;

檢查語法 : /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. ;

     4          location / {

     5              root   /data/www/www;

     6              index  index.html index.htm;

     7              access_log /app/log/www_access.log commonlog;

     8          }

     9      }

說明:別名,就寫在域名后面就行了,以空格分開即可;

應(yīng)用場景:

1)如輸入baidu.com會自動跳轉(zhuǎn)到www.baidu.com,輸入taobao.com會自動跳轉(zhuǎn)到www.taobao.com,這里我們就是要實現(xiàn)輸入和訪問www.一樣顯示我們的網(wǎng)站。

2)在生產(chǎn)環(huán)境中,利用別名來監(jiān)控集群環(huán)境下面的RS的URL是否正常。

如:ServerAlias 21www. 22www.

可以在監(jiān)控服務(wù)里面配置hosts來監(jiān)控RS  21www. 22www.等地址是否正常,進而判斷每臺機器的www.是否正常。

如不使用別名,則很難通過URL方式檢測判斷下面的機器是否正常(因為域名是同一個)。

當(dāng)然,Keepalived和Haproxy等軟件也有對于RS使用URL的健康檢查措施,這方面的案例見相關(guān)文檔。

配置多個Nginx實例

Nginx安裝后的啟動命令在“/usr/local/nginx/sbin/nginx”,我們可以通過 -h 參數(shù)查看相關(guān)參數(shù)的用法:

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  #測試配置文件是否正確,在運行時需要重新加載配置的時候,此命令非常重要,用來檢測所修改的配置文件是否存在語法錯誤。
  -q            : suppress non-error messages during configuration testing 
   -s signal     : send signal to a master process: stop, quit, reopen, reload  #發(fā)送信號給一個master進程,這里的reload參數(shù)很重要,是優(yōu)雅重啟Nginx的參數(shù),類似Apache的graceful參數(shù)。
  -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。借此可以啟動多個Nginx實例。
  -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不能沖突,否則起不來的,這樣可以單獨開啟nginx實例。

cp -af: 

殺掉nginx:pkill nginx

啟動多實例:

  • /usr/local/nginx/sbin/nginx -c /etc/nginx/nginx.conf

  • /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

應(yīng)用場景:

服務(wù)器資源有限,且單個服務(wù)器的性能又很空余,又希望各http相對獨立,此時就可以采用nginx多實例的方式;


本文出自 “冷水泡茶” 博客,請務(wù)必保留此出處http://miaocbin.blog.51cto.com/689091/1692772

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多