Nginx学习

一、什么是Nginx

    Nginx 是一个高性能的 Web 和反向代理服务器, 它具有有很多非常优越的特性:
    作为 Web 服务器:相比 Apache,Nginx 使用更少的资源,支持更多的并发连接,体现更高的效率,这点使 Nginx 尤其受到虚拟主机提供商的欢迎。能够支持高达 50,000 个并发连接数的响应,感谢 Nginx 为我们选择了 epoll and kqueue 作为开发模型.


    作为负载均衡服务器:Nginx 既可以在内部直接支持 Rails 和 PHP,也可以支持作为 HTTP代理服务器对外进行服务。Nginx 用C 编写, 不论是系统资源开销还是 CPU 使用效率都比 Perlbal 要好的多。


    作为邮件代理服务器: Nginx 同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm 描述了成功并且美妙的使用经验。


    Nginx 安装非常的简单,配置文件非常简洁(还能够支持perl语法),Bugs非常少的服务器: Nginx 启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下进行软件版本的升级。


二、为什么使用Nginx

    为什么Nginx的性能要比Apache高得多?这得益于Nginx使用了最新的epoll(Linux 2.6内核)和kqueue(freebsd)网络I/O模型,而Apache则使用的是传统的select模型。目前Linux下能够承受高并发访问的Squid、Memcached都采用的是epoll网络I/O模型。

 

  处理大量的连接的读写,Apache所采用的select网络I/O模型非常低效。下面用一个比喻来解析Apache采用的select模型和Nginx采用的epoll模型进行之间的区别:

 

  假设你在大学读书,住的宿舍楼有很多间房间,你的朋友要来找你。select版宿管大妈就会带着你的朋友挨个房间去找,直到找到你为止。而epoll版宿管大妈会先记下每位同学的房间号,你的朋友来时,只需告诉你的朋友你住在哪个房间即可,不用亲自带着你的朋友满大楼找人。如果来了10000个人,都要找自己住这栋楼的同学时,select版和epoll版宿管大妈,谁的效率更高,不言自明。同理,在高并发服务器中,轮询I/O是最耗时间的操作之一,select和epoll的性能谁的性能更高,同样十分明了。

   

三、Nginx安装

    下详

 

四、Nginx配置

        Nginx配置文件说明

 

五、Nginx启动

 

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
killall nginx
/root/run.sh

  

    相关问题及解决:
    1、现象:Nginx启动提示
       Starting php_fpm  done
       [emerg]: bind() to 0.0.0.0:80 failed(98: Address already in use)
       [emerg]: bind() to 0.0.0.0:8088failed (98: Address already in use)
       [emerg]: bind() to 0.0.0.0:80 failed(98: Address already in use)
       [emerg]: bind() to 0.0.0.0:8088 failed(98: Address already in use)
       [emerg]: bind() to 0.0.0.0:80 failed(98: Address already in use)
       [emerg]: still could not bind()
       原因:端口被占用
       解决:netstat-ntpl————查看端口使用情况
             killallnginx————kill掉所有的nginx进程


六、Nginx作为web服务器的网站

    1、使用 Nginx 运行PHP(FastCGI) 程序的网站:
       六间房视频        nginx0.6.14
       Discuz官方论坛    nginx0.7.2
       互联星空播客      nginx0.5.33
       水木社区          nginx0.5.35
       落伍者站长论坛    nginx0.6.29
    2、使用 Nginx 作反向代理、规则过滤的网站:
       新浪博客          nginx0.5.35
       YUPOO相册         nginx0.5.35
    3、使用 Nginx 运行静态HTML页、图片、FLV视频的网站:
       网易新闻          nginx0.6.31
       酷6网             nginx0.5.36
       豆瓣网            nginx
       新华网RSS订阅频道 nginx
       迅雷安全中心      nginx0.6.31
       腾讯3G下载        nginx0.5.33

 

七、使用 Nginx 运行静态HTML页、图片、FLV视频

     待整理

 

八、Nginx编译时,支持的选项

 

