1. 程式人生 > 其它 >Nginx安裝,目錄結Nginx安裝,目錄結構與配置檔案詳解構與配置檔案詳解

Nginx安裝,目錄結Nginx安裝,目錄結構與配置檔案詳解構與配置檔案詳解

閱讀目錄

回到頂部

1.Nginx簡介

  Nginx(發音同 engine x)是一款輕量級的Web 伺服器/反向代理伺服器及電子郵件(IMAP/POP3)代理伺服器,並在一個BSD-like 協議下發行。由俄羅斯的程式設計師Igor Sysoev所開發,最初供俄國大型的入口網站及搜尋引擎Rambler(俄文:Рамблер)使用。 其特點是佔有記憶體少,併發能力強,事實上nginx的併發能力確實在同類型的網頁伺服器中表現較好.目前中國大陸使用nginx網站使用者有:新浪、網易、 騰訊,另外知名的微網誌Plurk也使用nginx。

a)為什麼Nginx的總體效能比Apache高?

Nginx使用最新的epoll(Linux2.6核心)和kqueue(freebsd)網路I/O模型,而Apache使用的是傳統的select模型,目前Linux下能夠承受併發訪問的Squuid,Memcached都是採用的是epoll網路I/O模型。

b)那麼如何正確的選擇web伺服器呢?

靜態業務:高併發,採用Nginx或者lighttpd
動態業務:採用Nginx或Apache均可
既有動態業務也有靜態業務:Nginx或Apache均可,不要多選要單選
動態業務可有前段代理(haproxy),根據頁面元素的型別,向後轉發相應的伺服器進行處理。
說明:Nginx做web(Apache,lighttpd)反向代理(haproxy,lvs,nat)快取伺服器(squid)

c)windows瀏覽器訪問報錯處理解決方法:

1)ping 跟ip 物理通不通
2)telnet ip :埠 瀏覽器到web服務通不通
3)伺服器本地curl ip web服務開沒開
4)檢視錯誤日誌如:cat /application/nginx/error_log

Pcre全稱(Perl Compatible Regular Expressions),中文perl相容正則表示式,官方站點為http://www.pcre.org,安裝pcre庫是為了使Nginx支援HTTP Rewrite模組,安裝如下:

1)檢視當前Linux系統環境,命令如下:
[root@www ~]# cat /etc/redhat-release
 CentOS release 6.6(Final)
[root@www ~]# uname -r
 2.6.32-504.el6.x86_64
[root@www ~]# uname -m
 x86_64#<==64位系統
2)採用yum安裝方式安裝pcre,命令如下:
[root@www ~]# yum install pcre pcre-devel -y
[root@www ~]# rpm -qa pcre pcre-devel

3)yum安裝操作後檢查安裝結果,命令如下:(pcre-7.8-6.el6.x86_64提示:yum安裝的pcre版本有些低,不過一般情況不影響使用)

[root@www ~]# rpm -qa pcre pcre-devel
 pcre-devel-7.8-6.el6.x86_64

4)編譯安裝pcre

wget    http://sourceforge.net/projects/pcre/files/pcre/7.80/pcre-7.80.tar.gz
tar zxf pcre-7.80.tar.gz
cd pcre-7.80
./configure
make && make install
cd ../ 
回到頂部

2.安裝Nginx

  Nginx的英文官方網站是http://nginx.org/,在這裡可以檢視Nginx的各個軟體版本資訊。Nginx軟體有三種版本:穩定版、開發版和歷史穩定版

  1.檢查並安裝Nginx基礎依賴包pcre-devel、openssl-devel操作命令如下:

[root@www ~]# rpm -qa pcre-devel pcre
 pcre-devel-7.8-6.el6.x86_64       #<==pcre的devel包已經安裝!
 pcre-7.8-6.el6.x86_64             #<==pcre包已經安裝!
[root@www ~]# rpm -qa openssl-devel openssl
 openssl-1.0.1e-30.el6.x86_64       #<==這裡沒有opensll“devel”字串的包

  2.安裝openssl-devel,安裝openssl-devel的命令及檢查命令如下:

[root@www ~]# yum install -y openssl openssl-devel
[root@www ~]# rpm -qa openssl openssl-devel
 openssl-devel-1.0.1e-30.el6.8.x86_64
 openssl-1.0.1e-30.el6.8.x86_64
  3.開始安裝Nginx
wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
#下載軟體包,進入http://nginx.org/download/ 複製對應版本的連結地址。提示,如果發現Nginx軟體下載地址已不可用,可能版本已更新,可去官方地址http://www.nginx.org下載。

  4.安裝的操作過程演示

