julley 发表于 2015-8-21 09:33:33

LNMP安装与配置

  Nginx与apache、lighttp性能综合对比,如下图:
                          
  
  注意:关闭rpm默认安装的apache和mysql
  1.准备php函数的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源代码包所需要的函数支持包
tar zxvf libiconv-1.13.1.tar.gz
cd libiconv-1.13.1/
./configure --prefix=/usr/local
make
make install
cd ../
(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 ../../
  (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
  
  default-character-set=utf8
  #修改客户端和连接字符集
  
  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 password123
  #修改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
  #-p1    是数字
#解压并打补丁,让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
  
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
usernginx nginx;
worker_processes 1;
#相当于cpu个数
error_loglogs/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_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
            '$status $body_bytes_sent "$http_referer" '
            '"$http_user_agent" "$http_x_forwarded_for"';
#日志格式
access_loglogs/access.logmain;
#调用格式的日志
      
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_length1k;
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_zonecrawler$binary_remote_addr10m;
server
{
    listen       80;
#监听端口
    server_name192.168.150.253;
#主机名,或IP。如果是主机名,要能够DNS解析

location / {
roothtml;
#网站主目录。/usr/local/nginx/html/
index index.html index.htm index.php;
#默认网页顺序
}

    #limit_conn   crawler20;   
                           
    location ~ .*\.(php|php5)?$
#~:匹配      后面正则表达式:.*任意字符\.点php或php5结尾。碰到网页文
件名是.php或.php5结尾
    {   
roothtml;
      #fastcgi_passunix:/tmp/php-cgi.sock;
      fastcgi_pass127.0.0.1:9000;
#连接fastcgi,用来解析php语句
      fastcgi_index index.php;
#首页为index.php
   #fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name
fastcgi_paramSCRIPT_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_formataccess'$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.logaccess;
      }
}

  15. 在/usr/local/nginx/conf/目录中创建fastcgi_params文件
  Vi/usr/local/nginx/conf/fastcgi_params         (与配置文件中,只写一个就好)
fastcgi_paramSCRIPT_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_paramSCRIPT_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-urootnginx取出nginx主进程PID
3)/usr/local/nginx/sbin/nginx -s reload

19. 编写每天定时切割Nginx日志的脚本
1、创建脚本/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、设置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
==&egrave;www.sina.com公司网站
server {
      listen       80;
      server_namewww.sina.com;
      access_loglogs/sina.access.logmain;
      location / {
            root   /web/sina;
            indexindex.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_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            include      fastcgi_params;
      }
    }
==&egrave;www.sohu.com公司网站

server {                              
      listen       80;
      server_namewww.sohu.com;
      access_loglogs/sohu.access.logmain;
      location / {
            root   /web/sohu;
            indexindex.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_indexindex.php;
      #   fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            include      fastcgi_params;
      }

最后在客户端测试虚拟主机www.baidu.com和www.sina.com两家公司网站

  21.列表页显示
location / {
            autoindex on;         #打开列表页
            root   html;
            indexindex.html index.php index.htm;
         }

  22.虚拟目录设置
location /bbs{
                alias /mnt/bbs/;
      }
#这样配置html静态文件是可以出来的,但是php动态页面出不来,而且会浏览器的页面上会显示" No input file specified. "的报错,其实是php系统文件地址( SCRIPT_FILENAME)找不到,也就是说fastcgi_paramSCRIPT_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_indexindex.php;
                     fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            include      fastcgi_params;
            log_formatbbs'$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_indexindex.php;
            fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;
            include      fastcgi_params;
            log_formatphp'$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_logoff;
      }
#客户端访问网址: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 = / {
       #仅仅匹配 /
   
}
Location / {
       #匹配任何以/开头的查询,但是正则表达式及较长的字符串(/bbs/)将被优先匹配.
   
}
Location ^~ /images/ {
       #匹配任何以/images/开头的字符串,并且停止搜索,所以正则表达式将不会被检查.
   
}
Location ~* \.(gif|jpg|jpeg)$ {
       #匹配以.gif、.jpg、.jpeg结尾的任何请求,但是,/images/内的请求将使用configuration c的配置
      
}
请求处理匹配结果示例:
/ -> 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_namewww.sina.com;
      access_loglogs/sina.access.logmain;
      location / {
            root   /web/sina;
            indexindex.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_namewww.sohu.com;
      access_loglogs/sohu.access.logmain;
      location / {
            root   /web/sohu;
            indexindex.html index.htm;
      }
      error_page   500 502 503 504/50x.html;
      location = /50x.html {
            root   html;
      }
      }

例2:静态index.html跳转到动态index.php文件
cd/web/sina/
viindex.php
       <?
      print_r ($_GET);
?>
       Vinginx.conf
         server {
      listen       80;
      server_namewww.sina.com;
      access_loglogs/sina.access.logmain;
      location / {
            root   /web/sina;
            indexindex.html index.htm;
            rewrite^/index(\d+).html/index.php?id=$1last;
# ^/ 以/开头。\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_indexindex.php;
            fastcgi_paramSCRIPT_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_namewww.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_namewww.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_indexindex.php;
#fastcgi_paramSCRIPT_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
#它指定适用于图片的转换类型
页: [1]
查看完整版本: LNMP安装与配置