小心別讓日子把你給混了

          光榮在于平淡...艱巨在于漫長...
          posts - 26, comments - 7, trackbacks - 0, articles - 1
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          Nginx安裝及配置簡介

          Posted on 2008-12-12 15:40 60 閱讀(68877) 評論(4)  編輯  收藏

          Nginx作為一個后起之秀,他的迷人之處已經讓很多人都投入了他的懷抱。配置簡單,實現原理簡單。做一個負載平衡的再好不過了。

          其原理:

          簡單介紹一下他的安裝及配置過程

          官方網站
          http://wiki.codemongers.com/Main

          一、依賴的程序

          1. gzip module requires zlib library
          2. rewrite module requires pcre library
          3. 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
          重啟:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
          測試配置文件:nginx -t

          簡單吧,安裝,啟動都比較方便。

          四、配置文件
          http://wiki.codemongers.com/NginxFullExample

          #運行用戶
          
           1user  nobody nobody;
           2#啟動進程
           3worker_processes  5;
           4#全局錯誤日志及PID文件
           5error_log  logs/error.log notice;
           6pid        logs/nginx.pid;
           7#工作模式及連接數上限
           8events {
           9#工作模式有:select(標準模式),poll(標準模式),kqueue(高效模式,適用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
          10#epoll(高效模式,本例用的。適用Linux 2.6+,SuSE 8.2,),
             
          #/dev/poll(高效模式,適用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
          11use epoll;
          12worker_connections      1024;
          13}
          14#設定http服務器,利用它的反向代理功能提供負載均衡支持
          15http {
          16#設定mime類型
          17include      conf/mime.types;
          18default_type  application/octet-stream;
          19#設定日志格式
          20log_format main        '$remote_addr - $remote_user [$time_local] '
          21                       '"$request" $status $bytes_sent '
          22                       '"$http_referer" "$http_user_agent" '
          23                       '"$gzip_ratio"';
          24
          25log_format download    '$remote_addr - $remote_user [$time_local] '                       '"$request" $status $bytes_sent '                       '"$http_referer" "$http_user_agent" '                       '"$http_range" "$sent_http_content_range"';
          26#設定請求緩沖
          27client_header_buffer_size    10k;
          28large_client_header_buffers  4 4k;
          29
          30#開啟gzip模塊,要求安裝gzip 在運行./config時要指定
          31gzip on;
          32gzip_min_length  1100;
          33gzip_buffers    4 8k;
          34gzip_types      text/plain;
          35output_buffers  1 32k;
          36postpone_output  1460;
          37#設定訪問日志
          38access_log  logs/access.log  main;
          39client_header_timeout  3m;
          40client_body_timeout    3m;
          41send_timeout          3m;
          42sendfile                on;
          43tcp_nopush              on;
          44tcp_nodelay            on;
          45keepalive_timeout  65;
          46
          47#設定負載均衡的服務器列表
          48upstream backserver {
          49#weigth參數表示權值,權值越高被分配到的幾率越大
          50#本例是指在同一臺服務器,多臺服務器改變ip即可
          51server 127.0.0.1:8081 weight=5;
          52server 127.0.0.1:8082;
          53server 127.0.0.1:8083;
          54}
             #Deny access to any host other than (www).4535.com
             server {
                 server_name  _;  #default
                 return 404;
             }
             
          55#設定虛擬主機,默認為監聽80端口,改成其他端口會出現問題
          56server {
          57listen         80;
          58server_name    test.com www.test.com;
          59charset utf8;
          60#設定本虛擬主機的訪問日志
          61access_log  logs/test.com.log  main;
          62#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉發。但如果文件較多效果不是太好。
          63location ~ ^/(images|js|css)/  {
          64root    /usr/local/testweb;
          65expires 30m;
          66}
          67#對 "/" 啟用負載均衡
          68location / {
          69proxy_pass      http://backserver;
          70proxy_redirect          off;
          71proxy_set_header        Host $host;
          72proxy_set_header        X-Real-IP $remote_addr;
          73proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          74client_max_body_size    10m;
          75client_body_buffer_size 128k;
          76proxy_connect_timeout  90;
          77proxy_send_timeout      90;
          78proxy_read_timeout      90;
          79proxy_buffer_size      4k;
          80proxy_buffers          4 32k;
          81proxy_busy_buffers_size 64k;
          82proxy_temp_file_write_size 64k;}
          83#設定查看Nginx狀態的地址,在運行./config 要指定,默認是不安裝的。
          84location /NginxStatus {
          85stub_status            on;
          86access_log             on;
          87auth_basic             "NginxStatus";
          88#是否要通過用戶名和密碼訪問,測試時可以不加上。conf/htpasswd 文件的內容用 apache 提供的 htpasswd 工具來產生即可#auth_basic_user_file  conf/htpasswd;
          89}
          90}
          91

          生活是用腳一步一步走出來的.....


          評論

          # re: Nginx安裝及配置簡介  回復  更多評論   

          2009-09-30 15:51 by 智能手機中文網
          還是有點問題

          # nginx  回復  更多評論   

          2011-05-30 02:09 by J控 奔途
          看了半天類似的資料,愣是沒找到解決問題的辦法,不過,后來找了個視頻看了會才讓我想到解決辦法. 我的問題出在fastcgi沒有啟動. 我找到的資料里面大多都沒有提及這一點,fastcgi如何啟動,都只說了怎么怎么裝完就行~ 其實很簡單,首先找到php的安裝目錄 比如/usr/local/php,然后執行"/usr/local/php/bin/php-cgi -b 9000"這樣就能在所有IP上偵聽9000端口(nginx里fastcgi默認端口)

          # re: Nginx安裝及配置簡介  回復  更多評論   

          2014-02-12 16:33 by woshineyy
          謝謝,樓主很詳細,如果大家要看在線視頻 nginx的教程視頻 在www.ppst.cc 上有好多,請關注,也可以通過錄視頻 來獲取收益

          # re: Nginx安裝及配置簡介  回復  更多評論   

          2014-12-06 16:50 by hu
          sdfsdfs

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 林州市| 宜良县| 顺平县| 云林县| 南华县| 博白县| 大同市| 江都市| 泗水县| 习水县| 双辽市| 永吉县| 中西区| 腾冲县| 金秀| 宜章县| 金乡县| 孙吴县| 常州市| 科技| 汽车| 灵璧县| 新河县| 珠海市| 惠安县| 轮台县| 铜山县| 武山县| 亳州市| 茂名市| 平江县| 永济市| 樟树市| 河源市| 常宁市| 兴山县| 新泰市| 海林市| 饶河县| 泰州市| 洛川县|