免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1397 | 回复: 0
打印 上一主题 下一主题

nginx简明教程 [复制链接]

论坛徽章:
2
丑牛
日期:2013-09-29 09:47:222015七夕节徽章
日期:2015-08-21 11:06:17
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-03-23 20:26 |只看该作者 |倒序浏览

小结一下,写个简明教程,带负载均衡和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
内容示例为
[color="#0000ff"]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;
[color="#0000ff"]       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;
       }
[color="#0000ff"]       limit_zone  httplimit $binary_remote_addr 10m;
[color="#0000ff"]       server
       {
           listen       80;
           client_max_body_size 50M;
           server_name
[color="#0000ff"]www.2tutu.com
[color="#0000ff"]192.168.1.33;
           index index.php index.htm index.html;
           root  /web/htdocs;
           limit_conn httplimit 8;
[color="#0000ff"]           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
[color="#0000ff"]http://test
[color="#0000ff"];
                   break;
               }
              
[color="#0000ff"]          }   
         
          location ~* /404.html$ {
              access_log        off;
              expires           1s;
          }
[color="#0000ff"]          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;
[color="#0000ff"]               if (!-f $request_filename) {
                   proxy_pass
[color="#0000ff"]http://test
[color="#0000ff"];
                   break;
               }
               include conf/fcgi.conf;
               fastcgi_index index.php;
               fastcgi_pass  127.0.0.1:9999;
          }
[color="#0000ff"]          location /status {
                    stub_status             on;
                    access_log              off;
          }
[color="#0000ff"]       }
}
以上配置:配置一个侦听端口为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配置中的[color="#0000ff"]fastcgi_pass  127.0.0.1:9999;对应。
创建启动脚本
vi startn
内容示例为
[color="#0000ff"]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
内容示例为
[color="#0000ff"]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()的测试文件试试了。
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4206/showart_505507.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP