导航

不许杀人,也不许放火

« 收工睡觉换回CentOS4 »

nginx简明教程

小结一下,写个简明教程,带负载均衡和php解释
下载
wget http://sysoev.ru/nginx/nginx-0.5.35.tar.gz

解压后安装到/usr/local/nginx
./configure --with-http_ssl_module  --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_stub_status_module --prefix=/usr/local/nginx
make
make install

创建用户,建立web根目录
useradd -d /dev/null -s /sbin/nologin www
cd /
mkdir web
cd web
mkdir htdocs
cd /
chown -R www:www web
cd /usr/local/nginx
mkdir var
cd ..
chown -R www:www nginx
cd nginx

配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
内容示例为
user  www www;
worker_processes 2;
error_log  /usr/local/nginx/logs/error.log;
pid        /usr/local/nginx/var/nginx.pid;
worker_rlimit_nofile 51200;
events
{
       use epoll;
       worker_connections 51200;
}
http
{
       include       conf/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" ';
       access_log  /usr/local/nginx/logs/access.log  main;

       keepalive_timeout 30;
     
       tcp_nopush        on;
       tcp_nodelay       off;
 
       upstream test {
           server 127.0.0.1:81 fail_timeout=60;
           server 127.0.0.1:82 fail_timeout=60;
       }

       limit_zone  httplimit $binary_remote_addr 10m;

       server
       {
           listen       80;
           client_max_body_size 50M;
           server_name 
www.2tutu.com 192.168.1.33;
           index index.php index.htm index.html;
           root  /web/htdocs;
           limit_conn httplimit 8;

           location / {
               index index.php index.htm index.html;
               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 50m;
               client_body_buffer_size 256k;
               proxy_connect_timeout 30;
               proxy_send_timeout 30;
               proxy_read_timeout 30;
               proxy_buffer_size 4k;
               proxy_buffers 4 32k;
               proxy_busy_buffers_size 64k;
               proxy_temp_file_write_size 64k;
               proxy_next_upstream  error timeout invalid_header http_500 http_503 http_404;
               proxy_max_temp_file_size 128m;
               if (!-f $request_filename) {
                   proxy_pass
http://test;
                   break;
               }
              

          }    
         
          location ~* /404.html$ {
              access_log        off;
              expires           1s;
          }

          location ~ .*\.php?$
          {
               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 50m;
               client_body_buffer_size 256k;
               proxy_connect_timeout 30;
               proxy_send_timeout 30;
               proxy_read_timeout 30;
               proxy_buffer_size 4k;
               proxy_buffers 4 32k;
               proxy_busy_buffers_size 64k;
               proxy_temp_file_write_size 64k;
               proxy_next_upstream  error timeout invalid_header http_500 http_503 http_404;
               proxy_max_temp_file_size 128m;

               if (!-f $request_filename) {
                   proxy_pass
http://test;
                   break;
               }
               include conf/fcgi.conf;
               fastcgi_index index.php;
               fastcgi_pass  127.0.0.1:9999;
          }

          location /status {
                    stub_status             on;
                    access_log              off;
          }

       }
}

以上配置:配置一个侦听端口为80的web服务,主机头为www.2tutu.com或者192.168.1.33,并且限制了同一IP的并发连接数为8。如果本地WEB根目录/web/htdocs下有内容,则提供本地内容,否则proxy到127.0.0.1:81或127.0.0.1:82(进行简单负载均衡)。

php解释通过fastcgi实现,php编译参数中一定要有--enable-fastcgi ,比如:
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/local --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-debug --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-force-cgi-redirect --enable-mbstring --with-mcrypt

然后用lighttpd带的spawn-fcgi启动php-cgi处理进程,把lighttpd复制到/usr/local/php/bin下,然后
/usr/local/php/bin/spawn-fcgi -a 127.0.0.1 -p 9999 -C 32 -u www -f /usr/local/php/bin/php-cgi
侦听9999端口,与nginx配置中的fastcgi_pass  127.0.0.1:9999;对应。

创建启动脚本
vi startn
内容示例为
NGINXDIR=/usr/local/nginx
if [ -f $NGINXDIR/var/nginx.pid ]; then
       echo "Maybe already running:" `cat $NGINXDIR/var/nginx.pid`
else
       $NGINXDIR/sbin/nginx -c $NGINXDIR/conf/nginx.conf
fi

创建停止脚本
vi stopn
内容示例为
NGINXDIR=/usr/local/nginx
if [ -f $NGINXDIR/var/nginx.pid ]; then
       kill `cat $NGINXDIR/var/nginx.pid`
fi

这时候运行/usr/local/nginx/startn即启动服务,可以在/web/htdocs下放一个phpinfo()的测试文件试试了。

  • quote 2.alen
  • good 你的波可我早发现就好了。 找的好辛苦。555
  • 2008-5-20 12:10:11 回复该留言
  • quote 3.DR
  • 博主的文章写得不错啊 希望多出点Nginx的文档 谢谢你能提供这么好的参考文档
    我参照着把网站也改成Nginx的了 呵呵!
  • 2008-8-5 16:27:43 回复该留言

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Search

控制面板

首页

最近发表

友情链接

Powered By Z-Blog