Nginx安装及配置详解

  • 安装。
    (我的环境是centos 6.5)
    首先保证编译环境:
    yum -y install gcc gcc-c++ automake autoconf libtool make 

   然后需要安装
pcre、zlib、openssl。如果有,就不需要安装。(可能可以通过yum安装,暂未试过)
  三者安装方式基本一致,下载源码解压,configure然后make && make install即可,此处不赘述
  附网址:
  pcre-8.30.tar.gz
  http://nchc.dl.sourceforge.net/project/pcre/pcre/8.30/pcre-8.30.tar.gz

  zlib-1.2.8.tar.gz
  http://zlib.net/zlib-1.2.8.tar.gz

  openssl-1.0.2a.tar.gz
  http://www.openssl.org/source/openssl-1.0.2a.tar.gz
 
    安装nginx:
    tar zxvf nginx-x.x.xx.tar.gz
    cd nginx-x.x.xx
    ./configure   (如果不指定任何参数,会被默认安装到/usr/local/nginx下)
     安装时需要提供pcre库的地址,只需要--with-pcre=就可以解决,否则会报错。
    
    然后

    make &&  make install


当然configure脚本是支持很多选项的,接下来将对其的选项一一做出分析。

--prefix=        nginx安装路径,若无,则默认为/usr/local/nginx
--sbin-path=        nginx可执行文件安装路径,只能是安装时指定,若无,则默认为/sbin/nginx
--conf-path=        在没有给定-c选项下默认的nginx.conf的路径,若无,则默认为/conf/nginx.conf
--pid-path=        在nginx.conf中没有指定pid指令的情况下,默认的nginx.pid的路径。若无,则默认为/logs>nginx.pid
--lock-path=        nginx.lock文件的路径
--error-log-path=        在nginx.conf中没有指定error_log指令的情况下,默认的错误日志的路径。若无,则默认为/logs/error.log
--http-log-path=        在nginx.conf中没有指定access_log指令的情况下,默认的访问日志的路径。若无,则默认为/logs/access.log
--http-client-body-temp-path=PATH          指定http客户端请求缓存文件存放目录的路径
--http-proxy-temp-path=PATH          指定http反向代理缓存文件存放目录的路径
--http_fastcgi-temp-path        指定hhtp FastCGI缓存文件存放目录的路径
--user=        在nginx.conf中没有指定user指令的情况下,默认的nginx使用的用户。若无,则默认为nobody
--group=        在nginx.conf中么有指定user指令的情况下,默认的nginx使用的租。若无,则默认为nobody
--builddir=DIR        指定编译的目录
--add-module=PATH        添加一个在指定路径中能找到的第三方模块



--with-rtsig_module        启用rtsig模块
--with-select_module(--without-select_module)        允许或不允许开启select模式,如果configure没有找到更合适的模式,比如:kqueue(sun os)、epoll(linux kernel 2.6+)、rtsig(实时信号)或/dev/poll(一种类似select的模式,底层实现与select基本相同,都是采用轮训方法),select模式将是默认安装模式。
--with-poll_module(--without-poll_module)        允许或不允许开启poll模式,如果没有更适合的模式,比如 :kqueue(sun os)、epoll(linux kernel 2.6+) ,则可以开启。
--with-http_ssl_module        开启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"页
--with-http_perl_module          启用ngx_http_perl_module
--with-perl_modules_path=PATH          指定perl模块的路径
--with-mail        启用IMAP4/POP3/SMTP代理模块
--with-mail_ssl_module        启用ngx_mail_ssl_module
--with-cc=PATH        指定c编译器的路径
--with-cpp=PATH        指定c预处理器的路径

--with-cpu-opt=CPU        为特定的CPU编译,有效的值包括:pentium、pentiumpro、pentium3、pentium4、athlon、opteron、amd64、sparc32、sparc64、ppc64
--with-pcre=DIR        指定PCRE库源代码的路径
--with-pcre-opt=OPTIONS        设置PCRE的额外编译选项
--with-md5=DIR        设置MD5库的源代码路径
--with-md5-opt=OPTIONS        设置MD5库的额外编译选项
--with-md5-asm        使用MD5汇编源码
--with-sha1=DIR        设置SHA1库的源码路径
--with-sha1-opt=OPTIONS        设置SHA1库的额外编译选项
--with-sha1-asm        使用SHA1库的汇编源码
--with-zlib=DIR        设置zlib库的源代码路径
--with-zlib-opt=OPTIONS        设置zlib库的额外编译选项
--with-zlib-asm=CPU        zlib针对CPU的优化,有效的值包括:pentiu、pentiumpro
--with-openssl=DIR        设置OPENSSL库的源代码路径
--with-openssl-opt=OPTIONS        设置OPENSSL库的额外编译选项
--with-debug        启用调试日志


--without-http        禁用http server
--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
--without-pcre        禁止PCRE库的使用,同时也会禁止HTTP rewrite模块,在"location"配置指令中的正则表达式也需要PCRE


在不同的版本之间,选项可能有一些变化,请使用./configure --help命令来检查当前的选项列表。






  • nginx.conf配置。

【code  3-1】
#    使用的用户和组
user    www    www;
#    指定工作衍生进程数(一般等于cpu总核数的两倍,例如两个四核CPU,则总核数为8)
worker_processes 8;
#    指定错误日志存放路径,错误日志记录级别可选项为:[    debug    |    info    |    notice    |    warn    |    error    |    crit    ]
error_log    /data1/logs/nginx_error.log    crit;
#    指定pid存放的路径
pid    /usr/local/webserver/nginx/nginx.pid;
#    指定文件描述符数量
worker_rlimit_nofile    51200;

events
{
    #    使用的网络I/O模型,linux系统推荐采用epoll模型,FreeBSD系统推荐采用kqueue模型
    use epoll;
    #    允许的连接数
    worker_connections 51200;
}

http
{
    include     mime.types;
    default_type     application/octet-stream;
    #     设置使用的字符集,如果一个网站有多种字符集,请不要随便设置,应让程序员在html代码中通过meta标签设置
    #     charset gb2313;
     
    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压缩
    gzip on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_http_version 1.1;
    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.yourdomain.com    yourdomain.com;
        index    index.html    index.htm;
        root    /data0/htdocs;
        
        # limit_conn    crawler    20;
        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    /data1/logs/access.log    access;
    }
}

通过上述nginx.conf配置文件示例,可以看出nginx.conf配置文件结构主要由以下部分组成:

【code 3-2】
……
events
{
……
}

http
{
……
    server
    {
    ……
    }

    server
    {
    ……
    }
……
}


以上内容部分摘自并整理于——
实战Nginx:取代Apache的高性能web服务器


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29773961/viewspace-1389042/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/29773961/viewspace-1389042/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值