LNMP安裝與配置

Nginx與apache、lighttp性能綜合對比,以下圖:javascript

                       

 

注意:關閉rpm默認安裝的apachemysqlphp

1.準備php函數的rpmcss

yum -y install gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel openldap openldap-devel nss_ldap openldap-clients openldap-servershtml

 

2.準備lnmp其餘的源代碼包java

wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.46.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.14.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.14-fpm-0.5.14.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.5.3-m3.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.1.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/libmcrypt-2.5.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/mcrypt/mcrypt-2.6.8.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/memcache/memcache-2.2.5.tgz
wget http://blog.s135.com/soft/linux/nginx_php/mhash/mhash-0.9.9.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/pcre/pcre-8.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.6.1.tar.bz2
wget http://blog.s135.com/soft/linux/nginx_php/pdo/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/imagick-2.3.0.tgznode

 

3.安裝php-5.2.14源代碼包所須要的函數支持包mysql

tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../linux

(libiconv庫爲須要作轉換的應用提供了一個iconv()的函數,以實現一個字符編碼到另外一個字符編碼的轉換)

tar zxvf libmcrypt-2.5.8.tar.gz 
cd libmcrypt-2.5.8/
./configure
make
make install
cd libltdl/
./configure --enable-ltdl-install
make
make install
cd ../../nginx

(libmcrypt是加密算法擴展庫。支持DES, 3DES, RIJNDAEL, Twofish, IDEA, GOST, CAST-256, ARCFOUR, SERPENT, SAFER+等算法。)c++



tar zxvf mhash-0.9.9.9.tar.gz
cd mhash-0.9.9.9/
./configure
make
make install
cd ../

(加密算法支持)

ln -s /usr/local/lib/libmcrypt.la /usr/lib/libmcrypt.la
ln -s /usr/local/lib/libmcrypt.so /usr/lib/libmcrypt.so
ln -s /usr/local/lib/libmcrypt.so.4 /usr/lib/libmcrypt.so.4
ln -s /usr/local/lib/libmcrypt.so.4.4.8 /usr/lib/libmcrypt.so.4.4.8
ln -s /usr/local/lib/libmhash.a /usr/lib/libmhash.a
ln -s /usr/local/lib/libmhash.la /usr/lib/libmhash.la
ln -s /usr/local/lib/libmhash.so /usr/lib/libmhash.so
ln -s /usr/local/lib/libmhash.so.2 /usr/lib/libmhash.so.2
ln -s /usr/local/lib/libmhash.so.2.0.1 /usr/lib/libmhash.so.2.0.1
ln -s /usr/local/bin/libmcrypt-config /usr/bin/libmcrypt-config

tar zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/

./configure
make
make install
cd ../

 

4. 編譯安裝MySQL 5.5.3-m3

groupadd mysql

useradd -g mysql mysql

 

tar zxvf mysql-5.5.3-m3.tar.gz

cd mysql-5.5.3-m3

./configure --prefix=/usr/local/mysql --without-debug --enable-thread-safe-client --with-pthread --enable-assembler --enable-profiling --with-mysqld-ldflags=-all-static --with-client-ldflags=-all-static --with-extra-charsets=all --with-plugins=all --with-mysqld-user=mysql --without-embedded-server --with-server-suffix=-community --with-unix-socket-path=/tmp/mysql.sock

Make

#編譯

Make install

#安裝

Cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf

#準備mysql配置文件

Vi /etc/my.cnf

[client]

default-character-set=utf8

#修改客戶端和鏈接字符集

[mysqld]

character-set-server=utf8

#修改服務器和數據庫字符集

collation-server = utf8_general_ci

#修改服務器校驗字符集                   登錄mysql後能夠\s查看字符集

 

Setfacl -m u:mysql:rwx -R /usr/local/mysql

Setfacl -m d:u:mysql:rwx -R /usr/local/mysql

#設置權限

/usr/local/mysql/bin/mysql_install_db --user=mysql

#安裝mysql和test數據庫

/usr/local/mysql/bin/mysqld_safe --user=mysql &

#啓動mysql服務

/usr/local/mysql/bin/mysqladmin -uroot password  123

#修改mysql登陸密碼爲123

/usr/local/mysql/bin/mysql -uroot -p123

#用mysql登陸

 