--prefix=<path> - Nginx安装路径。如果没有指定,默认为 /usr/local/nginx。
--sbin-path=<path> - Nginx可执行文件安装路径。只能安装时指定,如果没有指定,默认为<prefix>/sbin/nginx。
--conf-path=<path> - 在没有给定-c选项下默认的nginx.conf的路径。如果没有指定,默认为<prefix>/conf/nginx.conf。
--pid-path=<path> - 在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。如果没有指定,默认为 <prefix>/logs/nginx.pid。
--lock-path=<path> - nginx.lock文件的路径。
--error-log-path=<path> - 在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。如果没有指定,默认为 <prefix>/logs/error.log。
--http-log-path=<path> - 在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。如果没有指定,默认为 <prefix>/logs/access.log。
--user=<user> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。如果没有指定,默认为 nobody。
--group=<group> - 在nginx.conf中没有指定user指令的情况下,默认的nginx使用的组。如果没有指定,默认为 nobody。
--builddir=DIR - 指定编译的目录
--with-rtsig_module - 启用 rtsig 模块
--with-select_module --without-select_module - Whether or not to enable the select module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
//允许或不允许开启SELECT模式,如果 configure 没有找到更合适的模式,比如:kqueue(sun os),epoll (linux kenel 2.6+), rtsig(实时信号)或者/dev/poll(一种类似select的模式,底层实现与SELECT基本相 同,都是采用轮训方法) SELECT模式将是默认安装模式
--with-poll_module --without-poll_module - Whether or not to enable the poll module. This module is enabled by default if a more suitable method such as kqueue, epoll, rtsig or /dev/poll is not discovered by configure.
--with-http_ssl_module - Enable ngx_http_ssl_module. Enables SSL support and the ability to handle HTTPS requests. Requires OpenSSL. On Debian, this is libssl-dev.
//开启HTTP SSL模块,使NGINX可以支持HTTPS请求。这个模块需要已经安装了OPENSSL,在DEBIAN上是libssl
--with-http_realip_module - 启用 ngx_http_realip_module
--with-http_addition_module - 启用 ngx_http_addition_module
--with-http_sub_module - 启用 ngx_http_sub_module
--with-http_dav_module - 启用 ngx_http_dav_module
--with-http_flv_module - 启用 ngx_http_flv_module
--with-http_stub_status_module - 启用 "server status" 页
--without-http_charset_module - 禁用 ngx_http_charset_module
--without-http_gzip_module - 禁用 ngx_http_gzip_module. 如果启用,需要 zlib 。
--without-http_ssi_module - 禁用 ngx_http_ssi_module
--without-http_userid_module - 禁用 ngx_http_userid_module
--without-http_access_module - 禁用 ngx_http_access_module
--without-http_auth_basic_module - 禁用 ngx_http_auth_basic_module
--without-http_autoindex_module - 禁用 ngx_http_autoindex_module
--without-http_geo_module - 禁用 ngx_http_geo_module
--without-http_map_module - 禁用 ngx_http_map_module
--without-http_referer_module - 禁用 ngx_http_referer_module
--without-http_rewrite_module - 禁用 ngx_http_rewrite_module. 如果启用需要 PCRE 。
--without-http_proxy_module - 禁用 ngx_http_proxy_module
--without-http_fastcgi_module - 禁用 ngx_http_fastcgi_module
--without-http_memcached_module - 禁用 ngx_http_memcached_module
--without-http_limit_zone_module - 禁用 ngx_http_limit_zone_module
--without-http_empty_gif_module - 禁用 ngx_http_empty_gif_module
--without-http_browser_module - 禁用 ngx_http_browser_module
--without-http_upstream_ip_hash_module - 禁用 ngx_http_upstream_ip_hash_module
--with-http_perl_module - 启用 ngx_http_perl_module
--with-perl_modules_path=PATH - 指定 perl 模块的路径
--with-perl=PATH - 指定 perl 执行文件的路径
--http-log-path=PATH - Set path to the http access log
--http-client-body-temp-path=PATH - Set path to the http client request body temporary files
--http-proxy-temp-path=PATH - Set path to the http proxy temporary files
--http-fastcgi-temp-path=PATH - Set path to the http fastcgi temporary files
--without-http - 禁用 HTTP server
--with-mail - 启用 IMAP4/POP3/SMTP 代理模块
--with-mail_ssl_module - 启用 ngx_mail_ssl_module
--with-cc=PATH - 指定 C 编译器的路径
--with-cpp=PATH - 指定 C 预处理器的路径
--with-cc-opt=OPTIONS - Additional parameters which will be added to the variable CFLAGS. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-cc-opt="-I /usr/local/include". If we are using select() and it is necessary to increase the number of file descriptors, then this also can be assigned here: --with-cc-opt="-D FD_SETSIZE=2048".
--with-ld-opt=OPTIONS - Additional parameters passed to the linker. With the use of the system library PCRE in FreeBSD, it is necessary to indicate --with-ld-opt="-L /usr/local/lib".
--with-cpu-opt=CPU - 为特定的 CPU 编译,有效的值包括:pentium, pentiumpro, pentium3, pentium4, athlon, opteron, amd64, sparc32, sparc64, ppc64
--without-pcre - 禁止 PCRE 库的使用。同时也会禁止 HTTP rewrite 模块。在 "location" 配置指令中的正则表达式也需要 PCRE 。
--with-pcre=DIR - 指定 PCRE 库的源代码的路径。
--with-pcre-opt=OPTIONS - Set additional options for PCRE building.
--with-md5=DIR - Set path to md5 library sources.
--with-md5-opt=OPTIONS - Set additional options for md5 building.
--with-md5-asm - Use md5 assembler sources.
--with-sha1=DIR - Set path to sha1 library sources.
--with-sha1-opt=OPTIONS - Set additional options for sha1 building.
--with-sha1-asm - Use sha1 assembler sources.
--with-zlib=DIR - Set path to zlib library sources.
--with-zlib-opt=OPTIONS - Set additional options for zlib building.
--with-zlib-asm=CPU - Use zlib assembler sources optimized for specified CPU, valid values are: pentium, pentiumpro
--with-openssl=DIR - Set path to OpenSSL library sources
--with-openssl-opt=OPTIONS - Set additional options for OpenSSL building
--with-debug - 启用调试日志
--add-module=PATH - Add in a third-party module found in directory PATH

 

