最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

linux服務(wù)器下LNMP安裝與配置方法

 更新時間:2013年07月08日 15:48:33   投稿:mdxy-dxy  
現(xiàn)在很多朋友都選擇了linux服務(wù)器下配置LNMP(linux+nginx+mysql+php),這里分享下LNMP安裝與配置方法,需要的朋友可以參考下

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


 

注意:關(guān)閉rpm默認安裝的apache和mysql

1.準備php函數(shù)的rpm包

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-servers
 
2.準備lnmp其他的源代碼包

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.tgz
 
3.安裝php-5.2.14源代碼包所需要的函數(shù)支持包

復制代碼 代碼如下:

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

(libiconv庫為需要做轉(zhuǎn)換的應(yīng)用提供了一個iconv()的函數(shù),以實現(xiàn)一個字符編碼到另一個字符編碼的轉(zhuǎn)換)

復制代碼 代碼如下:

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 ../../

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

復制代碼 代碼如下:

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
#修改服務(wù)器和數(shù)據(jù)庫字符集
collation-server = utf8_general_ci
#修改服務(wù)器校驗字符集                   登陸mysql后可以\s查看字符集
 
Setfacl -m u:mysql:rwx -R /usr/local/mysql
Setfacl -m d:u:mysql:rwx -R /usr/local/mysql
#設(shè)置權(quán)限
/usr/local/mysql/bin/mysql_install_db --user=mysql
#安裝mysql和test數(shù)據(jù)庫
/usr/local/mysql/bin/mysqld_safe --user=mysql &
#啟動mysql服務(wù)
/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    是數(shù)字
#解壓并打補丁,讓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'
#編譯過程設(shè)定變量(編譯過程需要)
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加速器,優(yōu)化和動態(tài)內(nèi)容緩存,提高了php腳本的緩存性能,使得PHP腳本在編譯的狀態(tài)下,對服務(wù)器的開銷幾乎完全消除。 它還有對腳本起優(yōu)化作用,以加快其執(zhí)行效率。使您的PHP程序代碼執(zhí)效率能提高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是一套功能強大、穩(wěn)定而且免費的工具集和開發(fā)包,可以用來讀、寫和處理超過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進程執(zhí)行者用戶

useradd nginx
 
10. 創(chuàng)建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>
#最大子進程數(shù)128,如果內(nèi)存小于2G,則64個最佳
<value name="rlimit_files">65535</value>
# Set open file desc rlimit,同時打開的文件數(shù),linux系統(tǒng)允許同時打開的文件數(shù)為1024,修改linux系統(tǒng)中允許同時打開的文件,ulimit -SHn 65535,而且這個參數(shù)重啟后還能生效,加到 /etc/profile全局配置文件的最后,開機就會生效,ulimit -a查看open files 65535
       ulimit  用戶控制shell啟動進程所占用的資源
              -H   設(shè)定硬性資源限制,也就是管理員設(shè)定的限制
              -S    設(shè)定軟性資源限制,彈性限制
              -n    設(shè)定可同時打開的最大文件個數(shù)
              -f     設(shè)定單個文件最大大小
              -a    查看目前的限制
<value name="max_requests">1024</value>
#最大請求數(shù), How much requests each process should execute before respawn.一個子進程能夠回應(yīng)1042個請求
 
