02 March 2016

相信做web开发的没有几个不熟悉nginx,所以这里就不介绍它,也不对他逆天的性能进行追捧了。 另外打算用yum安装nginx也不用看本文。yum可以方便的安装nginx没错,而且也能用的好好的。 但是如果你想在安装nginx过程中做更多自定义的工作,可以继续阅读下去。比如指定安装目录,指定要安装的插件等等。 [摘自百度百科]

下载解压

  • 本文使用1.6.2的版本。

      wget http://nginx.org/download/nginx-1.6.2.tar.gz
        
      tar -zxf nginx-1.6.2.tar.gz
    

安装

  • 准备目录,比如要安装到/opt/nginx/目录下

    mkdir /opt/nginx

  • 安装依赖模块及其源:

    yum install pcre pcre-devel openssl openssl-devel pcre zile-devel gcc

  • 进入刚才的解压的目录进行安装

    /configure –prefix=/opt/nginx –with-http_stub_status_module –with-http_realip_module –with-http_gzip_static_module –with-http_ssl_module make make install

安装服务

  • 新建服务文件:/etc/init.d/nginx并输入下面内容

      #!/bin/sh
      #
      # nginx - this script starts and stops the nginx daemon
      #
      # chkconfig:   - 85 15
      # description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
      #               proxy and IMAP/POP3 proxy server
      # processname: nginx
      # config:      /opt/nginx/conf/nginx.conf
      # pidfile:     /opt/nginx/logs/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/sbin/nginx"
      prog=$(basename $nginx)
        
      NGINX_CONF_FILE="/opt/nginx/conf/nginx.conf"
        
      [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
        
      lockfile=/var/lock/subsys/nginx
        
      make_dirs() {
         options=`$nginx -V 2>&1 | grep 'configure arguments:'`
         for opt in $options; do
             if [ `echo $opt | grep '.*-temp-path'` ]; then
                 value=`echo $opt | cut -d "=" -f 2`
                 if [ ! -d "$value" ]; then
                     # echo "creating" $value
                     mkdir -p $value && chown -R $user $value
                 fi
             fi
         done
      }
        
      start() {
          [ -x $nginx ] || exit 5
          [ -f $NGINX_CONF_FILE ] || exit 6
          #make_dirs
          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
          sleep 1
          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
    
  • 赋予权限并且将ngix加入到linux服务中和随机启动列表

       chmod +x /etc/init.d/nginx
       chkconfig --add nginx
       chkconfig nginx on
    

启动并测试nginx

  • 启动nginx很简单:service nginx start 即可。
  • 使用curl命令进行测试:curl http://localhost
  • 测试如果有问题,检查80端口是否开启。

附上一个nginx.conf的典型配置

user  nobody nobody;
worker_processes  2;

error_log  logs/error.log  info;
pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    types {
        text/html                             html htm;
        text/css                              css;

        image/gif                             gif;
        image/jpeg                            jpeg jpg;
        image/png                             png;
        image/x-icon                          ico;
        image/x-jng                           jng;
        image/svg+xml                         svg svgz;

        application/x-javascript              js;
    }

    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $cookie_n [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" $http_cdn_src_ip $cookie_fs $request_time';

    access_log  logs/access.log  main;

    sendfile        on;

    keepalive_timeout  65;
    gzip  on;
    gzip_types text/plain text/xml text/css
        text/comma-separated-values text/csv
        text/javascript application/x-javascript application/javascript
        application/json
        application/atom+xml;

    upstream app {
        server 127.0.0.1:8080;
    }

    server {
        listen       80;
        server_name  api;

        charset utf-8;

        access_log  logs/data.access.log main;

        error_page  404              /404.html;
        error_page  500 502 503 504  /50x.html;

        root /opt/data-engine/current/webapp/;

        # http://wiki.nginx.org/HttpStubStatusModule
        location /server-status {
          stub_status on;
          access_log  off;
          allow 127.0.0.0/8;
          allow 10.0.0.0/8;
          allow 192.168.0.0/16;
          deny all;
        }

        location ~* ^/(WEB-INF)/? {
          return 404;
        }

        location / {
          index  index.html index.htm;
          try_files $uri @app;
        }

        location @app {
          proxy_ignore_client_abort on;
          proxy_pass http://app;
          proxy_set_header X-Proxy nginx;
          proxy_read_timeout 600s;
        }
    }
}




blog comments powered by Disqus
Fork me on GitHub