九、参考文献

    1、LNMP-Linux下Nginx+MySQL+PHP+phpMyAdmin+eAcelerator一键安装包
       http://blog.licess.cn/lnmp/
    2、LEMP构建高性能WEB服务器
       http://bbs.linuxtone.org/thread-123-1-1.html
    3、Nginx 安装笔记(含PHP支持、虚拟主机、反向代理负载均衡)
       http://www.jb51.net/article/20456.htm
    4、nginx-and-memcached-a-400-boost
       http://www.igvita.com/2008/02/11/nginx-and-memcached-a-400-boost/
    5、Nginx 0.8.x + PHP 5.2.10(FastCGI)搭建胜过Apache十倍的Web服务器
       http://blog.s135.com/nginx_php_v5/
    6、使用Nginx作为Web服务器的国内网站[原创]
       http://blog.s135.com/post/352/
    7、关于Nginx+PHP的虚拟主机目录权限控制的探究
       http://www.xpb.cn/blog/665.html

 

一、软件下载:
    1、官网下载

cd /usr/src
wget http://sysoev.ru/nginx/nginx-0.8.15.tar.gz
wget http://www.php.net/get/php-5.2.10.tar.gz/from/this/mirror
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz
wget http://dev.mysql.com/get/Downloads/MySQL-5.1/mysql-5.1.38.tar.gz/from/http://mysql.he.net/
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.13.tar.gz
wget "http://downloads.sourceforge.net/mcrypt/libmcrypt-2.5.8.tar.gz?modtime=1171868460&big_mirror=0"
wget "http://downloads.sourceforge.net/mcrypt/mcrypt-2.6.8.tar.gz?modtime=1194463373&big_mirror=0"
wget http://pecl.php.net/get/memcache-2.2.5.tgz
wget "http://downloads.sourceforge.net/mhash/mhash-0.9.9.9.tar.gz?modtime=1175740843&big_mirror=0"
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
wget http://bart.eaccelerator.net/source/0.9.5.3/eaccelerator-0.9.5.3.tar.bz2
wget http://pecl.php.net/get/PDO_MYSQL-1.0.2.tgz
wget http://blog.s135.com/soft/linux/nginx_php/imagick/ImageMagick.tar.gz
wget http://pecl.php.net/get/imagick-2.2.2.tgz
以下为安装过程中可能需要的安装包
yum -y update
yum -y install patch make gcc gcc-c++ gcc-g77 flex bison
yum -y install libtool libtool-libs autoconf kernel-devel
yum -y install libjpeg libjpeg-devel libpng libpng-devel
yum -y install freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel
yum -y install glib2 glib2-devel bzip2
yum -y install bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs
yum -y install e2fsprogs-devel krb5 krb5-devel libidn libidn-devel
yum -y install openssl openssl-devel vim-minimal sendmail
yum -y install fonts-chinese scim-chewing scim-pinyin scim-tables-chinese

  2、从blog.s135.com下载(比较稳定,只允许在本站,或者在Linux/Unix下通过Wget、Curl等命令下载以下软件):
cd /usr/src
wget http://blog.s135.com/soft/linux/nginx_php/nginx/nginx-0.8.15.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/php/php-5.2.10.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/phpfpm/php-5.2.10-fpm-0.5.11.diff.gz
wget http://blog.s135.com/soft/linux/nginx_php/mysql/mysql-5.1.38.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/libiconv/libiconv-1.13.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-7.9.tar.gz
wget http://blog.s135.com/soft/linux/nginx_php/eaccelerator/eaccelerator-0.9.5.3.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.2.2.tgz

二、安装PHP 5.2.10(FastCGI模式)
  1、编译安装PHP 5.2.10所需的支持库:
tar -zxvf libiconv-1.13.tar.gz
cd libiconv-1.13/
./configure --prefix=/usr/local
make
make install
cd ../

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

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

tar -zxvf mcrypt-2.6.8.tar.gz
cd mcrypt-2.6.8/
/sbin/ldconfig
./configure
make
make install
cd ../

  2、编译安装MySQL 5.1.38
tar -zxvf mysql-5.1.35.tar.gz
cd mysql-5.1.35
./configure --prefix=/usr/local/mysql --enable-assembler --with-charset=utf8 --enable-thread-safe-client --with-extra-charsets=all --without-isam
make;make install
cd ../
groupadd mysql
useradd -g mysql mysql
cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf
/usr/local/mysql/bin/mysql_install_db --user=mysql
chown -R mysql /usr/local/mysql/var
chgrp -R mysql /usr/local/mysql/.
cp /usr/local/mysql/share/mysql/mysql.server /etc/init.d/mysql
chmod 755 /etc/init.d/mysql
chkconfig --level 345 mysql on
echo "/usr/local/mysql/lib/mysql" >> /etc/ld.so.conf
echo "/usr/local/lib" >>/etc/ld.so.conf
ldconfig
ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
ln -s /usr/local/mysql/include/mysql /usr/include/mysql
service mysql start
/usr/local/mysql/bin/mysqladmin -u root password 123
service mysql restart
service mysql stop



  附:以下为附加步骤,如果你想在这台服务器上运行MySQL数据库,则执行以下两步。如果你只是希望让PHP支持MySQL扩展库,能够连接其他服务器上的MySQL数据库,那么,以下无需执行。

  ①、创建MySQL数据库存放目录
mkdir -p /data0/mysql/3306/data/
chown -R mysql:mysql /data0/mysql/

  ②、以mysql用户帐号的身份建立数据表:
