天天看點

Nginx指南和配置詳解

Nginx指南

Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 伺服器,也是一個 IMAP/POP3/SMTP 代理伺服器。 Nginx 因它的穩定性、豐富的功能集、示例配置檔案和低系統資源的消耗而聞名了。更多的請見官方wiki: http://wiki.nginx.org/Main 。

其優點:

Nginx做為HTTP伺服器,有以下幾項基本特性:

處理靜态檔案,索引檔案以及自動索引;打開檔案描述符緩沖.

無緩存的反向代理加速,簡單的負載均衡和容錯.

一、依賴的程式

gzip module requires zlib library

rewrite module requires pcre library

ssl support requires openssl library

二、安裝

$./configure
$make
$make install      

預設安裝的路徑是/usr/local/nginx 

更多的安裝配置

$./configure --prefix=/usr/local/nginx
                 --with-openssl=/usr/include (啟用ssl)
                 --with-pcre=/usr/include/pcre/ (啟用正規表達式)
                 --with-http_stub_status_module (安裝可以檢視nginx狀态的程式)
                 --with-http_memcached_module (啟用memcache緩存)
                 --with-http_rewrite_module (啟用支援url重寫)      

三、nginx指令

-?,-h         : this help
  -v            : show version and exit
  -V            : show version and configure options then exit
  -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
  -p prefix     : set prefix path (default: /usr/local/nginx/)
  -c filename   : set configuration file (default: conf/nginx.conf)
  -g directives : set global directives out of configuration file      
啟動:nginx

重新開機:nginx -s reload

退出:nginx -s quit

測試配置檔案:nginx -t      

Nginx配置詳解

在進行Nginx配置的時候會出現很多不明白的地方,其實有些時候隻要換一個思維的方式就能找多你要解決問題的方法。官方配置樣例:http://wiki.nginx.org/NginxFullExample

#運作使用者   
   user nobody nobody;   
   #啟動程序   
   worker_processes 4;   
   #全局錯誤日志及PID文檔  [ debug | info | notice | warn | error | crit ] 
   error_log logs/error.log notice;   
   pid logs/Nginx.pid;   
   #工作模式及連接配接數上限   
   events { 
        #工作模式有:# use [ kqueue | rtsig | epoll | /dev/poll | select | poll ] ;  
        use epoll;   
        worker_connections 1024;   
  }   
  #設定http伺服器,利用他的反向代理功能提供負載均衡支援   
  http {   
       #設定mime類型   
       include conf/mime.types;   
       default_type application/octet-stream;   
       #設定日志格式   
       log_format main '$remote_addr - $remote_user [$time_local] '   
                             '"$request" $status $bytes_sent '   
                             '"$http_referer" "$http_user_agent" '   
                             '"$gzip_ratio"';   
       log_format download '$remote_addr - $remote_user [$time_local] '   
                             '"$request" $status $bytes_sent '   
                             '"$http_referer" "$http_user_agent" '   
                             '"$http_range" "$sent_http_content_range"';   
        #設定請求緩沖   
       client_header_buffer_size 1k;   
       large_client_header_buffers 4 4k;   
       #開啟gzip子產品   
       gzip on;   
       gzip_min_length 1100;   
       gzip_buffers 4 8k;   
       gzip_types text/plain;   
       output_buffers 1 32k;   
       postpone_output 1460;   
       #設定access log   
       access_log logs/access.log main;   
       client_header_timeout 3m;   
       client_body_timeout 3m;   
       send_timeout 3m;   
       sendfile on;   #指令激活或停用的sendfile()的用法。
       tcp_nopush on;   
       tcp_nodelay on;   
       keepalive_timeout 65;   
       #設定負載均衡的伺服器清單   
       upstream mysvr {   
              #weigth參數表示權值,權值越高被配置設定到的幾率越大   
              #本機上的Squid開啟3128端口   
              server 192.168.8.1:3128 weight=5;   
              server 192.168.8.2:80 weight=1;   
              server 192.168.8.3:80 weight=6;   
       }   
      #設定虛拟主機   
      server {   
              listen 80;   
              server_name 192.168.8.1  www.yejr.com;   
              charset utf8;   
              #設定本虛拟主機的通路日志   
              access_log logs/www.yejr.com.access.log main;   
              #假如通路 /img/*, /js/*, /css/* 資源,則直接取本地文檔,不通過squid   
             #假如這些文檔較多,不推薦這種方式,因為通過squid的緩存效果更好   
             location ~ ^/(img|js|css)/ {   
                    root /data3/Html;   
                    expires 24h;   
             }   
             #對 "/" 啟用負載均衡   
             location / {   
                     proxy_pass http://mysvr;   
                     proxy_redirect off;   
                     proxy_set_header Host $host;   
                     proxy_set_header X-Real-IP $remote_addr;   
                     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   
                     client_max_body_size 10m;   
                     client_body_buffer_size 128k;   
                     proxy_connect_timeout 90;   
                     proxy_send_timeout 90;   
                     proxy_read_timeout 90;   
                     proxy_buffer_size 4k;   
                     proxy_buffers 4 32k;   
                     proxy_busy_buffers_size 64k;   
                     proxy_temp_file_write_size 64k;   
            }   
            #設定檢視Nginx狀态的位址   
            location /NginxStatus {   
                     stub_status on;   
                     access_log on;   
                     auth_basic "NginxStatus";   
                     auth_basic_user_file conf/htpasswd;   
            }   
            # error_page 404 /404.html;  
 
            # location /404.html {  
                 # root /spool/www;  
                 # charset on;  
                 # source_charset koi8-r;  
            # }  
            # location /old_stuff/ {  
                 # rewrite ^/old_stuff/(.*)$ /new_stuff/$1 permanent;  
            # }  
            #location /download/ {  
                 # valid_referers none blocked server_names *.example.com;  
                 # if ($invalid_referer) {  
                       # #rewrite ^/ http://www.example.com/;  
                       # return 403;  
                  # }  
                  # rewrite_log on;  
                  # # rewrite /download/*/mp3/*.any_ext to /download/*/mp3/*.mp3  
                  # rewrite ^/(download/.*)/mp3/(.*)\..*$ /$1/mp3/$2.mp3 break;  
                  # root /spool/www;  
                  # # autoindex on;  
                  # access_log /var/log/nginx-download.access_log download;  
             # }  
             # location ~* ^.+\.(jpg|jpeg|gif)$ {  
                   # root /spool/www;  
                   # access_log off;  
                   # expires 30d;  
             #}
       }   
}      

備注:conf/htpasswd 文檔的内容用 apache 提供的 htpasswd 工具來産生即可.

檢視 Nginx 運作狀态 輸入位址http://192.168.8.1/NginxStatus/ 。輸入驗證帳号密碼,即可看到類似如下内容:

Active connections: 328   
       server accepts handled requests   
       9309 8982 28890   
       Reading: 1 Writing: 3 Waiting: 324      

第一行表示現在活躍的連接配接數,第三行的第三個數字表示Nginx運作着。

繼續閱讀