5. 編譯安裝PHP(FastCGI模式。使用fastCGI管理php,加快php解析速度)

tar zxvf php-5.2.14.tar.gz
gzip -cd php-5.2.14-fpm-0.5.14.diff.gz | patch -d php-5.2.14 -p1

#-p  1    是數字
#解壓並打補丁,讓php支持fpm來方便管理php-cgi進程(使用php-fpm管理fastCGI)

# gzip   -c  保留源文件   -d  解壓

cd php-5.2.14/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-mysqli=/usr/local/mysql/bin/mysql_config --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-discard-path --enable-safe-mode --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fastcgi --enable-fpm --enable-force-cgi-redirect --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap
make ZEND_EXTRA_LIBS='-liconv'
#編譯過程設定變量(編譯過程須要)
make install
cp /lnmp/php-5.2.14/php.ini-dist  /usr/local/php/etc/php.ini
cd ../

 

6.準備編譯安裝PHP5擴展模塊

tar zxvf memcache-2.2.5.tgz
cd memcache-2.2.5/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

tar jxvf eaccelerator-0.9.6.1.tar.bz2
cd eaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure --enable-eaccelerator=shared --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

(eAccelerator是一個自由開放源碼php加速器,優化和動態內容緩存,提升了php腳本的緩存性能,使得PHP腳本在編譯的狀態下,對服務器的開銷幾乎徹底消除。 它還有對腳本起優化做用,以加快其執行效率。使您的PHP程序代碼執效率能提升1-10倍)

tar zxvf PDO_MYSQL-1.0.2.tgz
cd PDO_MYSQL-1.0.2/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-pdo-mysql=/usr/local/mysql
make
make install
cd ../

tar zxvf ImageMagick.tar.gz
cd ImageMagick-6.5.1-2/
./configure
make
make install
cd ../

(ImageMagick是一套功能強大、穩定並且免費的工具集和開發包,能夠用來讀、寫和處理超過89種基本格式的圖片文件,包括流行的TIFF、JPEG、GIF、 PNG、PDF以及PhotoCD等格式)

tar zxvf imagick-2.3.0.tgz
cd imagick-2.3.0/
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make
make install
cd ../

 

7. 修改php.ini文件,讓php模塊生效

vi /usr/local/php/etc/php.ini

extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"

 

手工添加

extension = "memcache.so"
extension = "pdo_mysql.so"
extension = "imagick.so"

再查找output_buffering = Off

修改成output_buffering = On

再查找  ; cgi.fix_pathinfo=0

修改成cgi.fix_pathinfo=0,防止Nginx文件類型錯誤解析漏洞

 

8. 在php.ini中配置eAccelerator加速PHP

 

 

mkdir -p /usr/local/eaccelerator_cache

#準備eaccelerator緩存目錄

 

vi /usr/local/php/etc/php.ini

[eaccelerator]
zend_extension="/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so"
eaccelerator.shm_size="64"
eaccelerator.cache_dir="/usr/local/eaccelerator_cache"
eaccelerator.enable="1"
eaccelerator.optimizer="1"
eaccelerator.check_mtime="1"
eaccelerator.debug="0"
eaccelerator.filter=""
eaccelerator.shm_max="0"
eaccelerator.shm_ttl="3600"
eaccelerator.shm_prune_period="3600"
eaccelerator.shm_only="0"
eaccelerator.compress="1"
eaccelerator.compress_level="9"

 

9.準備php-cgi和nginx進程執行者用戶

useradd nginx

 

10. 建立php-fpm配置文件- php-fpm.conf

vi /usr/local/php/etc/php-fpm.conf

<value name="display_errors">0</value>

#0改爲1,頁面上會輸出錯誤日誌.   取消註釋

unix user of processes
      <value name="user">nginx</value>

      Unix group of processes
      <value name="group">nginx</value>                        取消註釋

 <value name="max_children">128</value>
#最大子進程數128,若是內存小於2G,則64個最佳

<value name="rlimit_files">65535</value>
# Set open file desc rlimit,同時打開的文件數,linux系統容許同時打開的文件數爲1024,修改linux系統中容許同時打開的文件,ulimit -SHn 65535,並且這個參數重啓後還能生效,加到 /etc/profile全局配置文件的最後,開機就會生效,ulimit -a查看open files 65535

       ulimit  用戶控制shell啓動進程所佔用的資源

              -H   設定硬性資源限制,也就是管理員設定的限制

              -S    設定軟性資源限制,彈性限制

              -n    設定可同時打開的最大文件個數

              -f     設定單個文件最大大小

              -a    查看目前的限制

