资讯专栏INFORMATION COLUMN

Nginx 安装

Yu_Huang / 2072人阅读

摘要:使用双处理器的系统使用可能会造成内核崩溃高效方法,使用于内核版本及以后的系统,内核要打补丁可执行的实时信号,不常见使用于,,和使用于,为了防止出现内核崩溃的问题,有必要安装这个安全补丁。

nginx的优点[摘自互联网]
1. 处理静态文件、索引文件以及自动索引;打开文件描述符缓冲。
2. 无缓存的反向代理加速,简单的负载均衡和容错。
3. FastCGI:简单的负载均衡和容错。
4. 模块化的结构:包括 gzipping,byte ranges,chunked responses,以及SSI-filter等filter。如果由FastCGI或其他代理服务器处理单页中存在的多个SSI,则这项处理可以并行运行,而不需要相互等待。
5. 支持SSL和TLS SNI
6. Nginx 专为性能优化而开发,性能是其最重要的考量,实现上非常注重效率。它支持内核Poll模型,能经受高负载的考验,有报告表明能支持高达50000个并发连接数。感谢Nginx为我们选择了epoll and kqueue作为开发模型。
7. Nginx具有很高的稳定性。其他HTTP服务器,当遇到访问的峰值,或者有人恶意发起慢速连接时,很可能会导致服务器物理内存耗尽或频繁交换,失去响应,只能重启服务器。例如当前apache一旦上到200个以上进程,web响应速度就明显非常缓慢了。而Nginx采取了分阶段资源分配技术,使得它的CPU与内存占用率非常低。Nginx官方表示保持10000个没有活动的连接,它只占2.5M内存,所以类似DDOS这样的攻击对Nginx来说基本上是毫无用处的。就稳定性而言,Nginx比lighthttpd更胜一筹。
8. Nginx支持热部署。它的启动特别容易,并且几乎可以做到7*24不间断运行,即使运行数个月也不需要重新启动。你还能够在不间断服务的情况下,对软件版本进行升级。
9. Nginx采用master-slave模型,能够充分利用SMP的优势,且能够减少工作进程在磁盘I/O的阻塞延迟。当采用select()/poll()调用时,还可以限制每个进程的连接数。
10. Nginx代码质量非常高,代码很规范,手法成熟,模块扩展也很容易,特别值得一提的是强大的Upstream与Filter链。Upstream为诸如reverse proxy,与其他服务器通信模块的编写奠定了很好的基础。而Filter链最酷的部分就是各个filter不必等待前一个filter执行完毕。它可以把前一个filter的输出作为当前filter的输入,这有点像Unix的管道。这意味着,一个模块可以开始压缩从后端服务器发送过来的请求,且可以在模块接收完成后端服务器的整个请求之前把压缩流转向客户端。 
11. 作为邮件代理服务器:Nginx同时也是一个非常优秀的邮件代理服务器(最早开发这个产品的目的之一也是作为邮件代理服务器),Last.fm描述了成功并且美妙的使用经验。
11. Nginx采用了一些os提供的最新特性如对sendfile(Linux 2.2+),accept-filter(FreeBSD 4.1+),TCP_DEFER_ACCEPT(Linux 2.4+)的支持,从而大大提高了性能。 
事件模型
Nginx支持如下处理连接的方法(I/O复用方法),这些方法可以通过use指令指定。
1. select 标准方法
2. Poll 标准方法
3. Kueue 高效方法。使用FreeBSD 4.1+,OpenBSD 2.9+,NetBSD 2.0 和 MaxOS X。使用双处理器的MacOS X系统使用kqueue可能会造成内核崩溃
4. Epoll 高效方法,使用于Linux内核2.6版本及以后的系统,2.4内核要打补丁
5. Rtsig 可执行的实时信号,不常见 
6. /dev/poll 使用于 Solaris 7 11/99+,HP/UX 11.22+(eventport),IRIX 6.5.15+ 和 Tru64 UNIX 5.1A+
7. Eventport 使用于Solaris 10,为了防止出现内核崩溃的问题,有必要安装这个安全补丁。
安装 规划
a. 主机名:n1.web.org
b. IP地址:192.168.128.21
c. 系统:CentOS 6.5 64bit
d. 源码目录:/usr/local/src
e. 安装目录:/opt/nginx
安装
a. 所需软件包
    pcre-8.35.tar.gz
    zlib-1.2.8.tar.gz
    nginx-1.6.2.tar.gz