[root@www ~]# mkdir -p /home/tools
[root@www ~]# cd /home/tools
[root@www tools]# wget -q http://nginx.org/download/nginx-1.6.3.tar.gz
[root@www tools]# ls -l nginx-1.6.3.tar.gz 
-rw-r--r--1 root root 804164 11月 23 15:26 nginx-1.6.3.tar.gz
[root@www tools]# useradd nginx -s /sbin/nologin -M
[root@www tools]# tar xf nginx-1.6.3.tar.gz 
[root@www tools]# cd nginx-1.6.3
[root@www nginx-1.6.3]#./configure  --user=nginx --group=nginx --prefix=/application/nginx --with-http_stub_status_module  --with-http_ssl_module
[root@www nginx-1.6.3]# make
[root@www nginx-1.6.3]# make install
[root@www nginx-1.6.3]# ln -s /application/nginx-1.6.3 /application/nginx
[root@www nginx-1.6.3]# ls -l /application/nginx/總用量 16
drwxr-xr-x. 2 root root 4096 7月  20 11:19 conf
drwxr-xr-x. 2 root root 4096 7月  20 11:19 html
drwxr-xr-x. 2 root root 4096 7月  20 11:19 logs
drwxr-xr-x. 2 root root 4096 7月  20 11:19 sbin

  安裝過程出現如下錯誤,執行命令:

yum install openssl openssl-devel-y

  出現的錯誤資訊如下:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules,or install the OpenSSL library
into the system,or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
回到頂部

3.啟動並檢查安裝結果

  1- 啟動前檢查配置檔案語法,執行命令

[root@www tools]# /application/nginx/sbin/nginx -t
nginx: the configuration file /application/nginx-1.6.3/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.6.3/conf/nginx.conf test is successful

  2-啟冬Nginx服務,執行命令

/application/nginx/sbin/nginx

  3-檢視Nginx服務對應的埠是否啟動

方法一; lsof -i :80

方法二; netstat -lnt|grep 80

  4.檢查Nginx啟動實際效果

  在瀏覽器輸入 http://10.0.0.8 (10.0.0.8wei an安裝Nginx伺服器的IP地址)

  在Linux 用wget命令檢測    wget 本機的連線的IP地址

  curl命令檢測         curl本機連線的ip地址()

回到頂部

4.Nginx軟體功能模組說明

  1)Nginx核心功能模組(Coer functionality),主要負責Nginx的全域性應用,針對主配置檔案Main區塊和Events區塊,http://nginx.org/en/docs/ngx_core_module.html(更多檢視)

  2)標準的http功能模組集合,http://nginx.org/en/docs(可以檢視更多的功能模組)

                   企業應用場景功能模組彙總

   

回到頂部

5. Nginx目錄結構

  執行命令 tree /application/nginx ;如果tree命令找不到就執行 yum install tree -y安裝

 1 [root@www ~]# tree /application/nginx/
 2 /application/nginx/
 3 |-- client_body_temp
 4 |-- conf                                  #這是Nginx所有配置檔案的目錄,極其重要
 5 |   |-- fastcgi.conf                    #fastcgi相關引數的配置檔案
 6 |   |-- fastcgi.conf.default                 #fastcgi.conf的原始備份
 7 |   |-- fastcgi_params                   #fastcgi的引數檔案
 8 |   |-- fastcgi_params.default
 9 |   |-- koi-utf