<value name="max_requests">1024</value>
#最大請求數, How much requests each process should execute before respawn.一個子進程可以迴應1042個請求

 

11. 啓動php-cgi(fastcgi)進程,監聽127.0.0.1的9000端口,進程數爲128(若是服務器內存小於3GB,能夠只開啓64個進程),用戶爲nginx

/usr/local/php/sbin/php-fpm start

#啓動php-cgi

/usr/local/php/sbin/php-fpm reload

#從新加載配置文件

/usr/local/php/sbin/php-fpm stop

#關閉php-fpm,此時nginx確定連不上php

 

12. 安裝Nginx所需的pcre

tar zxvf pcre-8.10.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

 

13. 安裝Nginx

tar zxvf nginx-0.8.46.tar.gz
cd nginx-0.8.46/
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make && make install
cd ../

 

14. 修改Nginx配置文件

vi /usr/local/nginx/conf/nginx.conf

user  nginx nginx;

worker_processes 1;

#至關於cpu個數

error_log  logs/nginx_error.log;

#錯誤日誌

pid        /usr/local/nginx/nginx.pid;

#主進程PID保存文件

#Specifies the value for maximum file descriptors that can be opened by this process. 
worker_rlimit_nofile 65535;

#文件描述符數量

events 
{
  use epoll;

#網絡I/O模型,建議linux使用epoll,FreeBSD使用kqueue
  worker_connections 65535;

#最大容許鏈接數
}

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

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

              '$status $body_bytes_sent "$http_referer" '

              '"$http_user_agent" "$http_x_forwarded_for"';

#日誌格式

access_log  logs/access.log  main;

#調用格式的日誌
      
  sendfile on;
  tcp_nopush     on;

#tcp延遲

  keepalive_timeout 65;

#保持鏈接時間


  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

#fastcgi設置

  gzip on;
  gzip_min_length  1k;
  gzip_buffers     4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2;
  gzip_types       text/plain application/x-javascript text/css application/xml;
  gzip_vary on;

#網絡壓縮設置

  #limit_zone  crawler  $binary_remote_addr  10m;

  server
  {
    listen       80;

#監聽端口
    server_name  192.168.150.253;

#主機名,或IP。若是是主機名,要可以DNS解析

 

location / {

root  html;

#網站主目錄。/usr/local/nginx/html/
index index.html index.htm index.php;

#默認網頁順序

}
 

    #limit_conn   crawler  20;    
                             
    location ~ .*\.(php|php5)?$

#~:匹配       後面正則表達式:.*任意字符  \.點 php或php5結尾。碰到網頁文

件名是.php或.php5結尾
    {     

root  html; 
      #fastcgi_pass  unix:/tmp/php-cgi.sock;
      fastcgi_pass  127.0.0.1:9000;

#鏈接fastcgi,用來解析php語句
      fastcgi_index index.php;

#首頁爲index.php

   #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

#啓動fast-cgi,能夠在每一個服務中啓動,也能夠放入/usr/local/nginx/conf/fastcgi_params,每一個server均可以享用
      include fastcgi_params;

#包括fastcgi_params中參數
    }
    
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
      expires      30d;

#圖片格式緩存30天
    }

    location ~ .*\.(js|css)?$
    {
      expires      1h;

#js/css緩存2小時
    }    

    log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '
              '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" $http_x_forwarded_for';
    access_log  /data1/logs/access.log  access;
      }
}

 

15. 在/usr/local/nginx/conf/目錄中建立fastcgi_params文件

Vi  /usr/local/nginx/conf/fastcgi_params         (與配置文件中,只寫一個就好)

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;

 

#建議把fastcgi_param寫到nginx.conf中而不是把它寫到fastcgi_params配置文件中,不然配置不夠靈活,好比後面默認php設置和alias php設置中,他們的php頁面的系統地址是不一樣的,好比:

默認php文件->/usr/local/nginx/html/index.php

Alias php文件->/mnt/bbs/index.php

這個時候你會發現fastcgi_params中的SCRIPT_FILENAME的值是相同的,這樣會致使alias php的頁面出不來,而配置在nginx.conf中各自配置各自的php系統地址,這樣比較靈活.

 