11. 啟動php-cgi(fastcgi)進程,127.0.0.1的9000端口,進程數(shù)為128(如果服務(wù)器內(nèi)存小于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
#關(guān)閉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個數(shù)
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;
#文件描述符數(shù)量
events
{
  use epoll;
#網(wǎng)絡(luò)I/O模型,建議linux使用epoll,F(xiàn)reeBSD使用kqueue
  worker_connections 65535;
#最大允許連接數(shù)
}
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;
#調(diào)用格式的日志
     
  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設(shè)置
  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;
#網(wǎng)絡(luò)壓縮設(shè)置
  #limit_zone  crawler  $binary_remote_addr  10m;
  server
  {
    listen       80;
    server_name  192.168.150.253;
#主機名,或IP。如果是主機名,要能夠DNS解析
 
location / {
root  html;
#網(wǎng)站主目錄。/usr/local/nginx/html/
index index.html index.htm index.php;
#默認網(wǎng)頁順序
}
 
    #limit_conn   crawler  20;   
                            
    location ~ .*\.(php|php5)?$
#~:匹配       后面正則表達式:.*任意字符  \.點 php或php5結(jié)尾。碰到網(wǎng)頁文
件名是.php或.php5結(jié)尾
    {    
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,可以在每個服務(wù)中啟動,也可以放入/usr/local/nginx/conf/fastcgi_params,每個server都可以享用
      include fastcgi_params;
#包括fastcgi_params中參數(shù)
    }
   
    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/目錄中創(chuàng)建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設(shè)置和alias php設(shè)置中,他們的php頁面的系統(tǒng)地址是不同的,比如:
默認php文件->/usr/local/nginx/html/index.php
Alias php文件->/mnt/bbs/index.php
這個時候你會發(fā)現(xiàn)fastcgi_params中的SCRIPT_FILENAME的值是相同的,這樣會導致alias php的頁面出不來,而配置在nginx.conf中各自配置各自的php系統(tǒng)地址,這樣比較靈活.
 
#如果你覺得每個連接php的配置中都要加這一句話有點冗余,那就把它加入到fastcgi_params文件中,這樣只需要加一次,其他所有的nginx.conf中的有關(guān)連接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日志的腳本

1、創(chuàng)建腳本/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`

2、設(shè)置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公司網(wǎng)站
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公司網(wǎng)站
 
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兩家公司網(wǎng)站
 
21.列表頁顯示
location / {
            autoindex on;           #打開列表頁
            root   html;
            index  index.html index.php index.htm;
         }
 
22.虛擬目錄設(shè)置
location /bbs{
                alias /mnt/bbs/;
        }
#這樣配置html靜態(tài)文件是可以出來的,但是php動態(tài)頁面出不來,而且會瀏覽器的頁面上會顯示" No input file specified. "的報錯,其實是php系統(tǒng)文件地址( 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;
        }
#后面兩行是關(guān)于日志的,就是為了更好的觀察由nginx提交給fastcgi的php的系統(tǒng)地址SCRIPT_FILENAME,在這里我用$request_filename來給SCRIPT_FILENAME賦值,在日志中的結(jié)果為/mnt/bbs/index.php,在這里我發(fā)現(xiàn)一個問題就是$request_filename中的root設(shè)置為/mnt,否則$request_filename的值為:/mnt/bbs/bbs/index.php.
 
由以上可以得到一個結(jié)論,就是默認php設(shè)置也可以這樣設(shè)置關(guān)于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系統(tǒng)地址,日志中顯示為/scripts/index.php
23.nginx狀態(tài)監(jiān)控
location /nginxstatus{
        stub_status on;
        access_log  off;
        }
#客戶端訪問網(wǎng)址:http://www.baidu.com/nginxstatus
 
24.rewrite正則過濾
location ~ \.php$ {
        proxy_pass   http://127.0.0.1;
        }
Rewrite指令最后一項參數(shù)為flag標記,支持的flag標記如下:
Last 標示完成rewrite規(guī)則
Break 不再匹配后面的規(guī)則
Redirect 302臨時重定向
Permanent 301永久重定向
Last和break用來實現(xiàn)uri重寫,瀏覽器地址欄的url地址不變,但在服務(wù)器訪問的路徑發(fā)生了變化,redirect和permanent用來實現(xiàn)url跳轉(zhuǎn),瀏覽器地址欄會顯示跳轉(zhuǎn)后的url地址,使用alias指令時必須使用last標記,使用proxy_pass指令時要使用break標記,last標記在本條rewrite規(guī)則執(zhí)行完畢后,會對其所在的server{}標簽重新發(fā)起請求,而break標記則在本條規(guī)則匹配完成后,終止匹配,不再匹配后面的規(guī)則.
 
在匹配的過程中,nginx將首先匹配字符串,然后再匹配正則表達式,匹配到第一個正則表達式后,會停止搜索,如果匹配到正則表達式,則使用正則表達式的搜索結(jié)果,如果沒有匹配到正則表達式,則使用字符串的搜索結(jié)果.
可以使用前綴"^~"來禁止匹配到字符串后,再去檢查正則表達式,匹配到url后,將停止查詢.
使用前綴"="可以進行精確的url匹配,如果找到匹配的uri,則停止查詢,例如"location=/",只能匹配到"/",而"/test.html"則不能被匹配.
正則表達式的匹配,按照它們在配置文件中的順序進行,寫在前面的優(yōu)先.
Location = / {
       #僅僅匹配 /
    [configuration A]
}
Location / {
       #匹配任何以/開頭的查詢,但是正則表達式及較長的字符串(/bbs/)將被優(yōu)先匹配.
    [configuration B]
}
Location ^~ /images/ {
       #匹配任何以/images/開頭的字符串,并且停止搜索,所以正則表達式將不會被檢查.
    [configuration C]
}
Location ~* \.(gif|jpg|jpeg)$ {
       #匹配以.gif、.jpg、.jpeg結(jié)尾的任何請求,但是,/images/內(nèi)的請求將使用configuration c的配置
       [configuratoin D]
}
請求處理匹配結(jié)果示例:
/ -> configuration A;
/documents/document.html -> configuration B;
/images/1.gif -> configuration c;
/documents/1.jpg -> configuration D;
 
例1:域名跳轉(zhuǎn)
輸入www.sina.com,跳轉(zhuǎn)到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    ,也可以寫為.* 任意都跳轉(zhuǎn)
                     }
        }
        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:靜態(tài)index.html跳轉(zhuǎn)到動態(tài)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+ 多個數(shù)字。()第一個變量。     /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
                     會跳轉(zhuǎn)到index.php,把2當變量傳入index.php程序
   
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變量向后端服務(wù)器傳遞需要解析的客戶端訪問的域名(傳遞域名)
proxy_set_header X-Forwarded-For $remote_addr;
              #$remote_addr 把客戶端真實IP賦予X-Forwarded-For。后端服務(wù)器才能獲取真實的客戶端IP。以便記錄日志,要不日志中記錄的訪問信息都是負載服務(wù)器,而不是客戶端(傳遞IP)
}
}
 
 
26.模塊設(shè)置
Error_log
#錯誤日志
Include
#包含子配置文件,0.6版本以后子配置文件放在nginx.conf所在的路徑下
Pid
#主進程id號
User
#nginx nginx表明nginx進程的執(zhí)行者和組
Worker_processes
#與cpu個數(shù)相同,4核cpu為4
Worker_rlimit_nofile 65535
#打開的文件描述符,不過提前得設(shè)置ulimit -SHn 65535,即linux允許的打開文件個數(shù)
Worker_connectiones 65535
#客戶端最大連接數(shù)65535
Alias
#虛擬目錄
Error_page
#404,500錯誤跳轉(zhuǎn)頁面
Index
#index index.html,設(shè)置默認首頁
Keepalive_timeout
#即tcp持續(xù)連接超時時間
Limit_rate
#limit _rate 100k,即限速為100KB/s
Limit_rate_after
#limit_rate_after 1m,即下載文件超過1m,則進入limit_rate限速階段
Listen
#listen 192.168.100.1:80,即設(shè)置ip和端口
Location
#該指令允許對不同的uri進行不同的配置,可以是字符串、正則表達式
Resolver
#resolver 8.8.8.8,為nginx設(shè)置dns域名指向
Root
#設(shè)置網(wǎng)站根目錄
Send_timeout
#超時時間是指進行了兩次tcp握手,還沒有轉(zhuǎn)為established狀態(tài)的時間,如果超過這個時間,客戶沒有響應(yīng),nginx則關(guān)閉連接,可以用來防止ddos攻擊
Sendfile
#啟用或禁用sendfile()函數(shù),作用于拷貝兩個文件描述符之間的操作函數(shù),這個拷貝是在內(nèi)核中操作的,比read和write拷貝高效得多
Server
#普通web配置或虛擬主機的配置的區(qū)域
Server_name
#根據(jù)客戶端請求header頭信息中的host域名,來匹配該請求應(yīng)該由哪個虛擬主機配置或服務(wù)器的ip
Tcp_nodelay
#封裝tcp/ip數(shù)據(jù)包的等待時間,也叫納格算法,在keepalive開啟才有用
Tcp_nopush
#要求sendfile開啟的時候才起作用,設(shè)置該選擇的原因是nginx在linux上,試圖在一個包中發(fā)送它的httpd應(yīng)答頭
Allow
#allow 192.168.100.254,只允許192.168.100.254訪問
Deny
#deny all,拒絕其他任何人訪問
Autoindex
#autoindex on,即開啟列表頁功能
Charset
#charset utf8;source_charset gbk,把服務(wù)器上的gbk網(wǎng)頁編碼轉(zhuǎn)換成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中的服務(wù)器
Proxy_set_header
# proxy_set_header Host $host,設(shè)置把$host帶給后端的nginx服務(wù)器
Proxy_temp_path
#用戶指定一個本地目錄緩沖較大的請求,類似于client_body_temp_path
Stub_status
# stub_status on,即開戶狀態(tài)監(jiān)控
Image_filter
#它指定適用于圖片的轉(zhuǎn)換類型

相關(guān)文章

  • CentOS平臺實現(xiàn)搭建rsync遠程同步服務(wù)器的方法

    CentOS平臺實現(xiàn)搭建rsync遠程同步服務(wù)器的方法

    這篇文章主要介紹了CentOS平臺實現(xiàn)搭建rsync遠程同步服務(wù)器的方法,較為詳細的分析了CentOS搭建rsync遠程同步服務(wù)器的具體步驟、相關(guān)命令與注意事項,需要的朋友可以參考下
    2018-04-04
  • 在Linux系統(tǒng)中高效查找文件所在位置的方法

    在Linux系統(tǒng)中高效查找文件所在位置的方法

    Linux操作系統(tǒng)以其強大的功能和靈活性而聞名,其文件系統(tǒng)結(jié)構(gòu)也是其強大功能的一部分,在Linux中,文件和目錄以樹狀結(jié)構(gòu)組織,這使得查找特定文件或目錄變得至關(guān)重要,本文將介紹幾種在Linux中查找文件所在位置的方法,從而提高工作效率,需要的朋友可以參考下
    2024-11-11
  • Linux中文件系統(tǒng)truncate.c詳解

    Linux中文件系統(tǒng)truncate.c詳解

    這篇文章主要介紹了Linux中文件系統(tǒng)truncate.c詳解,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Linux的wget命令詳解

    Linux的wget命令詳解

    這篇文章主要為大家詳細介紹了Linux的wget命令,教大家如何使用wget,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • CentOS7?minimal?最小化安裝網(wǎng)絡(luò)設(shè)置過程

    CentOS7?minimal?最小化安裝網(wǎng)絡(luò)設(shè)置過程

    這篇文章主要介紹了CentOS7?minimal?最小化安裝網(wǎng)絡(luò)設(shè)置過程,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Linux書簽上下移動方式

    Linux書簽上下移動方式

    這篇文章主要介紹了Linux書簽上下移動方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-03-03
  • CentOS 7.2安裝Zabbix 3.2教程詳解

    CentOS 7.2安裝Zabbix 3.2教程詳解

    這篇文章主要介紹了CentOS 7.2安裝Zabbix 3.2教程詳解的相關(guān)資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下
    2016-11-11
  • Ubuntu18.04系統(tǒng)安裝、配置Redis及phpredis擴展操作詳解

    Ubuntu18.04系統(tǒng)安裝、配置Redis及phpredis擴展操作詳解

    這篇文章主要介紹了Ubuntu18.04系統(tǒng)安裝、配置Redis及phpredis擴展操作,結(jié)合實例形式分析了Ubuntu18.04系統(tǒng)安裝、配置Redis及phpredis擴展的相關(guān)原理、步驟、配置命令與操作注意事項,需要的朋友可以參考下
    2020-03-03
  • Linux下自動刪除歸檔日志文件的方法

    Linux下自動刪除歸檔日志文件的方法

    這篇文章主要介紹了Linux下自動刪除歸檔日志文件的方法,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
    2016-12-12
  • Linux如何使用ntp自動聯(lián)網(wǎng)校準時間

    Linux如何使用ntp自動聯(lián)網(wǎng)校準時間

    電腦用戶經(jīng)常需要查看當前時間、安裝網(wǎng)絡(luò)時間協(xié)議(NTP)服務(wù)和校準時間,確保系統(tǒng)時間的準確性,首先,用戶可通過系統(tǒng)自帶功能查看當前時間,其次,通過安裝NTP服務(wù)可以自動同步網(wǎng)絡(luò)時間,用戶需要選擇合適的NTP服務(wù)器,這些服務(wù)器地址可以在互聯(lián)網(wǎng)上查到
    2024-10-10

最新評論

名山县| 大埔区| 黄浦区| 海盐县| 武功县| 潍坊市| 清徐县| 兴安县| 赤城县| 辽中县| 宝鸡市| 定陶县| 察哈| 遂溪县| 探索| 漳州市| 竹溪县| 五家渠市| 格尔木市| 田东县| 丰台区| 无为县| 高州市| 广元市| 黑水县| 紫阳县| 南木林县| 尚义县| 壶关县| 东乌珠穆沁旗| 吴堡县| 博白县| 嘉善县| 堆龙德庆县| 文山县| 永安市| 龙里县| 固阳县| 临颍县| 天柱县| 岑溪市|