10 |   |-- koi-win
11 |   |-- mime.types                      #媒體型別,
12 |   |-- mime.types.default
13 |   |-- nginx.conf                      #這是Nginx預設的主配置檔案
14 |   |-- nginx.conf.default
15 |   |-- scgi_params                     #scgi相關引數檔案,一般用不到
16 |   |-- scgi_params.default
17 |   |-- uwsgi_params                       #uwsgi相關引數檔案,一般用不到
18 |   |-- uwsgi_params.default
19 |   `-- win-utf
20 |-- fastcgi_temp                       #fastcgi臨時資料目錄
21 |-- html                       #這是編譯安裝時Nginx的預設站點目錄,類似
22                     Apache的預設站點htdocs目錄
23 |   |--50x.html     #     錯誤頁面優雅替代顯示檔案,例如:出現502錯誤時會呼叫此頁面
24          #     error_page   500502503504  /50x.html;
25 |   `-- index.html   #     預設的首頁檔案,首頁檔名字是在nginx.conf中事先定義好的。
26 |-- logs          #這是Nginx預設的日誌路徑,包括錯誤日誌及訪問日誌
27 |   |-- access.log      #     這是Nginx的預設訪問日誌檔案,使用tail -f access.log,可以實時觀看網站使用者訪問情況資訊
28 |   |-- error.log      #     這是Nginx的錯誤日誌檔案,如果Nginx出現啟動故障等問題,一定要看看這個錯誤日誌
29 |   `-- nginx.pid      #     Nginx的pid檔案,Nginx程序啟動後,會把所有程序的ID號寫到此檔案
30 |-- proxy_temp       #臨時目錄
31 |-- sbin      #這是Nginx命令的目錄,如Nginx的啟動命令nginx
32 |   `-- nginx      #Nginx的啟動命令nginx
33 |-- scgi_temp      #臨時目錄
34 `-- uwsgi_temp      #臨時目錄
35 9 directories,21 files
回到頂部

6)Nginx主配置檔案nginx.conf

  1.Nginx主配置檔案nginx.conf是一個純文字型別的檔案(其他配置檔案也是如此)Nginx整個配置檔案nginx.conf主題框架:

  

  2.檢視Nginx配置檔案

 1 [root@www conf]# egrep -v "#|^$" nginx.conf.default     ←去掉包含#號和空行的內容
 2 worker_processes  1;                ← worker程序的數量
 3 events {                              ← 事件區塊開始
 4     worker_connections  1024;            ←每個worker程序支援的最大連線數
 5 }                                    ←事件區塊結束
 6 http {                               ← HTTP區塊開始
 7     include       mime.types;            ← Nginx支援的媒體型別庫檔案
 8     default_type  application/octet-stream;     ← 預設的媒體型別
 9     sendfile        on;       ←開啟高效傳輸模式
10     keepalive_timeout  65;       ←連線超時
11     server {            ←第一個Server區塊開始,表示一個獨立的虛擬主機站點
12         listen       80;      ← 提供服務的埠,預設80
13         server_name  localhost;       ←提供服務的域名主機名
14         location / {            ←第一個location區塊開始
15             root   html;       ←站點的根目錄,相當於Nginx的安裝目錄
16             index  index.html index.htm;      ← 預設的首頁檔案,多個用空格分開
17         }          ←第一個location區塊結果
18         error_page   500502503504  /50x.html;     ← 出現對應的http狀態碼時,使用50x.html迴應客戶
19         location = /50x.html {          ←location區塊開始,訪問50x.html
20             root   html;      ← 指定對應的站點目錄為html
21         }
22     }

Nginx配置檔案

  1 user nginx nginx;                #定義Nginx執行的使用者和使用者組
  2 worker_processes 1;            #nginx程序數,建議設定為等於CPU總核心數。
  3 error_log /var/log/nginx/error.log info;      #全域性錯誤日誌定義型別,[ debug | info | notice | warn | error | crit ]
  4 pid /var/run/nginx.pid;                #程序檔案
  5 worker_rlimit_nofile 1024;          #一個nginx程序開啟的最多檔案描述符數目,理論值應該是最多開啟檔案數(系統的值ulimit -n)與nginx程序數相除,但是nginx分配請求並不均勻,所以#建議與ulimit -n的值保持一致
  6 
  7 events
  8 {
  9 use epoll;          #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本核心中的高效能網路I/O模型,
                如果跑在FreeBS  #D上面,就用kqueue模型。 10 11 worker_connections 65535;     #單個程序最大連線數(最大連線數=連線數*程序數) 12 } 13 14 15 16 http                  #HTTP區塊開始 17 { 18 include mime.types;                   #Nginx支援的媒體型別庫檔案 19 default_type application/octet-stream;       #預設媒體型別 20 #charset utf-8;                     #預設編碼 21 server_names_hash_bucket_size 128;         #伺服器名字的hash表大小 22 client_header_buffer_size 32k;            #上傳檔案大小限制 23 large_client_header_buffers 4 64k;          #設定請求緩 24 client_max_body_size 8m;                #設定請求緩 25 sendfile on;       #開啟高效檔案傳輸模式,sendfile指令指定nginx是否呼叫sendfile函式來輸出檔案,對於普通應用設為 on,如果用來進行下載等應用磁碟IO重負載應用,可設定為o  #ff,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。
 26 autoindex on;                   #開啟目錄列表訪問,合適下載伺服器,預設關閉。
 27 tcp_nopush on;               #防止網路阻塞
 28 tcp_nodelay on;              #防止網路阻塞
 29 keepalive_timeout 120;             #連線超時,單位是秒
 30 
 31 #FastCGI相關引數是為了改善網站的效能:減少資源佔用,提高訪問速度。
 32 fastcgi_connect_timeout 300;
 33 fastcgi_send_timeout 300;
 34 fastcgi_read_timeout 300;
 35 fastcgi_buffer_size 64k;
 36 fastcgi_buffers 4 64k;
 37 fastcgi_busy_buffers_size 128k;
 38 fastcgi_temp_file_write_size 128k;
 39 
 40 #gzip模組設定
 41 gzip on;                         #開啟gzip壓縮輸出
 42 gzip_min_length 1k;                 #最小壓縮檔案大小
 43 gzip_buffers 4 16k;                #壓縮緩衝區
 44 gzip_http_version 1.0;               #壓縮版本(預設1.1,前端如果是squid2.5請使用1.0)
 45 gzip_comp_level 2;                   #壓縮等級
 46 gzip_types text/x-javascript text/css application/xml;    #壓縮型別,預設就已經包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
 47 gzip_vary on;
 48 #limit_zone crawler $binary_remote_addr 10m;   #開啟限制IP連線數的時候需要使用
 49 
 50 
 51 
 52 #虛擬主機的配置
 53 server
 54 {
 55 
 56 listen 80;                         #監聽埠
 57 
 58 server_name localhost;                  #提供服務的域名主機名
 59 location / {                    #第一個location區塊開始
 60 root html;                      #站點的根目錄,相當於Nginx的安裝目錄
 61 index index.html index.htm index.jsp;        #預設的首頁檔案,多個用空格分開
 62 }                          #第一個location區塊結果
 63 
 64 
 65 
 66 #圖片快取時間設定
 67 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
 68 {
 69 expires 10d;
 70 }
 71 
 72 
 73 #JS和CSS快取時間設定
 74 location ~ .*\.(js|css)?$
 75 {
 76 expires 1h;
 77 }
 78 
 79 
 80 #日誌格式設定
 81 log_format access '$remote_addr - $remote_user [$time_local] "$request" '
 82 '$status $body_bytes_sent "$http_referer" '
 83 '"$http_user_agent" $http_x_forwarded_for';
 84 
 85 access_log /var/log/nginx/access_$(data+%F -d -1day).log access;    #定義本虛擬主機的訪問日誌
 86 
 87 
 88 
 89 location / {                              #對 "/" 啟用反向代理
 90 proxy_pass http://127.0.0.1:88;
 91 proxy_redirect off;
 92 proxy_set_header X-Real-IP $remote_addr;        #後端的Web伺服器可以通過X-Forwarded-For獲取使用者真實IP
 93 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 94 
 95 #以下是一些反向代理的配置,可選
 96 proxy_set_header Host $host;
 97 client_max_body_size 10m;                 #允許客戶端請求的最大單檔案位元組數
 98 client_body_buffer_size 128k;             #緩衝區代理緩衝使用者端請求的最大位元組數,
 99 proxy_connect_timeout 90;            #nginx跟後端伺服器連線超時時間(代理連線超時)
100 proxy_send_timeout 90;                 #後端伺服器資料回傳時間(代理髮送超時)
101 proxy_read_timeout 90;            #連線成功後,後端伺服器響應時間(代理接收超時)
102 proxy_buffer_size 4k;                 #設定代理伺服器(nginx)儲存使用者頭資訊的緩衝區大小
103 proxy_buffers 4 32k;                 #proxy_buffers緩衝區,網頁平均在32k以下的設定
104 proxy_busy_buffers_size 64k;             #高負荷下緩衝大小(proxy_buffers*2)
105 proxy_temp_file_write_size 64k;                #設定快取資料夾大小,大於這個值,將從upstream伺服器傳
106 
107 }
108 
109 #設定檢視Nginx狀態的地址
110 location /NginxStatus {
111 stub_status on;
112 access_log on;
113 auth_basic "NginxStatus";
114 auth_basic_user_file conf/htpasswd;          #htpasswd檔案的內容可以用apache提供的htpasswd工具來產生。
115 
116 }
117 
118 #本地動靜分離反向代理配置
119 #所有jsp的頁面均交由tomcat或resin處理
120 location ~ .(jsp|jspx|do)?$ {
121 proxy_set_header Host $host;
122 proxy_set_header X-Real-IP $remote_addr;
123 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
124 proxy_pass http://127.0.0.1:8080;
125 }
126 
127 
128 #所有靜態檔案由nginx直接讀取不經過tomcat或resin
129 location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
130 { expires 15d; }
131 location ~ .*.(js|css)?$
132 { expires 1h; }
133 }
134 }