/usr/local/mysql/bin/mysql_install_db --basedir=/usr/local/mysql --datadir=/data0/mysql/3306/data --user=mysql

  ③、创建my.cnf配置文件:
vi /data0/mysql/3306/my.cnf


  输入以下内容: 

[client]
default-character-set = utf8
port    = 3306
socket  = /tmp/mysql.sock

[mysql]
prompt="(\u:blog.s135.com:)[\d]> "
no-auto-rehash

[mysqld]
#default-character-set = utf8
user    = mysql
port    = 3306
socket  = /tmp/mysql.sock
basedir = /usr/local/mysql
datadir = /data0/mysql/3306/data
open_files_limit    = 10240
back_log = 600
max_connections = 3000
max_connect_errors = 6000
table_cache = 614
external-locking = FALSE
max_allowed_packet = 32M
sort_buffer_size = 2M
join_buffer_size = 2M
thread_cache_size = 300
thread_concurrency = 8
query_cache_size = 32M
query_cache_limit = 2M
query_cache_min_res_unit = 2k
default-storage-engine = MyISAM
default_table_type = MyISAM
thread_stack = 192K
transaction_isolation = READ-COMMITTED
tmp_table_size = 246M
max_heap_table_size = 246M
long_query_time = 1
log_long_format
log-bin = /data0/mysql/3306/binlog
binlog_cache_size = 4M
binlog_format = MIXED
max_binlog_cache_size = 8M
max_binlog_size = 512M
expire_logs_days = 7
key_buffer_size = 256M
read_buffer_size = 1M
read_rnd_buffer_size = 16M
bulk_insert_buffer_size = 64M
myisam_sort_buffer_size = 128M
myisam_max_sort_file_size = 10G
myisam_max_extra_sort_file_size = 10G
myisam_repair_threads = 1
myisam_recover

skip-name-resolve
master-connect-retry = 10
slave-skip-errors = 1032,1062,126,1114,1146,1048,1396

server-id = 1

innodb_additional_mem_pool_size = 16M
innodb_buffer_pool_size = 2048M
innodb_data_file_path = ibdata1:1024M:autoextend
innodb_file_io_threads = 4
innodb_thread_concurrency = 8
innodb_flush_log_at_trx_commit = 2
innodb_log_buffer_size = 16M
innodb_log_file_size = 128M
innodb_log_files_in_group = 3
innodb_max_dirty_pages_pct = 90
innodb_lock_wait_timeout = 120
innodb_file_per_table = 0
[mysqldump]
quick
max_allowed_packet = 32M


  ④、创建管理MySQL数据库的shell脚本:

vi /data0/mysql/3306/mysql


  输入以下内容(这里的用户名root和密码123接下来的步骤会创建): 

  1. #!/bin/sh   
  2.   
  3. mysql_port=3306   
  4. mysql_username="root"  
  5. mysql_password="123"  
  6.   
  7. function_start_mysql()   
  8. {   
  9.     printf "Starting MySQL...\n"  
  10.     /bin/sh /usr/local/mysql/bin/mysqld_safe --defaults-file=/data0/mysql/${mysql_port}/my.cnf 2>&1 /dev/null &   
  11. }   
  12.   
  13. function_stop_mysql()   
  14. {   
  15.     printf "Stoping MySQL...\n"  
  16.     /usr/local/mysql/bin/mysqladmin -u ${mysql_username} -p${mysql_password} -S /tmp/mysql.sock shutdown   
  17. }   
  18.   
  19. function_restart_mysql()   
  20. {   
  21.     printf "Restarting MySQL...\n"  
  22.     function_stop_mysql   
  23.     sleep 5   
  24.     function_start_mysql   
  25. }   
  26.   
  27. function_kill_mysql()   
  28. {   
  29.     kill -9 $(ps -ef grep 'bin/mysqld_safe' grep ${mysql_port} awk '{printf $2}')   
  30.     kill -9 $(ps -ef grep 'libexec/mysqld' grep ${mysql_port} awk '{printf $2}')   
  31. }   
  32.   
  33. if "$1" "start" ]; then   
  34.     function_start_mysql   
  35. elif "$1" "stop" ]; then   
  36.     function_stop_mysql   
  37. elif "$1" "restart" ]; then   
  38. function_restart_mysql   
  39. elif "$1" "kill" ]; then   
  40. function_kill_mysql   
  41. else  
  42.     printf "Usage: /data0/mysql/${mysql_port}/mysql {start|stop|restart|kill}\n"  
  43. fi  

 

  ⑤、赋予shell脚本可执行权限:

chmod +x /data0/mysql/3306/mysql

  ⑥、启动MySQL:
/data0/mysql/3306/mysql start

  ⑦、通过命令行登录管理MySQL服务器(提示输入密码时直接回车):
/usr/local/mysql/bin/mysql -u root -p -S /tmp/mysql.sock

  ⑧、输入以下SQL语句,创建一个具有root权限的用户(admin)和密码(12345678):
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY '123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1' IDENTIFIED BY '123';

  ⑨、(可选)停止MySQL:
/data0/mysql/3306/mysql stop

3、编译安装PHP(FastCGI模式)