#若是你以爲每一個鏈接php的配置中都要加這一句話有點冗餘,那就把它加入到fastcgi_params文件中,這樣只須要加一次,其餘全部的nginx.conf中的有關鏈接fastcgi的一塊就不用加fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name這一句話了.

16.配置開機啓動nginx,php-fpm,ulimit

1)nginx

Vi /etc/rc.local

/usr/local/nginx/sbin/nginx

2)php-fpm

Vi /etc/rc.local

/usr/local/php/sbin/php-fpm start

3)ulimit

Vi /etc/profile

ulimit -SHn 65535

4)mysql

Vi /etc/rc.local

/usr/local/mysql/bin/mysqld_safe  --user=mysql  &

 

17.檢查nginx配置文件語句錯誤

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

 

18.平滑重啓nginx進程

1)Pkill -HUP nginx

2)kill -HUP `pgrep -uroot nginx`

   Pgrep  -uroot  nginx  取出nginx主進程PID

3)/usr/local/nginx/sbin/nginx -s reload

 

19. 編寫天天定時切割Nginx日誌的腳本
一、建立腳本/usr/local/nginx/sbin/cut_nginx_log.sh

vi /usr/local/nginx/sbin/cut_nginx_log.sh

 

#!/bin/bash
# This script run at 00:00

# The Nginx logs path
logs_path="/usr/local/nginx/logs/"

mkdir -p ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/
mv ${logs_path}access.log ${logs_path}$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m")/access_$(date -d "yesterday" +"%Y%m%d").log
kill -USR1 `cat /usr/local/nginx/nginx.pid`



二、設置crontab,天天凌晨00:00切割nginx訪問日誌

crontab -e

00 00 * * * /bin/bash  /usr/local/nginx/sbin/cut_nginx_log.sh


20.配置nginx虛擬主機

 

Sina和sohu域名事先解析

 

Vi /usr/local/nginx/conf/nginx.conf

==èwww.sina.com公司網站

