小心別讓日子把你給混了

          光榮在于平淡...艱巨在于漫長(zhǎng)...
          posts - 26, comments - 7, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          Nginx安裝及配置簡(jiǎn)介

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

          Nginx作為一個(gè)后起之秀,他的迷人之處已經(jīng)讓很多人都投入了他的懷抱。配置簡(jiǎn)單,實(shí)現(xiàn)原理簡(jiǎn)單。做一個(gè)負(fù)載平衡的再好不過了。

          其原理:

          簡(jiǎn)單介紹一下他的安裝及配置過程

          官方網(wǎng)站
          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

          默認(rèn)安裝的路徑是/usr/local/nginx

          更多的安裝配置
          ./configure --prefix=/usr/local/nginx
          --with-openssl=/usr/include (啟用ssl)
          --with-pcre=/usr/include/pcre/ (啟用正規(guī)表達(dá)式)
          --with-http_stub_status_module (安裝可以查看nginx狀態(tài)的程序)
          --with-http_memcached_module (啟用memcache緩存)
          --with-http_rewrite_module (啟用支持url重寫)


          三、啟動(dòng)及重啟
          啟動(dòng):nginx
          重啟:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
          測(cè)試配置文件:nginx -t

          簡(jiǎn)單吧,安裝,啟動(dòng)都比較方便。

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

          #運(yùn)行用戶
          
           1user  nobody nobody;
           2#啟動(dòng)進(jìn)程
           3worker_processes  5;
           4#全局錯(cuò)誤日志及PID文件
           5error_log  logs/error.log notice;
           6pid        logs/nginx.pid;
           7#工作模式及連接數(shù)上限
           8events {
           9#工作模式有:select(標(biāo)準(zhǔn)模式),poll(標(biāo)準(zhǔn)模式),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#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
          15http {
          16#設(shè)定mime類型
          17include      conf/mime.types;
          18default_type  application/octet-stream;
          19#設(shè)定日志格式
          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#設(shè)定請(qǐng)求緩沖
          27client_header_buffer_size    10k;
          28large_client_header_buffers  4 4k;
          29
          30#開啟gzip模塊,要求安裝gzip 在運(yùn)行./config時(shí)要指定
          31gzip on;
          32gzip_min_length  1100;
          33gzip_buffers    4 8k;
          34gzip_types      text/plain;
          35output_buffers  1 32k;
          36postpone_output  1460;
          37#設(shè)定訪問日志
          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#設(shè)定負(fù)載均衡的服務(wù)器列表
          48upstream backserver {
          49#weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大
          50#本例是指在同一臺(tái)服務(wù)器,多臺(tái)服務(wù)器改變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#設(shè)定虛擬主機(jī),默認(rèn)為監(jiān)聽80端口,改成其他端口會(huì)出現(xiàn)問題
          56server {
          57listen         80;
          58server_name    test.com www.test.com;
          59charset utf8;
          60#設(shè)定本虛擬主機(jī)的訪問日志
          61access_log  logs/test.com.log  main;
          62#如果訪問 /images/*, /js/*, /css/* 資源,則直接取本地文件,不用轉(zhuǎn)發(fā)。但如果文件較多效果不是太好。
          63location ~ ^/(images|js|css)/  {
          64root    /usr/local/testweb;
          65expires 30m;
          66}
          67#對(duì) "/" 啟用負(fù)載均衡
          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#設(shè)定查看Nginx狀態(tài)的地址,在運(yùn)行./config 要指定,默認(rèn)是不安裝的。
          84location /NginxStatus {
          85stub_status            on;
          86access_log             on;
          87auth_basic             "NginxStatus";
          88#是否要通過用戶名和密碼訪問,測(cè)試時(shí)可以不加上。conf/htpasswd 文件的內(nèi)容用 apache 提供的 htpasswd 工具來產(chǎn)生即可#auth_basic_user_file  conf/htpasswd;
          89}
          90}
          91

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


          評(píng)論

          # re: Nginx安裝及配置簡(jiǎn)介  回復(fù)  更多評(píng)論   

          2009-09-30 15:51 by 智能手機(jī)中文網(wǎng)
          還是有點(diǎn)問題

          # nginx  回復(fù)  更多評(píng)論   

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

          # re: Nginx安裝及配置簡(jiǎn)介  回復(fù)  更多評(píng)論   

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

          # re: Nginx安裝及配置簡(jiǎn)介  回復(fù)  更多評(píng)論   

          2014-12-06 16:50 by hu
          sdfsdfs

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 越西县| 定结县| 淮滨县| 高雄市| 大兴区| 永州市| 鹿泉市| 岑溪市| 阿拉善右旗| 建阳市| 通许县| 武胜县| 马边| 沂源县| 瑞昌市| 儋州市| 镇巴县| 澄江县| 萨嘎县| 马尔康县| 呼图壁县| 玉田县| 留坝县| 崇仁县| 政和县| 海晏县| 贺兰县| 毕节市| 连云港市| 深水埗区| 辽阳市| 建始县| 秀山| 巴林左旗| 资中县| 泸定县| 家居| 新巴尔虎左旗| 伊川县| 鹿邑县| 清流县|