tar -zxvf php-5.2.10.tar.gz
//下面一步针对php-5.3.3的版本不需要执行,直接编译即可
gzip -cd php-5.2.10-fpm-0.5.11.diff.gz | patch -d php-5.2.10 -p1
cd php-5.2.10/
./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-xmlrpc --enable-zip --enable-ftp
make ZEND_EXTRA_LIBS='-liconv'
make install
cp php.ini-dist /usr/local/php/etc/php.ini
cd ../
curl http://pear.php.net/go-pear | /usr/local/php/bin/php


  4、编译安装PHP5扩展模块
-----------------------------------memcache---------------------------------------
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 ../

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

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

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

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



  5、修改php.ini文件
     手工修改: 

    查找/usr/local/webserver/php/etc/php.ini中的extension_dir = "./"
  修改为extension_dir = "/usr/local/webserver/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


     自动修改:若嫌手工修改麻烦,可执行以下shell命令,自动完成对php.ini文件的修改:

sed -i 's#extension_dir = "./"#extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/"\nextension = "memcache.so"\nextension = "pdo_mysql.so"\nextension = "imagick.so"\n#' /usr/local/php/etc/php.ini
sed -i 's#output_buffering = Off#output_buffering = On#' /usr/local/php/etc/php.ini
sed -i "s#; always_populate_raw_post_data = On#always_populate_raw_post_data = On#g" /usr/local/php/etc/php.ini


  6、配置eAccelerator加速PHP:
mkdir -p /usr/local/eaccelerator_cache
cat >>/usr/local/php/etc/php.ini<<EOF
[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″
eaccelerator.keys = “disk_only”
eaccelerator.sessions = “disk_only”
eaccelerator.content = “disk_only”
EOF


  7、创建www用户和组,以及供虚拟主机使用的目录:
/usr/sbin/groupadd www
/usr/sbin/useradd -g www www
mkdir -p /web/www
chmod +w /web/www
chown -R www:www /web/www
mkdir -p /www/html/www.lnmp.com
chmod +w /www/html/www.lnmp.com
chown -R www:www /www/html/www.lnmp.com

 

 

  8、创建php-fpm配置文件(php-fpm是为PHP打的一个FastCGI管理补丁,可以平滑变更php.ini配置而无需重启php-cgi):
  在/usr/local/webserver/php/etc/目录中创建php-fpm.conf文件:

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


  输入以下内容(如果您安装 Nginx + PHP 用于程序调试,请将以下的<value name="display_errors">0</value>改为<value name="display_errors">1</value>,以便显示PHP错误信息,否则,Nginx 会报状态为500的空白错误页):  

 

<?xml version="1.0" ?>  
<configuration>   
   
   All relative paths in this config are relative to php's install prefix     
   
   <section name="global_options">   
   
       Pid file     
       <value name="pid_file">/usr/local/php/logs/php-fpm.pid</value>   
   
       Error log file     
       <value name="error_log">/usr/local/php/logs/php-fpm.log</value>   
   
       Log level     
       <value name="log_level">notice</value>   
   
       When this amount of php processes exited with SIGSEGV or SIGBUS ...     
       <value name="emergency_restart_threshold">10</value>   
   
       ... in a less than this interval of time, a graceful restart will be initiated.     
       Useful to work around accidental curruptions in accelerator's shared memory.     
       <value name="emergency_restart_interval">1m</value>   
   
       Time limit on waiting child's reaction on signals from master     
       <value name="process_control_timeout">5s</value>   
   
       Set to 'no' to debug fpm     
       <value name="daemonize">yes</value>   
   
   </section>   
   
   <workers>   
   
       <section name="pool">   
   
           Name of pool. Used in logs and stats.     
           <value name="name">default</value>   
   
           Address to accept fastcgi requests on.     
           Valid syntax is 'ip.ad.re.ss:port' or just 'port' or '/path/to/unix/socket'     
           <value name="listen_address">127.0.0.1:9000</value>   
   
           <value name="listen_options">   
   
               Set listen(2) backlog     
               <value name="backlog">-1</value>   
   
               Set permissions for unix socket, if one used.     
               In Linux read/write permissions must be set in order to allow connections from web server.     
               Many BSD-derrived systems allow connections regardless of permissions.     
               <value name="owner"></value>   
               <value name="group"></value>   
               <value name="mode">0666</value>   
           </value>   
   
           Additional php.ini defines, specific to this pool of workers.     
           <value name="php_defines">   
               <value name="sendmail_path">/usr/sbin/sendmail -t -i</value>   
               <value name="display_errors">1</value>   
           </value>   
   
           Unix user of processes     
               <value name="user">www</value>   
   
           Unix group of processes     
               <value name="group">www</value>   
   
           Process manager settings     
           <value name="pm">   
   
               Sets style of controling worker process count.     
               Valid values are 'static' and 'apache-like'     
               <value name="style">static</value>   
   
               Sets the limit on the number of simultaneous requests that will be served.     
               Equivalent to Apache MaxClients directive.     
               Equivalent to PHP_FCGI_CHILDREN environment in original php.fcgi     
               Used with any pm_style.     
               <value name="max_children">128</value>   
   
               Settings group for 'apache-like' pm style     
               <value name="apache_like">   
   
                   Sets the number of server processes created on startup.     
                   Used only when 'apache-like' pm_style is selected     
                   <value name="StartServers">20</value>   
   
                   Sets the desired minimum number of idle server processes.     
                   Used only when 'apache-like' pm_style is selected     
                   <value name="MinSpareServers">5</value>   
   
                   Sets the desired maximum number of idle server processes.     
                   Used only when 'apache-like' pm_style is selected     
                   <value name="MaxSpareServers">35</value>       
               </value>       
           </value>   
   
           The timeout (in seconds) for serving a single request after which the worker process will be terminated     
           Should be used when 'max_execution_time' ini option does not stop script execution for some reason     
           '0s' means 'off'     
           <value name="request_terminate_timeout">0s</value>   
   
           The timeout (in seconds) for serving of single request after which a php backtrace will be dumped to slow.log file     
           '0s' means 'off'     
           <value name="request_slowlog_timeout">0s</value>   
   
           The log file for slow requests     
           <value name="slowlog">logs/slow.log</value>   
   
           Set open file desc rlimit     
           <value name="rlimit_files">65535</value>   
   
           Set max core size rlimit     
           <value name="rlimit_core">0</value>   
   
           Chroot to this directory at the start, absolute path     
           <value name="chroot"></value>   
   
           Chdir to this directory at the start, absolute path     
           <value name="chdir"></value>   
   
           Redirect workers' stdout and stderr into main error log.     
           If not set, they will be redirected to /dev/null, according to FastCGI specs     
           <value name="catch_workers_output">yes</value>   
   
           How much requests each process should execute before respawn.     
           Useful to work around memory leaks in 3rd party libraries.     
           For endless request processing please specify 0     
           Equivalent to PHP_FCGI_MAX_REQUESTS     
           <value name="max_requests">102400</value>   
   
           Comma separated list of ipv4 addresses of FastCGI clients that allowed to connect.     
           Equivalent to FCGI_WEB_SERVER_ADDRS environment in original php.fcgi (5.2.2+)     
           Makes sense only with AF_INET listening socket.     
           <value name="allowed_clients">127.0.0.1</value>   
   
           Pass environment variables like LD_LIBRARY_PATH     
           All $VARIABLEs are taken from current environment     
           <value name="environment">   
               <value name="HOSTNAME">$HOSTNAME</value>   
               <value name="PATH">/usr/local/bin:/usr/bin:/bin</value>   
               <value name="TMP">/tmp</value>   
               <value name="TMPDIR">/tmp</value>   
               <value name="TEMP">/tmp</value>   
               <value name="OSTYPE">$OSTYPE</value>   
               <value name="MACHTYPE">$MACHTYPE</value>   
               <value name="MALLOC_CHECK_">2</value>   
           </value>       
       </section>       
   </workers>       
</configuration> 


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

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start


  注:/usr/local/php/sbin/php-fpm还有其他参数,包括:start|stop|quit|restart|reload|logrotate,修改php.ini后不重启php-cgi,重新加载配置文件使用reload。


三、安装Nginx 0.8.15
  1、安装Nginx所需的pcre库:

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


  2、安装Nginx

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


  3、创建Nginx日志目录

mkdir -p /data1/logs
chmod +w /data1/logs
chown -R www:www /data1/logs


  4、创建Nginx配置文件
  ①、在/usr/local/nginx/conf/目录中创建nginx.conf文件:

rm -f /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf


  输入以下内容:

user  www www;

worker_processes 8;

error_log    /data1/logs/nginx_error.log    crit;

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

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

events
{
    use epoll;
    worker_connections 65535;
}

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

    #charset    gb2312;
           
    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 8m;
           
    sendfile on;
    tcp_nopush         on;

    keepalive_timeout 60;

    tcp_nodelay on;

    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;

    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    www.lnmp.org ;
        index index.html index.htm index.php;
        root    /web/www;

        #limit_conn     crawler    20;       
                                                       
        location ~ .*\.(php|php5)?$
                 
            #fastcgi_pass    unix:/tmp/php-cgi.sock;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index index.php;
            include fcgi.conf;
        }
       
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires            30d;
        }

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

        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    /web/www/access.log    access;
            }

    server
    {
        listen             80;
        server_name    www.lnmp.com;
        index index.html index.htm index.php;
        root    /www/html/www.lnmp.com;

        location ~ .*\.(php|php5)?$
                 
            #fastcgi_pass    unix:/tmp/php-cgi.sock;
            fastcgi_pass    127.0.0.1:9000;
            fastcgi_index index.php;
            include fcgi.conf;
        }

        log_format    wwwlogs    '$remote_addr - $remote_user [$time_local] "$request" '
                             '$status $body_bytes_sent "$http_referer" '
                             '"$http_user_agent" $http_x_forwarded_for';
        access_log    /www/html/www.lnmp.com/wwwlogs.log    wwwlogs;
    }

    server
    {
        listen    80;
        server_name    status.lnmp.com;

        location / {
             stub_status on;
             access_log     off;
        }
    }
}
 
 
  ②、在/usr/local/nginx/conf/目录中创建fcgi.conf文件:
vi /usr/local/webserver/nginx/conf/fcgi.conf


  输入以下内容: 

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;
 
  5、启动Nginx
ulimit -SHn 65535
/usr/local/nginx/sbin/nginx



四、配置开机自动启动Nginx + PHP

vi /root/run.sh


  在末尾增加以下内容:

ulimit -SHn 65535
/usr/local/php/sbin/php-fpm start
/usr/local/nginx/sbin/nginx


五、优化Linux内核参数
vi /etc/sysctl.conf


  在末尾增加以下内容:

# Add
net.ipv4.tcp_max_syn_backlog = 65536
net.core.netdev_max_backlog =  32768
net.core.somaxconn = 32768

net.core.wmem_default = 8388608
net.core.rmem_default = 8388608
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216

net.ipv4.tcp_timestamps = 0
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 2

net.ipv4.tcp_tw_recycle = 1
#net.ipv4.tcp_tw_len = 1
net.ipv4.tcp_tw_reuse = 1

net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_max_orphans = 3276800

#net.ipv4.tcp_fin_timeout = 30
#net.ipv4.tcp_keepalive_time = 120
net.ipv4.ip_local_port_range = 1024  65535
 
  使配置立即生效:
/sbin/sysctl -p



六、在不停止Nginx服务的情况下平滑变更Nginx配置
  1、修改/usr/local/nginx/conf/nginx.conf配置文件后,请执行以下命令检查配置文件是否正确:

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


  如果屏幕显示以下两行信息,说明配置文件正确:
  the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully


  2、这时,输入以下命令查看Nginx主进程号:

ps -ef | grep "nginx: master process" | grep -v "grep" | awk -F ' ' '{print $2}'


  屏幕显示的即为Nginx主进程号,例如:
  6302
  这时,执行以下命令即可使修改过的Nginx配置文件生效:

kill -HUP 6302


  或者无需这么麻烦,找到Nginx的Pid文件:

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`



七、编写每天定时切割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/logs/nginx.pid`
 

  2、设置crontab,每天凌晨00:00切割nginx访问日志

crontab -e


  输入以下内容:

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

 

八、Nginx的启动

 

kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
killall nginx
/root/run.sh


Nginx配置文件说明

一、基本配置  

#运行用户
user www www;
#启动进程
worker_processes 1;
#全局错误日志及PID文档
error_log  /web/www/logs/error.log notice;
pid        /usr/local/nginx/logs/nginx.pid;
#工作模式及连接数上限
events {
   use epoll;
   worker_connections      51200;
}
#设定http服务器,利用他的反向代理功能提供负载均衡支持
http {
   #设定mime类型
   include      mime.types;
   default_type application/octet-stream;
   #设定日志格式
   log_format main        '$remote_addr - $remote_user [$time_local]'
   '"$request" $status $bytes_sent '
   '"$http_referer" "$http_user_agent" '
   '"$gzip_ratio"';
   log_format download '$remote_addr - $remote_user [$time_local] '
   '"$request" $status $bytes_sent '
   '"$http_referer" "$http_user_agent" '
   '"$http_range" "$sent_http_content_range"';
   #设定请求缓冲
   client_header_buffer_size    1k;
   large_client_header_buffers 4 4k;
   #开启gzip模块
   gzip on;
   gzip_min_length 1100;
   gzip_buffers    4 8k;
   gzip_types      text/plain;
   output_buffers 1 32k;
   postpone_output 1460;
   #设定access log
   access_log logs/access.log main;
   client_header_timeout 3m;
   client_body_timeout    3m;
   send_timeout          3m;
   sendfile                on;
   tcp_nopush              on;
   tcp_nodelay            on;
   keepalive_timeout 65;

   #设定服务器支持shtml
   ssi on;
   ssi_silent_errors on;
   ssi_types text/shtml;
   #设定负载均衡的服务器列表
   upstream mysvr {
      #weigth参数表示权值,权值越高被分配到的几率越大
      #本机上的Squid开启3128端口
      server 192.168.8.1:3128 weight=5;
      server 192.168.8.2:80 weight=1;
      server 192.168.8.3:80 weight=6;
   }
   #设定虚拟主机
   server {
      listen          80;
      server_name    192.168.8.1 
www.lnmp.com;

      index index.html index.htm index.php;

      root /web/www;
      charset gb2312;
      #设定本虚拟主机的访问日志
      access_log logs/www.lnmp.com.access.log main;
      #假如访问 /img/*, /js/*, /css/* 资源,则直接取本地文档,不通过squid
      #假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好
      location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
      {
         expires      30d;
      }
      location ~ .*\.(js|css)?$
      {
         expires      1h;
      }

      #假如这些文档较多,不推荐这种方式,因为通过squid的缓存效果更好

       location ~ .*\.(php|php5)?$
       {      
          #fastcgi_pass  unix:/tmp/php-cgi.sock;
          fastcgi_pass  127.0.0.1:9000;
          fastcgi_index index.php;
          include fcgi.conf;
       }  
       #设置主机开启SSI支持shtml
       location ~ .*\.(shtml)?$ {
          ssi on;
          ssi_silent_errors on;
          ssi_types text/shtml;
       }
       #对 "/" 启用负载均衡
       location / {
         proxy_pass      
http://mysvr;
         proxy_redirect          off;
         proxy_set_header        Host $host;
         proxy_set_header        X-Real-IP $remote_addr;
         proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
         client_max_body_size    10m;
         client_body_buffer_size 128k;
         proxy_connect_timeout 90;
         proxy_send_timeout      90;
         proxy_read_timeout      90;
         proxy_buffer_size      4k;
         proxy_buffers          4 32k;
         proxy_busy_buffers_size 64k;
         proxy_temp_file_write_size 64k;
      }
      #设定查看Nginx状态的地址
      location /NginxStatus {
         stub_status            on;
         access_log             on;
         auth_basic             "NginxStatus";
      }
   }
}

 

 
二、高级配置

    1、查看 Nginx 运行状态

        输入地址http://192.168.8.1/NginxStatus/,输入验证帐号密码,即可看到类似如下内容: 

