zl程序教程

您现在的位置是:首页 >  系统

当前栏目

linux服务器下LNMP安装与配置方法

2023-06-13 09:15:03 时间
Nginx与apache、lighttp性能综合对比,如下图:



注意:关闭rpm默认安装的apache和mysql

1.准备php函数的rpm包

yum-yinstallgccgcc-c++autoconflibjpeglibjpeg-devellibpnglibpng-develfreetypefreetype-devellibxml2libxml2-develzlibzlib-develglibcglibc-develglib2glib2-develbzip2bzip2-develncursesncurses-develcurlcurl-devele2fsprogse2fsprogs-develkrb5krb5-devellibidnlibidn-developensslopenssl-developenldapopenldap-develnss_ldapopenldap-clientsopenldap-servers

2.准备lnmp其他的源代码包

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

3.安装php-5.2.14源代码包所需要的函数支持包

复制代码代码如下:


tarzxvflibiconv-1.13.1.tar.gz
cdlibiconv-1.13.1/
./configure--prefix=/usr/local
make
makeinstall
cd../

(libiconv库为需要做转换的应用提供了一个iconv()的函数,以实现一个字符编码到另一个字符编码的转换)

复制代码代码如下:


tarzxvflibmcrypt-2.5.8.tar.gz
cdlibmcrypt-2.5.8/
./configure
make
makeinstall
cdlibltdl/
./configure--enable-ltdl-install
make
makeinstall
cd../../

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

复制代码代码如下:
tarzxvfmhash-0.9.9.9.tar.gz
cdmhash-0.9.9.9/
./configure
make
makeinstall
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
tarzxvfmcrypt-2.6.8.tar.gz
cdmcrypt-2.6.8/
./configure
make
makeinstall
cd../

4.编译安装MySQL5.5.3-m3

复制代码代码如下:
groupaddmysql
useradd-gmysqlmysql

tarzxvfmysql-5.5.3-m3.tar.gz
cdmysql-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
#编译
Makeinstall
#安装
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-mu:mysql:rwx-R/usr/local/mysql
Setfacl-md: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-urootpassword 123
#修改mysql登录密码为123
/usr/local/mysql/bin/mysql-uroot-p123
#用mysql登录

5.编译安装PHP(FastCGI模式。使用fastCGI管理php,加快php解析速度)

复制代码代码如下:
tarzxvfphp-5.2.14.tar.gz
gzip-cdphp-5.2.14-fpm-0.5.14.diff.gz|patch-dphp-5.2.14-p1
#-p 1   是数字
#解压并打补丁,让php支持fpm来方便管理php-cgi进程(使用php-fpm管理fastCGI)
#gzip  -c 保留源文件  -d 解压
cdphp-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
makeZEND_EXTRA_LIBS="-liconv"
#编译过程设定变量(编译过程需要)
makeinstall
cp/lnmp/php-5.2.14/php.ini-dist /usr/local/php/etc/php.ini
cd../

6.准备编译安装PHP5扩展模块

复制代码代码如下:
tarzxvfmemcache-2.2.5.tgz
cdmemcache-2.2.5/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
make
makeinstall
cd../
tarjxvfeaccelerator-0.9.6.1.tar.bz2
cdeaccelerator-0.9.6.1/
/usr/local/php/bin/phpize
./configure--enable-eaccelerator=shared--with-php-config=/usr/local/php/bin/php-config
make
makeinstall
cd../
(eAccelerator是一个自由开放源码php加速器,优化和动态内容缓存,提高了php脚本的缓存性能,使得PHP脚本在编译的状态下,对服务器的开销几乎完全消除。它还有对脚本起优化作用,以加快其执行效率。使您的PHP程序代码执效率能提高1-10倍)
tarzxvfPDO_MYSQL-1.0.2.tgz
cdPDO_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
makeinstall
cd../
tarzxvfImageMagick.tar.gz
cdImageMagick-6.5.1-2/
./configure
make
makeinstall
cd../
(ImageMagick是一套功能强大、稳定而且免费的工具集和开发包,可以用来读、写和处理超过89种基本格式的图片文件,包括流行的TIFF、JPEG、GIF、PNG、PDF以及PhotoCD等格式)
tarzxvfimagick-2.3.0.tgz
cdimagick-2.3.0/
/usr/local/php/bin/phpize
./configure--with-php-config=/usr/local/php/bin/php-config
make
makeinstall
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进程执行者用户

useraddnginx

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

vi/usr/local/php/etc/php-fpm.conf
<valuename="display_errors">0</value>
#0改成1,页面上会输出错误日志.  取消注释
unixuserofprocesses
     <valuename="user">nginx</value>
     Unixgroupofprocesses
     <valuename="group">nginx</value>                       取消注释

 <valuename="max_children">128</value>