b. 准备工作
    [root@nginx1 ~]# yum -y install gcc gcc-c++ autoconf automake make libtool openssl openssl-devel
    [root@nginx1 ~]# useradd -M -s /sbin/nologin www
c. 安装pcre
    [root@nginx1 src]# tar -zxvf pcre-8.35.tar.gz 
    [root@nginx1 src]# cd pcre-8.35
    [root@nginx1 pcre-8.35]# ./configure --prefix=/opt/nginx/pcre
    [root@nginx1 pcre-8.35]# make && make install
d. 安装zlib
    [root@nginx1 src]# tar -zxvf zlib-1.2.8.tar.gz 
    [root@nginx1 src]# cd zlib-1.2.8
    [root@nginx1 zlib-1.2.8]# ./configure --prefix=/opt/nginx/zlib
    [root@nginx1 zlib-1.2.8]# make && make install
e. 安装nginx
    [root@nginx1 src]# tar -zxvf nginx-1.6.2.tar.gz 
    [root@nginx1 src]# cd nginx-1.6.2
    [root@nginx1 nginx-1.6.2]# more nginx.configure 
    ./configure 
    --user=www --group=www 
    --prefix=/opt/nginx/nginx 
    --with-http_stub_status_module 
    --with-http_ssl_module 
    --with-http_realip_module 
    --with-http_sub_module 
    --with-http_flv_module 
    --with-http_dav_module 
    --with-http_addition_module 
    --with-pcre=/usr/local/src/pcre-8.35 
    --with-zlib=/usr/local/src/zlib-1.2.8 
    --with-cc-opt="-O3" 
    --with-cpu-opt=opteron
    [root@nginx1 nginx-1.6.2]# make && make install
f. Nginx编译优化
    ① GCC 参数优化
    默认nginx使用的GCC的编译参数是-O。需要更加优化可以使用以下两个参数(即上面安装nginx时.configure的最后两项) --with-cc-opt="-O3" --with-cpu-opt=opteron 使得编译针对特定CPU以及增加GCC的优化。此方法仅对性能有所改善(大概增加性能1%左右)并不会有很大的性能提升,仅供参考。 
    ② 修改Nginx的header伪装服务器
    vi nginx-1.6.2/src/core/nginx.h
    #ifndef _NGINX_H_INCLUDED_
    #define _NGINX_H_INCLUDED_ 
    #define nginx_version      1005007
    #define NGINX_VERSION      "**1.5.7**"
    #define NGINX_VER          "**nginx**/" NGINX_VERSION
    #define NGINX_VAR          "NGINX"
    #define NGX_OLDPID_EXT     ".oldbin"
    #endif /* _NGINX_H_INCLUDED_ */ 
    修改加粗标记的文字为自己想要修改的文字即可。如,NGINX_VERSION 修改为5.7,NGINX_VER修改为Milo。
    用curl测试:curl -I 192.168.128.21,Server会返回Milo/5.7,而不是原来的Nginx服务器和版本号了。
    ③ Tcmalloc优化Nginx性能
    从Nginx 0.6.29 添加Feature:the ngx_google_perftools_module 以来,Nginx也可以利用Tcmalloc来提升性能。
