1. 程式人生 > >Nginx安裝與使用

Nginx安裝與使用

表示 cli 3.1 replace 需要 網站 pop emp 文字

Nginx安裝與使用

Nginx是一款輕量級的Web 服務器/反向代理服務器及電子郵件(IMAP/POP3)代理服務器,並在一個BSD-like 協議下發行。由俄羅斯的程序設計師Igor Sysoev所開發,供俄國大型的入口網站及搜索引擎Rambler(俄文:Рамблер)使用。其特點是占有內存少,並發能力強,事實上nginx的並發能力確實在同類型的網頁服務器中表現較好。(百度百科- http://www.dwz.cn/x32kG)

1. Nginx安裝

我使用的環境是64位 Ubuntu 14.04。nginx依賴以下模塊:

  • gzip模塊需要 zlib 庫

  • rewrite模塊需要 pcre 庫

  • ssl 功能需要openssl庫

1.1. 安裝pcre

  1. 獲取pcre編譯安裝包,在http://www.pcre.org/上可以獲取當前最新的版本
  2. 解壓縮pcre-xx.tar.gz包。
  3. 進入解壓縮目錄,執行./configure –prefix==/usr/local/pcre。
  4. make & make install

1.2. 安裝openssl

  1. 獲取openssl編譯安裝包,在http://www.openssl.org/source/上可以獲取當前最新的版本。

  2. 解壓縮openssl-xx.tar.gz包。

  3. 進入解壓縮目錄,執行./config。

  4. make & make install

1.3.安裝zlib

  1. 獲取zlib編譯安裝包,在http://www.zlib.net/上可以獲取當前最新的版本。

  2. 解壓縮openssl-xx.tar.gz包。

  3. 進入解壓縮目錄,執行./configure。

  4. make & make install

1.4.安裝nginx

  1. 獲取nginx,在http://nginx.org/en/download.html上可以獲取當前最新的版本。

  2. 解壓縮nginx-xx.tar.gz包。

  3. 進入解壓縮目錄,執行./configure

  4. make & make install

若安裝時找不到上述依賴模塊,使用–with-openssl=、–with-pcre=、–with-zlib=指定依賴的模塊目錄。如已安裝過,此處的路徑為安裝目錄;若未安裝,則此路徑為編譯安裝包路徑,nginx將執行模塊的默認編譯安裝。

啟動nginx之後,瀏覽器中輸入http://localhost可以驗證是否安裝啟動成功。
技術分享

2.Nginx配置

安裝完成之後,配置目錄conf下有以下配置文件,過濾掉了xx.default配置:

[email protected]:/opt/nginx-1.7.7/conf$ tree |grep -v default
.
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

除了nginx.conf,其余配置文件,一般只需要使用默認提供即可。

2.1.nginx.conf

nginx.conf是主配置文件,默認配置去掉註釋之後的內容如下圖所示:

  • worker_process表示工作進程的數量,一般設置為cpu的核數

  • worker_connections表示每個工作進程的最大連接數

  • server{}塊定義了虛擬主機

    • listener監聽端口

    • server_name監聽域名

    • location{}是用來為匹配的 URI 進行配置,URI 即語法中的“/uri/”。location / { }匹配任何查詢,因為所有請求都以 / 開頭。

      • root指定對應uri的資源查找路徑,這裏html為相對路徑,完整路徑為/opt/ opt/nginx-1.7.7/html/

      • index指定首頁index文件的名稱,可以配置多個,以空格分開。如有多個,按配置順序查找。

技術分享

從配置可以看出,nginx監聽了80端口、域名為localhost、跟路徑為html文件夾(我的安裝路徑為/opt/nginx-1.7.7,所以/opt/nginx-1.7.7/html)、默認index文件為index.html, index.htm、服務器錯誤重定向到50x.html頁面。

可以看到/opt/nginx-1.7.7/html/有以下文件:

[email protected]:/opt/nginx-1.7.7/html$ ls
50x.html  index.html

這也是上面在瀏覽器中輸入http://localhost,能夠顯示歡迎頁面的原因。實際上訪問的是/opt/nginx-1.7.7/html/index.html文件。

2.2.mime.types

文件擴展名與文件類型映射表,nginx根據映射關系,設置http請求響應頭的Content-Type值。當在映射表找不到時,使用nginx.conf中default-type指定的默認值。例如,默認配置中的指定的default-type為application/octet-stream。

include       mime.types;
default_type  application/octet-stream;

默認

下面截一段mime.types定義的文件擴展名與文件類型映射關系,完整的請自行查看:

技術分享

2.3.fastcgi_params

nginx配置Fastcgi解析時會調用fastcgi_params配置文件來傳遞服務器變量,這樣CGI中可以獲取到這些變量的值。默認傳遞以下變量:

技術分享

這些變量的作用從其命名可以看出。

2.4.fastcgi.conf

對比下fastcgi.conf與fastcgi_params文件,可以看出只有以下差異:

[email protected]:/opt/nginx-1.7.7/conf$ diff fastcgi.conf fastcgi_params
2d1
< fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

即fastcgi.conf只比fastcgi_params多了一行“fastcgi_param SCRIPT_FILENAME documentrootfastcgi_script_name;”

原本只有fastcgi_params文件,fastcgi.conf是nginx 0.8.30 (released: 15th of December 2009)才引入的。主要為是解決以下問題(參考:http://www.dwz.cn/x3GIJ):

原本Nginx只有fastcgi_params,後來發現很多人在定義SCRIPT_FILENAME時使用了硬編碼的方式。例如,fastcgi_param SCRIPT_FILENAME /var/www/foo$fastcgi_script_name。於是為了規範用法便引入了fastcgi.conf。

不過這樣的話就產生一個疑問:為什麽一定要引入一個新的配置文件,而不是修改舊的配置文件?這是因為fastcgi_param指令是數組型的,和普通指令相同的是:內層替換外層;和普通指令不同的是:當在同級多次使用的時候,是新增而不是替換。換句話說,如果在同級定義兩次SCRIPT_FILENAME,那麽它們都會被發送到後端,這可能會導致一些潛在的問題,為了避免此類情況,便引入了一個新的配置文件。

因此不再建議大家使用以下方式(搜了一下,網上大量的文章,並且nginx.conf的默認配置也是使用這種方式):

fastcgi_param SCRIPT_FILENAME documentrootfastcgi_script_name;

include fastcgi_params;

而使用最新的方式:

include fastcgi.conf;

2.5.uwsgi_params

與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param。

2.6.scgi_params

與fastcgi_params一樣,傳遞哪些服務器變量,只有前綴不一樣,以uwsgi_param開始而非fastcgi_param。

2.7.koi-utf、koi-win、win-utf

這三個文件都是與編碼轉換映射文件,用於在輸出內容到客戶端時,將一種編碼轉換到另一種編碼。

koi-win: charset_map koi8-r < – > windows-1251

koi-utf: charset_map koi8-r < – > utf-8

win-utf: charset_map windows-1251 < – > utf-8

koi8-r是斯拉夫文字8位元編碼,供俄語及保加利亞語使用。在Unicode未流行之前,KOI8-R 是最為廣泛使用的俄語編碼,使用率甚至起ISO/IEC 8859-5還高。這3個文件存在是因為作者是俄國人的原因。

3.啟動和修改配置

3.1 啟動

確保系統的 80 端口沒被其他程序占用,
/usr/local/nginx/sbin/nginx

檢查是否啟動成功:
netstat -ano|grep 80 有結果輸入說明啟動成功

打開瀏覽器訪問此機器的 IP,如果瀏覽器出現 Welcome to nginx! 則表示 Nginx 已經安裝並運行成功。

3.2 重啟

/usr/local/nginx/sbin/nginx –s reload

4 修改配置文件nginx.conf + 常用配置

修改配置文件nginx.conf 
    cd /usr/local/nginx/conf
    vi nginx.conf

4.1 nginx運行用戶和組

user    www www;  

4.2 啟動進程,通常設置成和cpu的數量相等

worker_processes  4;

4.3 全局錯誤日誌及PID文件

pid /var/run/nginx.pid;
error_log  /var/log/nginx/error.log;

events {
        #epoll是多路復用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能
use epoll;
                   #單個後臺worker process進程的最大並發鏈接數
        worker_connections  10240;
}

4.4 設定http服務器,利用它的反向代理功能提供負載均衡支持

http {
include mime.types;

    default_type  application/octet-stream;

     error_page 400 403 500 502 503 504  /50x.html;

    index index.html index.shtml

    autoindex off;

     fastcgi_intercept_errors on;

    sendfile        on;

    # These are good default values.
    tcp_nopush      on;
    tcp_nodelay     off;

    # output compression saves bandwidth
    gzip  off;
     #gzip_static on;
    #gzip_min_length  1k;
    gzip_http_version 1.0;
    gzip_comp_level 2;
    gzip_buffers  4 16k;
    gzip_proxied any;
    gzip_disable "MSIE [1-6]\.";
    gzip_types  text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;
    #gzip_vary on;

    server_name_in_redirect off;

4.5 設定負載均衡的服務器列表

    upstream portals {
              server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;
              server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;
                        server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;
              server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;
    }

    #upstream overflow {
     #       server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;       
     #       server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;       
    #}

    server {
                                 #偵聽8080端口
            listen       8080;
            server_name  127.0.0.1;

               #403、404頁面重定向地址
               error_page  403 = http://www.e100.cn/ebiz/other/217/403.html;
               error_page  404 = http://www.e100.cn/ebiz/other/218/404.html;
               proxy_connect_timeout      90;
               proxy_send_timeout         180;
               proxy_read_timeout         180;

               proxy_buffer_size 64k;
               proxy_buffers 4 128k;
               proxy_busy_buffers_size 128k;


               client_header_buffer_size 16k;
               large_client_header_buffers 4 64k;

            #proxy_send_timeout         3m;
            #proxy_read_timeout         3m;
            #proxy_buffer_size          4k;
            #proxy_buffers              4 32k;

            proxy_set_header Host $http_host;
            proxy_max_temp_file_size 0;
            #proxy_hide_header Set-Cookie;

     #       if ($host != ‘www.e100.cn‘ ) {
     #                rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;
     #       }


           location / {
                   deny all;
           }

               location ~ ^/resource/res/img/blue/space.gif {
                proxy_pass http://tecopera;
           }

           location = / {
               rewrite ^(.*)$  /ebiz/event/517.html last;
           }



               location = /ebiz/event/517.html {
                add_header Vary Accept-Encoding;
                root /data/web/html;
                expires 10m;
           }




           location = /check.html {
                root /usr/local/nginx/html/;
                access_log off;
           }

           location = /50x.html {
                root /usr/local/nginx/html/;
                expires 1m;
                access_log off;
           }


          location = /index.html {
                   add_header Vary Accept-Encoding;

4.6 定義服務器的默認網站根目錄位置

                root /data/web/html/ebiz;
                expires 10m;
           }

4.7 定義反向代理訪問名稱

               location ~ ^/ecps-portal/* {
               # expires 10m;

4.8 重定向集群名稱

                proxy_pass http://portals;
                #proxy_pass http://172.16.68.134:8082;
           }

               location ~ ^/fetionLogin/* {
               # expires 10m;
                proxy_pass http://portals;
                #proxy_pass http://172.16.68.134:8082;
            }

               #location  ~ ^/business/* {                                                                      
            #   # expires 10m;                                                                                
            #    proxy_pass http://172.16.68.132:8088;                                                                   
            #    #proxy_pass http://172.16.68.134:8082;                                                       
            #}

               location ~ ^/rsmanager/* {
                expires 10m;
                root /data/web/;
                #proxy_pass http://rsm;
           }

4.9 定義nginx處理的頁面後綴

               location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$  {
                        root /data/web/html/;

4.10 頁面緩存時間為10分鐘

                     expires 10m;
               }

4.11 設定查看Nginx狀態的地址

               location ~* ^/NginxStatus/ {
                    stub_status on;
                    access_log off;
                    allow 10.1.252.126;
                    allow 10.248.6.49;
                    allow 127.0.0.1;
                    deny all;
               }
         #       error_page   405 =200 @405;
         #       location @405
         #       {
         #                proxy_pass http://10.248.6.45:8080;
         #       }  

               access_log  /data/logs/nginx/access.log combined;
               error_log   /data/logs/nginx/error.log;
        }
         server {
                listen       8082;

                server_name  _;
               location = /check.html {
                    root /usr/local/nginx/html/;
                    access_log off;
               }

        }
         server {
                   listen       8088;
                   server_name  _;
                   location ~ ^/* {
                   root /data/web/b2bhtml/;
                   access_log off;
         }                
         }
        server {
                listen       9082;
                server_name  _;

        #        location ~ ^/resource/* {
        #            expires 10m;
         #           root /data/web/html/;
         #       }

                location  / {
                     root /data/web/html/sysMaintain/;
                       if (!-f $request_filename) {
                            rewrite ^/(.*)$ /sysMaintain.html last;
                           }
                }
        }

}

5.相關鏈接

http://www.cnblogs.com/skynet/p/4146083.html
http://www.cnblogs.com/zhuhongbao/archive/2013/06/04/3118061.html

http://www.pcre.org/

http://www.openssl.org/source/

http://www.zlib.net/

http://nginx.org/

百度百科:http://www.dwz.cn/x32kG

fastcgi.conf vs fastcgi_params:http://www.dwz.cn/x3GIJ

Nginx安裝與使用