#最大子进程数128,如果内存小于2G,则64个最佳
<valuename="rlimit_files">65535</value>
#Setopenfiledescrlimit,同时打开的文件数,linux系统允许同时打开的文件数为1024,修改linux系统中允许同时打开的文件,ulimit-SHn65535,而且这个参数重启后还能生效,加到/etc/profile全局配置文件的最后,开机就会生效,ulimit-a查看openfiles65535
      ulimit 用户控制shell启动进程所占用的资源
             -H  设定硬性资源限制,也就是管理员设定的限制
             -S   设定软性资源限制,弹性限制
             -n   设定可同时打开的最大文件个数
             -f    设定单个文件最大大小
             -a   查看目前的限制
<valuename="max_requests">1024</value>
#最大请求数,Howmuchrequestseachprocessshouldexecutebeforerespawn.一个子进程能够回应1042个请求

11.启动php-cgi(fastcgi)进程,监听127.0.0.1的9000端口,进程数为128(如果服务器内存小于3GB,可以只开启64个进程),用户为nginx:

复制代码代码如下:
/usr/local/php/sbin/php-fpmstart
#启动php-cgi
/usr/local/php/sbin/php-fpmreload
#重新加载配置文件
/usr/local/php/sbin/php-fpmstop
#关闭php-fpm,此时nginx肯定连不上php

12.安装Nginx所需的pcre库

复制代码代码如下:
tarzxvfpcre-8.10.tar.gz
cdpcre-8.10/
./configure
make&&makeinstall
cd../

13.安装Nginx

复制代码代码如下:
tarzxvfnginx-0.8.46.tar.gz
cdnginx-0.8.46/
./configure--user=nginx--group=nginx--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
make&&makeinstall
cd../

14.修改Nginx配置文件

复制代码代码如下:
vi/usr/local/nginx/conf/nginx.conf
user nginxnginx;
worker_processes1;
#相当于cpu个数
error_log logs/nginx_error.log;
#错误日志
pid       /usr/local/nginx/nginx.pid;
#主进程PID保存文件
#Specifiesthevalueformaximumfiledescriptorsthatcanbeopenedbythisprocess.
worker_rlimit_nofile65535;
#文件描述符数量
events
{
 useepoll;
#网络I/O模型,建议linux使用epoll,FreeBSD使用kqueue
 worker_connections65535;
#最大允许连接数
}
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;
#调用格式的日志

 sendfileon;
 tcp_nopush    on;
#tcp延迟
 keepalive_timeout65;
#保持连接时间

 fastcgi_connect_timeout300;
 fastcgi_send_timeout300;
 fastcgi_read_timeout300;
 fastcgi_buffer_size64k;
 fastcgi_buffers464k;
 fastcgi_busy_buffers_size128k;
 fastcgi_temp_file_write_size128k;
#fastcgi设置
 gzipon;
 gzip_min_length 1k;
 gzip_buffers    416k;
 gzip_http_version1.0;
 gzip_comp_level2;
 gzip_types      text/plainapplication/x-javascripttext/cssapplication/xml;
 gzip_varyon;
#网络压缩设置
 #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/
indexindex.htmlindex.htmindex.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_indexindex.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都可以享用
     includefastcgi_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设置和aliasphp设置中,他们的php页面的系统地址是不同的,比如:
默认php文件->/usr/local/nginx/html/index.php
Aliasphp文件->/mnt/bbs/index.php
这个时候你会发现fastcgi_params中的SCRIPT_FILENAME的值是相同的,这样会导致aliasphp的页面出不来,而配置在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-fpmstart
3)ulimit
Vi/etc/profile
ulimit-SHn65535
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-HUPnginx
2)kill-HUP`pgrep-urootnginx`
  Pgrep -uroot nginx 取出nginx主进程PID
3)/usr/local/nginx/sbin/nginx-sreload

19.编写每天定时切割Nginx日志的脚本

1、创建脚本/usr/local/nginx/sbin/cut_nginx_log.sh
vi/usr/local/nginx/sbin/cut_nginx_log.sh

#!/bin/bash
#Thisscriptrunat00:00
#TheNginxlogspath
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
0000***/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.htmlindex.htm;
       }
       error_page  500502503504 /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.htmlindex.htm;
       }
       error_page  500502503504 /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/{
           autoindexon;          #打开列表页
           root  html;
           index index.htmlindex.phpindex.htm;
        }

22.虚拟目录设置
location/bbs{
               alias/mnt/bbs/;
       }
#这样配置html静态文件是可以出来的,但是php动态页面出不来,而且会浏览器的页面上会显示"Noinputfilespecified."的报错,其实是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/;
           indexbbs.phpindex.htmlindex.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_loglogs/bbs.access.logbbs;
       }