配置[简单的初步配置]
    [root@nginx1 conf]# more nginx.conf
    user  www www;
    worker_processes  1;

    error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    pid        /var/run/nginx.pid;

    worker_rlimit_nofile 65535;
    events {
        use epoll;
        worker_connections  65535;
    }

    http {
        include       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" "$http_x_forwarded_for"";

        access_log  logs/access.log  main;

        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  0;
        keepalive_timeout  65;

        tcp_nodelay on;
        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;

        server {
            listen       80;
            server_name  web.xsl.com;
            index index.html index.htm;
            root /app/web/www;

            # location / {
            #    root   html;
            #    index  index.html index.htm;
            # }

            #error_page  404              /404.html;

            # redirect server error pages to the static page /50x.html
            #
            # error_page   500 502 503 504  /50x.html;
            # location = /50x.html {
            #    root   html;
            # }

            # proxy the PHP scripts to Apache listening on 127.0.0.1:80
            #
            #location ~ .php$ {
            #    proxy_pass   http://127.0.0.1;
            #}

            # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
            #
            #location ~ .php$ {
            #    root           html;
            #    fastcgi_pass   127.0.0.1:9000;
            #    fastcgi_index  index.php;
            #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            #    include        fastcgi_params;
            #}

            # deny access to .htaccess files, if Apache"s document root
            # concurs with nginx"s one
            #
            #location ~ /.ht {
            #    deny  all;
            #}
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        #server {
        #    listen       443 ssl;
        #    server_name  localhost;

        #    ssl_certificate      cert.pem;
        #    ssl_certificate_key  cert.key;

        #    ssl_session_cache    shared:SSL:1m;
        #    ssl_session_timeout  5m;

        #    ssl_ciphers  HIGH:!aNULL:!MD5;
        #    ssl_prefer_server_ciphers  on;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}

    }
启动脚本
    [root@nginx1 conf]# more /etc/init.d/nginx 
    #!/bin/sh
    #
    # chkconfig: - 85 15

    # processname: nginx
    # config: /opt/nginx/nginx/conf/nginx.conf
    # pidfile: /var/run/nginx.pid

    # Source function library.
    . /etc/rc.d/init.d/functions

    # Source networking configuration.
    . /etc/sysconfig/network

    # Check that networking is up.
    [ "$NETWORKING" = "no" ] && exit 0

    nginx="/opt/nginx/nginx/sbin/nginx"
    prog=$(basename $nginx)

    NGINX_CONF_FILE="/opt/nginx/nginx/conf/nginx.conf"

    lockfile=/var/lock/subsys/nginx

    start() {
        [ -x $nginx ] || exit 5
        [ -f $NGINX_CONF_FILE ] || exit 6
        echo -n $"Starting $prog: "
        daemon $nginx -c $NGINX_CONF_FILE
        retval=$?
        echo
        [ $retval -eq 0 ] && touch $lockfile
        return $retval
    }

    stop() {
        echo -n $"Stopping $prog: "
        killproc $prog -QUIT
        retval=$?
        echo
        [ $retval -eq 0 ] && rm -f $lockfile
        return $retval
    }

    restart() {
        configtest || return $?
        stop
        start
    }

    reload() {
        configtest || return $?
        echo -n $"Reloading $prog: "
        killproc $nginx -HUP
        RETVAL=$?
        echo
    }

    force_reload() {
        restart
    }

    configtest() {
        $nginx -t -c $NGINX_CONF_FILE
    }

    rh_status() {
        status $prog
    }

    rh_status_q() {
        rh_status > /dev/null 2>&1
    }

    case "$1" in
        start)
            rh_status_q && exit 0
            $1
            ;;
        stop)
            rh_status_q || exit 0
            $1
            ;;
        restart|configtest)
            $1
            ;;
        reload)
            rh_status_q || exit 7
            $1
            ;;
        force-reload)
            force_reload
            ;;
        status)
            rh_status
            ;;
        condrestart|try-restart)
            rh_status_q || exit 0
            ;;
        *)
            echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
            exit 2
    esac

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/39185.html

相关文章

  • nginx服务器详细安装过程(使用yum 和 源码包两种安装方式,并说明其区别)

    摘要:网上看别人写的服务器配置,有的是源码包安装的,有的时安装的。通过源码包编译安装的软件,通常都放在包名路径下。正则表达式使用在指令和模块中。 网上看别人写的 nginx 服务器配置 ,有的是源码包安装的,有的时 yum 安装的。如果是新手,可能会有疑问,这两种安装方式有什么区别?我应该使用哪种方式?系统里可以两个都安装可以吗?怎么卸载?等等问题,那么在这里,我做下总结,详细介绍下这两种方...

    waruqi 评论0 收藏0
  • Nginx笔记-0-Centos环境下安装

    摘要:如果发现运行只有一行回显,可能是当前端口被占用,使用端口号,默认,如果打印结果为两行或以上,即端口被占用,需要修改配置文件的端口号再重新运行。 概述 记录一下 Nginx 通过安装包以及通过源代码安装两种方式。目标是第一次接触 Nginx 的人也能看懂直接用。 一. 使用安装包配置 Tip: 这种安装方式比较简单,官方文档也说得比较清楚详细。这里搭建的环境是 Centos7, 可以sy...

    Rindia 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<