Active connections: 328
server accepts handled requests
9309    8982        28890
Reading: 1 Writing: 3 Waiting: 324
第一行表示现在活跃的连接数
第三行的第三个数字表示Nginx运行到

 

    2、配置虚拟主机

        在http下添加server来增加虚拟主机,代码如下 

server
        {
                listen 80;
                server_name  host1.com;
                root /myserver/host1;

                location ~ .*\.(php|php5)?$
                {
                         #fastcgi_pass  unix:/tmp/php-cgi.sock;
                         fastcgi_pass  127.0.0.1:9000;
                         fastcgi_index index.php;
                         include fcgi.conf;
                }
        }
server
        {
                listen 80;
                server_name  host2.com;
                root /myserver/host2;

                location ~ .*\.(php|php5)?$
                {
                         #fastcgi_pass  unix:/tmp/php-cgi.sock;
                         fastcgi_pass  127.0.0.1:9000;
                         fastcgi_index index.php;
                         include fcgi.conf;
                }
        }

 

 

 

    3、配置Nginx虚拟主机(防盗链及expires设置)

 

server
       {
               listen       80;
               server_name  bbs.linuxtone.org www.linuxtone.org;
               index index.html index.php index.htm;
               root  /data/www/wwwroot/lt/bbs;
               #access_log /var/log/nginx/access_bbs.redocn.com.log  combined;
               location / {
               if (!-e $request_filename) {
                         rewrite ^/archiver/((fid|tid)-[\w\-]+\.html)$   /archiver/index.php?$1 last;
                         rewrite ^/forum-([0-9]+)-([0-9]+)\.html$   /forumdisplay.php?fid=$1&page=$2 last;
                         rewrite ^/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$  /viewthread.php?tid=$1&extra=page%3D$3&page=$2

last;
                         rewrite ^/space-(username|uid)-(.+)\.html$   /space.php?$1=$2 last;
                         rewrite ^/tag-(.+)\.html$ /tag.php?name=$1 last;
                         break;
                                          }

                }

               #Preventing hot linking of images and other file types
               location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ {
                       valid_referers none blocked server_names *.linuxtone.org http://localhost;
               if ($invalid_referer) {
               rewrite   ^/   http://bbs.linuxtone.org/images/default/logo.gif;
               return   403;
                                     }
                                                               }
               # Add expires header for static content
               location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
               if (-f $request_filename) {
                  root /data/www/wwwroot/lt/bbs;
                  expires      1d;
                  break;
                  }

               }
               #support php
               location ~ .*\.php?$
               {
                       include enable_php5.conf;
               }

       }

 

 

    4、Nginx搭建下载站点限制并发数和速率

 

vi /usr/local/nginx/conf/vhosts/down.redocn.com.conf
limit_zone   one  $binary_remote_addr  10m;
server
       {
               listen       80;
               server_name  down.redocn.com;
               index index.html index.htm index.php;
               root   /data/www/wwwroot/down;
               error_page 404 /index.php;
               # redirect server error pages to the static page /50x.html
               error_page   500 502 503 504  /50x.html;
               location = /50x.html {
                root   html;
                  }
               #Zone limit
               location / {
                   limit_conn   one  1;
                   limit_rate  20k;
               }


               # serve static files
               location ~ ^/(images|javascript|js|css|flash|media|static)/  {
               root    /data/www/wwwroot/down;
               expires 30d;
                }
       }

 

 

    5、如何实现Nginx身份验证

 

mkdir /usr/local/nginx/conf/htpasswd  #创建存放密码的目录
/usr/local/apache2/bin/htpasswd -c /usr/local/nginx/conf/htpasswd/tongji admin

server
       {

              location ~ ^/(tongji)/  {
                 root    /data/www/wwwroot/count;
                 auth_basic              "LT-COUNT-TongJi";
                 auth_basic_user_file  /usr/local/nginx/conf/htpasswd/tongji;
              }

       }

 

 

    6、如何实现Nginx目录列表 

        在相关虚拟主机配置文件加入如下设置即可

 

location  /  {
    autoindex  on;
}

 

 

    7、修改Nginx的header伪装服务器

 

cd nginx-0.6.31/src/core
#define NGINX_VERSION      "1.2"
#define NGINX_VER          "LTWS/" NGINX_VERSION

 

 

      重新编译nginx即可,查看一下效果:
      [root@count ~]# curl -I http://bbs.linuxtone.org

HTTP/1.1 200 OK
Server: LTWS/1.2
Date: Mon, 23 Jun 2008 06:11:17 GMT
Content-Type: text/html; charset=gb2312
Transfer-Encoding: chunked
Connection: keep-alive
Set-Cookie: lt__sid=cJN2FT; expires=Mon, 30-Jun-2008 06:11:17 GMT; path=/
Set-Cookie: lt__onlineusernum=228; expires=Mon, 23-Jun-2008 06:16:17 GMT; path=/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值