#后面两行是关于日志的,就是为了更好的观察由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_loglogs/php.access.logphp;
       }
#此时从日志中可以看到,$request_filename的值为/usr/local/nginx/html/index.php,而以前默认的/scripts$fastcgi_script_name显然是错的php系统地址,日志中显示为/scripts/index.php
23.nginx状态监控
location/nginxstatus{
       stub_statuson;
       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不再匹配后面的规则
Redirect302临时重定向
Permanent301永久重定向
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=/{
      #仅仅匹配/
   [configurationA]
}
Location/{
      #匹配任何以/开头的查询,但是正则表达式及较长的字符串(/bbs/)将被优先匹配.
   [configurationB]
}
Location^~/images/{
      #匹配任何以/images/开头的字符串,并且停止搜索,所以正则表达式将不会被检查.
   [configurationC]
}
Location~*\.(gif|jpg|jpeg)${
      #匹配以.gif、.jpg、.jpeg结尾的任何请求,但是,/images/内的请求将使用configurationc的配置
      [configuratoinD]
}
请求处理匹配结果示例:
/->configurationA;
/documents/document.html->configurationB;
/images/1.gif->configurationc;
/documents/1.jpg->configurationD;

例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.htmlindex.htm;
                    if(-e$request_filename){
                           #-e  是否存在
           rewrite ^/ http://www.sohu.com/ permanent;
                           #^/ 域名以/开头。//www.sina.com   ,也可以写为.*任意都跳转
                    }
       }
       error_page  500502503504 /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.htmlindex.htm;
       }
       error_page  500502503504 /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.htmlindex.htm;
           rewrite ^/index(\d+).html /index.php?id=$1 last;
#^/以/开头。\d+多个数字。()第一个变量。    /index.php?id=$1把第一个变量赋予id变量,传入index.php文件
       }
       error_page  500502503504 /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.代理负载均衡技术(反向代理)
upstreammyweb1{
      #定义地址池       

192.168.190.190      

   
反向代理

upstreammyweb1{

           server192.168.190.190:80;   
           server192.168.190.191:80;

}

 

       server{

           listen      80;

           server_name www.sohu.com;

           location/{

               proxy_passhttp://myweb1;

               proxy_set_headerHost    $host;

               proxy_set_header    X-Forwarded-For$remote_addr;

           }

       }

        
       server192.168.244.10:80;

       server192.168.244.11:80;
}
   server{
       listen      80;
       server_name www.sohu.com;

            

192.168.190.191

        
                           #使用www.sohu.com访问

location/{
proxy_passhttp://myweb1;
             #使用地址池
proxy_set_headerHost$host;
             #利用HOST变量向后端服务器传递需要解析的客户端访问的域名(传递域名)
proxy_set_headerX-Forwarded-For$remote_addr;
             #$remote_addr把客户端真实IP赋予X-Forwarded-For。后端服务器才能获取真实的客户端IP。以便记录日志,要不日志中记录的访问信息都是负载服务器,而不是客户端(传递IP)
}
}

 
26.模块设置
Error_log
#错误日志
Include
#包含子配置文件,0.6版本以后子配置文件放在nginx.conf所在的路径下
Pid
#主进程id号
User
#nginxnginx表明nginx进程的执行者和组
Worker_processes
#与cpu个数相同,4核cpu为4
Worker_rlimit_nofile65535
#打开的文件描述符,不过提前得设置ulimit-SHn65535,即linux允许的打开文件个数
Worker_connectiones65535
#客户端最大连接数65535
Alias
#虚拟目录
Error_page
#404,500错误跳转页面
Index
#indexindex.html,设置默认首页
Keepalive_timeout
#即tcp持续连接超时时间
Limit_rate
#limit_rate100k,即限速为100KB/s
Limit_rate_after
#limit_rate_after1m,即下载文件超过1m,则进入limit_rate限速阶段
Listen
#listen192.168.100.1:80,即设置ip和端口
Location
#该指令允许对不同的uri进行不同的配置,可以是字符串、正则表达式
Resolver
#resolver8.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
#allow192.168.100.254,只允许192.168.100.254访问
Deny
#denyall,拒绝其他任何人访问
Autoindex
#autoindexon,即开启列表页功能
Charset
#charsetutf8;source_charsetgbk,把服务器上的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_passhttp://myweb1,即后跟的是nginx代理负载池upstream中的服务器
Proxy_set_header
#proxy_set_headerHost$host,设置把$host带给后端的nginx服务器
Proxy_temp_path
#用户指定一个本地目录缓冲较大的代理请求,类似于client_body_temp_path
Stub_status
#stub_statuson,即开户状态监控
Image_filter
#它指定适用于图片的转换类型