server {

        listen       80;

        server_name  www.sina.com;

        access_log  logs/sina.access.log  main;

        location / {

            root   /web/sina;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root           /web/sina;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

    }

==èwww.sohu.com公司網站

 

server {                              

        listen       80;

        server_name  www.sohu.com;

        access_log  logs/sohu.access.log  main;

        location / {

            root   /web/sohu;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root           /web/sohu;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

        #   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

 

最後在客戶端測試虛擬主機www.baidu.com和www.sina.com兩家公司網站

 

21.列表頁顯示

location / {

            autoindex on;           #打開列表頁

            root   html;

            index  index.html index.php index.htm;

         }

 

22.虛擬目錄設置

location /bbs{

                alias /mnt/bbs/;

        }

#這樣配置html靜態文件是能夠出來的,可是php動態頁面出不來,並且會瀏覽器的頁面上會顯示" No input file specified. "的報錯,實際上是php系統文件地址( SCRIPT_FILENAME)找不到,也就是說fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;中的$document_root$fastcgi_script_name不是真正的/mnt/bbs/index.php的地址,這可怎麼解決:

location /bbs {

            alias /mnt/bbs/;

            index bbs.php index.html index.php;

        }

        location ~ ^/bbs/ {

            root /mnt/;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

                     fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

            log_format  bbs  '$document_root$fastcgi_script_name ';

            access_log logs/bbs.access.log bbs;

        }

#後面兩行是關於日誌的,就是爲了更好的觀察由nginx提交給fastcgi的php的系統地址SCRIPT_FILENAME,在這裏我用$request_filename來給SCRIPT_FILENAME賦值,在日誌中的結果爲/mnt/bbs/index.php,在這裏我發現一個問題就是$request_filename中的root設置爲/mnt,不然$request_filename的值爲:/mnt/bbs/bbs/index.php.

 

由以上能夠獲得一個結論,就是默認php設置也能夠這樣設置關於SCRIPT_FILENAME:

location ~ \.php$ {

            root           html;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

            log_format  php  '$document_root$fastcgi_script_name ';

            access_log logs/php.access.log php;

        }

#此時從日誌中能夠看到,$request_filename的值爲/usr/local/nginx/html/index.php,而之前默認的/scripts$fastcgi_script_name顯然是錯的php系統地址,日誌中顯示爲/scripts/index.php

23.nginx狀態監控

location /nginxstatus{

        stub_status on;

        access_log  off;

        }

#客戶端訪問網址:http://www.baidu.com/nginxstatus

 

24.rewrite正則過濾

location ~ \.php$ {

        proxy_pass   http://127.0.0.1;

        }

Rewrite指令最後一項參數爲flag標記,支持的flag標記以下:

Last 標示完成rewrite規則

Break 再也不匹配後面的規則

Redirect 302臨時重定向

Permanent 301永久重定向

Last和break用來實現uri重寫,瀏覽器地址欄的url地址不變,但在服務器訪問的路徑發生了變化,redirect和permanent用來實現url跳轉,瀏覽器地址欄會顯示跳轉後的url地址,使用alias指令時必須使用last標記,使用proxy_pass指令時要使用break標記,last標記在本條rewrite規則執行完畢後,會對其所在的server{}標籤從新發起請求,而break標記則在本條規則匹配完成後,終止匹配,再也不匹配後面的規則.

 

在匹配的過程當中,nginx將首先匹配字符串,而後再匹配正則表達式,匹配到第一個正則表達式後,會中止搜索,若是匹配到正則表達式,則使用正則表達式的搜索結果,若是沒有匹配到正則表達式,則使用字符串的搜索結果.

可使用前綴"^~"來禁止匹配到字符串後,再去檢查正則表達式,匹配到url後,將中止查詢.

使用前綴"="能夠進行精確的url匹配,若是找到匹配的uri,則中止查詢,例如"location=/",只能匹配到"/",而"/test.html"則不能被匹配.

正則表達式的匹配,按照它們在配置文件中的順序進行,寫在前面的優先.

Location = / {

       #僅僅匹配 /

    [configuration A]

}

Location / {

       #匹配任何以/開頭的查詢,可是正則表達式及較長的字符串(/bbs/)將被優先匹配.

    [configuration B]

}

Location ^~ /images/ {
       #匹配任何以/images/開頭的字符串,而且中止搜索,因此正則表達式將不會被檢查.

    [configuration C]

}

Location ~* \.(gif|jpg|jpeg)$ {

       #匹配以.gif、.jpg、.jpeg結尾的任何請求,可是,/images/內的請求將使用configuration c的配置

       [configuratoin D]

}

請求處理匹配結果示例:

/ -> configuration A;

/documents/document.html -> configuration B;

/images/1.gif -> configuration c;

/documents/1.jpg -> configuration D;

 

例1:域名跳轉

輸入www.sina.com,跳轉到www.sohu.com

    server {

        listen       80;

        server_name  www.sina.com;

        access_log  logs/sina.access.log  main;

        location / {

            root   /web/sina;

            index  index.html index.htm;

                     if (-e $request_filename){

                            # -e   是否存在

            rewrite  ^/  http://www.sohu.com/  permanent;

                            # ^/  域名以/開頭。//www.sina.com    ,也能夠寫爲.* 任意都跳轉

                     }

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

}

server {

        listen       80;

        server_name  www.sohu.com;

        access_log  logs/sohu.access.log  main;

        location / {

            root   /web/sohu;

            index  index.html index.htm;

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        }

 

例2:靜態index.html跳轉到動態index.php文件

cd  /web/sina/

vi  index.php

       <?

        print_r ($_GET);

?>

       Vi  nginx.conf

           server {

        listen       80;

        server_name  www.sina.com;

        access_log  logs/sina.access.log  main;

        location / {

            root   /web/sina;

            index  index.html index.htm;

            rewrite  ^/index(\d+).html  /index.php?id=$1  last;

# ^/ 以/開頭。\d+ 多個數字。()第一個變量。     /index.php?id=$1 把第一個變量賦予id變量,傳入index.php文件

        }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            root   html;

        }

        location ~ \.php$ {

            root           /web/sina;

            fastcgi_pass   127.0.0.1:9000;

            fastcgi_index  index.php;

            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

        }

}

 

在客戶端輸入:http://www.sina.com/index2.html

                     會跳轉到index.php,把2當變量傳入index.php程序

 

25.代理負載均衡技術(反向代理)

upstream myweb1 {

       #定義地址池

 

         
   
   

192.168.190.190

   
   
         
   
   

反向代理

   

upstream myweb1 {

   

            server 192.168.190.190:80;

   

            server 192.168.190.191:80;

   

}

   

 

   

        server {

   

            listen       80;

   

            server_name  www.sohu.com;

   

            location / {

   

                proxy_pass http://myweb1;

   

                proxy_set_header Host     $host;

   

                proxy_set_header     X-Forwarded-For $remote_addr;

   

            }

   

        }

   
   

        server 192.168.244.10:80;

 

        server 192.168.244.11:80;

}

    server {

        listen       80;

        server_name  www.sohu.com;

 

         
   
   

192.168.190.191

   
   

                            #使用www.sohu.com訪問

 

location / {

proxy_pass http://myweb1;

              #使用地址池

proxy_set_header Host $host;

              #利用HOST變量向後端服務器傳遞須要解析的客戶端訪問的域名(傳遞域名)

proxy_set_header X-Forwarded-For $remote_addr;

              #$remote_addr 把客戶端真實IP賦予X-Forwarded-For。後端服務器才能獲取真實的客戶端IP。以便記錄日誌,要不日誌中記錄的訪問信息都是負載服務器,而不是客戶端(傳遞IP)

}

}

 

 

26.模塊設置

Error_log

#錯誤日誌

Include

#包含子配置文件,0.6版本之後子配置文件放在nginx.conf所在的路徑下

Pid

#主進程id號

User

#nginx nginx代表nginx進程的執行者和組

Worker_processes

#與cpu個數相同,4核cpu爲4

Worker_rlimit_nofile 65535

#打開的文件描述符,不過提早得設置ulimit -SHn 65535,即linux容許的打開文件個數

Worker_connectiones 65535

#客戶端最大鏈接數65535

Alias

#虛擬目錄

Error_page

#404,500錯誤跳轉頁面

Index

#index index.html,設置默認首頁

Keepalive_timeout

#即tcp持續鏈接超時時間

Limit_rate

#limit _rate 100k,即限速爲100KB/s

Limit_rate_after

#limit_rate_after 1m,即下載文件超過1m,則進入limit_rate限速階段

Listen

#listen 192.168.100.1:80,即設置ip和端口

Location

#該指令容許對不一樣的uri進行不一樣的配置,能夠是字符串、正則表達式

Resolver

#resolver 8.8.8.8,爲nginx設置dns域名指向

Root

#設置網站根目錄

Send_timeout

#超時時間是指進行了兩次tcp握手,尚未轉爲established狀態的時間,若是超過這個時間,客戶沒有響應,nginx則關閉鏈接,能夠用來防止ddos攻擊

Sendfile

#啓用或禁用sendfile()函數,做用於拷貝兩個文件描述符之間的操做函數,這個拷貝是在內核中操做的,比read和write拷貝高效得多

Server

#普通web配置或虛擬主機的配置的區域

Server_name

#根據客戶端請求header頭信息中的host域名,來匹配該請求應該由哪一個虛擬主機配置或服務器的ip

Tcp_nodelay

#封裝tcp/ip數據包的等待時間,也叫納格算法,在keepalive開啓纔有用

Tcp_nopush

#要求sendfile開啓的時候才起做用,設置該選擇的緣由是nginx在linux上,試圖在一個包中發送它的httpd應答頭

Allow

#allow 192.168.100.254,只容許192.168.100.254訪問

Deny

#deny all,拒絕其餘任何人訪問

Autoindex

#autoindex on,即開啓列表頁功能

Charset

#charset utf8;source_charset gbk,把服務器上的gbk網頁編碼轉換成utf8輸出給客戶端

Fastcgi_pass

#fastcgi_pass   127.0.0.1:9000;

#fastcgi_index  index.php;

#fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;

#include        fastcgi_params;

#fastcgi_pass後跟的是php-cgi進程的ip和端口

Access_log

#正確日誌

Proxy_pass

# proxy_pass http://myweb1,即後跟的是nginx代理負載池upstream中的服務器

Proxy_set_header

# proxy_set_header Host $host,設置把$host帶給後端的nginx服務器

Proxy_temp_path

#用戶指定一個本地目錄緩衝較大的代理請求,相似於client_body_temp_path

Stub_status

# stub_status on,即開戶狀態監控

Image_filter

#它指定適用於圖片的轉換類型

相關文章
相關標籤/搜索