于吉吉的技術博客

          建造高性能門戶網

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            65 隨筆 :: 6 文章 :: 149 評論 :: 0 Trackbacks
          nginx的官方網站是:http://www.nginx.org

          Nginx ("engine x") 是一個高性能的 HTTP 和 反向代理 服務器,也是一個 IMAP/POP3/SMTP 代理服務器。 Nginx 是由 Igor Sysoev 為俄羅斯訪問量第二的 Rambler.ru 站點開發的,它已經在該站點運行超過兩年半了。Igor 將源代碼以類BSD許可證的形式發布。盡管還是測試版,但是,Nginx 已經因為它的穩定性、豐富的功能集、示例配置文件和低系統資源的消耗而聞名了。

          nginx的特性
          1.目前官方 Nginx 并不支持Windows,您只能在包括Linux,UNIX,BSD系統下安裝和使用
          2.Nginx 本身只是一個HTTP和反向代理服務器,它無法像Apache一樣通過安裝各種模塊來支持不同的頁面腳本,例如PHP、CGI等
          3.Nginx 支持簡單的負載均衡和容錯
          4.支持作為基本 HTTP 服務器的功能,例如日志、壓縮、Byte ranges、Chunked responses、SSL、虛擬主機等等,應有盡有

          安裝
          安裝nginx前需要確保系統中已經安裝PCRE包,PCRE library這個是HTTP Rewrite模塊,也即是url靜態化的包,到http://www.pcre.org下載最新的PCRE源碼包,

          或直接使用yum install pcre,apt-get instll pcre進行安裝

          登錄 http://www.nginx.org/en/download.html 下載最新的版本

          wget http://www.nginx.org/download/nginx-0.8.53.tar.gz

          tar zxvf nginx-0.8.53.tar.gz

          cd nginx-0.8.53

          ./configure --prefix=/usr/local/nginx --with-pcre=/home/download/pcre-8.01  --with-http_ssl_module --with-openssl=/home/download/openssl-1.0.0a --with-http_stub_status_module 

          ##指定pcre目錄和啟動ssl,https模塊

          make

          make install

          ##為什么Nginx的性能要比Apache高得多?這得益于Nginx使用了最新的epoll(Linux 2.6內核)和kqueue(freebsd)網絡I/O模型,而Apache則使用的是傳統的select模型。目前Linux下能夠承受高并發訪問的 Squid、Memcached都采用的是epoll網絡I/O模型,處理大量的連接的讀寫,Apache所采用的select網絡I/O模型非常低效

          配置
          編輯vi /usr/local/nginx/conf/nginx.conf,其中/usr/local/nginx為安裝路徑

          ##指定nginx的用戶名和用戶組
          user  nobody;

          ##啟動進程數
          worker_processes  8;
          worker_rlimit_nofile 
          10240;

          ##指定PID文件
          pid        logs/nginx.pid;

          ##指定工作模式和鏈接上限
          events {
              
          use epoll;
              worker_connections  
          10240;
          }

          ##http服務器
          http {
          include       mime.types;
          default_type  text
          /html;

          ##指定日志格式
          log_format  main  '$http_x_forwarded_for - $remote_user [$time_local] $request '
                            
          '"$status" $body_bytes_sent "$http_referer" '
                            
          '"$http_user_agent" "$http_x_forwarded_for"';

          ##指定accesslog
          access_log logs/nginx.log main;

          ##指定超時
          keepalive_timeout  300;

          ##開啟gzip模塊
          gzip  on;
          gzip_min_length  
          1000;
          gzip_buffers     
          4 8k;
          gzip_types       text
          /*;
          gzip_http_version 
          1.0;
          output_buffers   
          1 32k;
          postpone_output  
          1460;
          gzip_proxied     any;
          gzip_vary        on;

          ##指定請求的緩沖
          client_header_timeout   5m;
          client_body_timeout     5m;
          send_timeout            5m;
          sendfile                on;
          tcp_nopush              on;
          tcp_nodelay             off;
          client_header_buffer_size 16k;
          large_client_header_buffers 
          4 64k;
          server_names_hash_bucket_size 
          128;
          ssi on;
          ssi_silent_errors on;
          ssi_types text
          /shtml;

          ##指定虛擬主機
          server {
          listen      
          80;
          server_name _;
          server_name_in_redirect  off;

          location 
          / {
           root 
          /dev/null;
          }

          }

          ##指定include文件
          include servers/*.com;

          }


          新建proxy.conf

          proxy_set_header X-Forwarded-For $remote_addr;    ##設定header
          proxy_set_header RealIP $remote_addr;
          proxy_set_header Accept-Encoding ' ';
          proxy_hide_header Vary;    ##隱藏header
          add_header via_up $upstream_addr;
          proxy_connect_timeout   2m;    ##代理連接超時
          proxy_send_timeout      2m;    ##代理發送超時
          proxy_read_timeout      2m;    ##代理發送超時
          proxy_temp_file_write_size 2048m;##設定緩存文件夾大小
          proxy_buffer_size               256k; 
          proxy_buffers                   4 256k; 
          proxy_busy_buffers_size 512k; 

          proxy_ignore_client_abort off
          ;

          proxy_next_upstream error timeout invalid_header
          ;

          新建目錄/conf/servers,并新建配置文件test.com

          server {
                  listen   
          80;
                  server_name  245.test.com;
                  root /home/htmlfile/test;

                  location 
          = / {
                      proxy_temp_path /var/www/cache
          ;
                      index index.html index.htm;
                  }


          }

          修改配置文件后,通過以下命令檢查配置是否正確

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

          the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
          configuration file /usr/local/nginx/conf/nginx.conf test is successful

          啟動命令
          /usr/local/nginx/sbin/nginx 
          停止命令
          /usr/local/nginx/sbin/nginx  -s stop
          平滑重新加載配置文件
          kill -HUP `cat /usr/local/nginx/logs/nginx.pid
          添加到自啟動
          echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

          posted on 2010-11-30 12:45 陳于喆 閱讀(2636) 評論(0)  編輯  收藏 所屬分類: linux 、安裝配置
          主站蜘蛛池模板: 余干县| 衡山县| 襄樊市| 兖州市| 镇沅| 巫溪县| 武定县| 中方县| 城口县| 许昌县| 通州市| 鸡泽县| 温泉县| 镇平县| 如东县| 巴彦县| 高邮市| 策勒县| 蒙阴县| 台东县| 遂昌县| 乌海市| 始兴县| 原平市| 南溪县| 锡林郭勒盟| 阆中市| 刚察县| 深泽县| 杭州市| 涡阳县| 民县| 舒城县| 赣州市| 青冈县| 宁德市| 涿州市| 越西县| 云霄县| 泽州县| 九寨沟县|