Nginx 安装及配置

作者 :
免费
  • 正文
  • Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器。 Nginx 是由 Igor Sysoev 为俄罗斯访问量第二的 Rambler.ru 站点开发的,第一个公开版本0.1.0发布于2004年10月4日。其将源代码以类BSD许可证的形式发布,因它的稳定性、丰富的功能集、示例配置文件和低系统资源的消耗而闻名。2011年6月1日,nginx 1.0.4发布。

    本文介绍一下它的安装及配置过程

    官方网站
    http://www.nginx.org/

    一、依赖的程序

    1. gzip module requires zlib library
    2. rewrite module requires pcre library
    3. ssl support requires openssl library

    二、安装
    ./configure
    make
    make install

    默认安装的路径是/usr/local/nginx

    更多的安装配置
    ./configure --prefix=/usr/local/nginx
    --with-openssl=/usr/include (启用ssl)
    --with-pcre=/usr/include/pcre/ (启用正规表达式)
    --with-http_stub_status_module (安装可以查看nginx状态的程序)
    --with-http_memcached_module (启用memcache缓存)
    --with-http_rewrite_module (启用支持url重写)
    三、启动及重启
    启动:nginx
    重启:kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
    测试配置文件:nginx -t

    简单吧,安装,启动都比较方便。

    四、配置文件
    conf\nginx.conf

    #运行用户
    user  nobody nobody;
    #启动进程
    worker_processes  5;
    #全局错误日志及PID文件
    error_log  logs/error.log notice;
    pid        logs/nginx.pid;
    #工作模式及连接数上限
    events {
      #工作模式有:select(标准模式),poll(标准模式),kqueue(高效模式,适用FreeBSD 4.1+, OpenBSD 2.9+, NetBSD 2.0 and MacOS X),
      #epoll(高效模式,本例用的。适用Linux 2.6+,SuSE 8.2,),/dev/poll(高效模式,适用Solaris 7 11/99+, HP/UX 11.22+ (eventport), IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+)
      use epoll;
      worker_connections      1024;
    }
    #设定http服务器,利用它的反向代理功能提供负载均衡支持
    http {
      #设定mime类型
      include      conf/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    10k;
      large_client_header_buffers  4 4k;
      
      #开启gzip模块,要求安装gzip 在运行./config时要指定
      gzip on;
      gzip_min_length  1100;
      gzip_buffers    4 8k;
      gzip_types      text/plain;
      output_buffers  1 32k;
      postpone_output  1460;
      
      #设定访问日志
      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;
      
      #设定负载均衡的服务器列表
      upstream backserver {
      #weigth参数表示权值,权值越高被分配到的几率越大
      #本例是指在同一台服务器,多台服务器改变ip即可
      server 127.0.0.1:8081 weight=5;
      server 127.0.0.1:8082;
      server 127.0.0.1:8083;
      }
      #设定虚拟主机,默认为监听80端口,改成其他端口会出现问题
      server {
        listen         80;
        server_name    wmphp.com www.wmphp.com;
        charset utf8;
        #设定本虚拟主机的访问日志
        access_log  logs/test.com.log  main;
        #如果访问 /images/*, /js/*, /css/* 资源,则直接取本地文件,不用转发。但如果文件较多效果不是太好。
        location ~ ^/(images|js|css)/  {
            root    /usr/local/testweb;
            expires 30m;
        }
        
        #对 "/" 启用负载均衡
        location / {
           proxy_pass      http://wmphp;
           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状态的地址,在运行./config 要指定,默认是不安装的。
        location /NginxStatus {
           stub_status            on;
           access_log              on;
           auth_basic              "NginxStatus";
           #是否要通过用户名和密码访问,测试时可以不加上。conf/htpasswd 文件的内容用 apache 提供的 htpasswd 工具来产生即可       
           #auth_basic_user_file  conf/htpasswd;
        }
    }
    
    详细的说明请看:http://wiki.nginx.org
    END
    如本资源侵犯了您的权益,请联系投诉邮箱admin@wmphp.com进行举报!我们将在收到邮件的1个小时内处理完毕。 本站仅为平台,发布的资源均为用户投稿或转载!所有资源仅供参考学习使用,请在下载后的24小时内删除,禁止商用! Wmphp.com(完美源码)助力正版,如您有自己的原创产品,可以联系客服投稿,代理出售! Wmphp.com(完美源码)客服QQ:136882447 Wmphp.com(完美源码)商务电话(仅对企业客户/个人用户):15120086569 (微信同步) 请注意:本站不提供任何免费的技术咨询服务,为了节约时间,下载前 请确认自己会技术
    完美源码 » Nginx 安装及配置
    3488+

    本站勉强运行

    3665+

    用户总数

    690+

    资源总数

    0+

    今日更新

    2024-4-8

    最后更新时间