安装必须插件:

yum install gcc-c++ autoconf automake

yum install zlib zlib-devel opensslopenssl--devel pcre pcre-devel

 

安装nginx

./configure

make

make install

 

关闭防火墙或添加配置项:

service iptables stop

chkconfig iptables off

 

检查配置文件是否正确:

/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf

 

 

 

启动nginx

cd /usr/local/nginx/sbin

./nginx

 

查询进程号:

ps -ef | grep nginx

停止进程:

/usr/local/nginx/sbin/nginx -s quit

 

重启:

/usr/local/nginx/sbin/nginx -s reload

 

测试:

测试端口:netstat -na|grep 80

浏览器中测试:http://ip:80


修改Nginx的配置文件nginx.conf,内容如下,实现反向代理负载均衡:

#user  nobody;
worker_processes  2;

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

events {
    worker_connections  10240;
}


http {   
include mime.types;
default_type  application/octet-stream;
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;
tcp_nodelay on;

 upstream  192.168.2.62 {
                  server 192.168.2.62:8081;
                  server 192.168.2.62:8082;     
        }
   
     server {

           listen 80;
           
           server_name 192.168.2.62;
           location / {
           proxy_pass http://192.168.2.62;
           proxy_set_header  X-Real-IP  $remote_addr;
}
}
}


设置Nginx自动启动,启动脚本如下:


#!/bin/bash
# nginx Startup script for the Nginx HTTP Server
# chkconfig: - 85 15
# description: Nginx is a high-performance web and proxy server.
# It has a lot of features, but it's not for everyone.
# processname: nginx
# pidfile: /usr/local/nginx/logs/nginx.pid
# config: /usr/local/nginx/conf/nginx.conf

nginxd=/usr/local/nginx/sbin/nginx
nginx_config=/usr/local/nginx/conf/nginx.conf

RETVAL=0
prog="nginx"
# Start nginx daemons functions.

start() {

if [ -e /usr/local/nginx/logs/nginx.pid ];then
   echo "nginx already running...."
   exit 1
fi

   echo -n "Starting $prog: "
   $nginxd -c ${nginx_config}
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && touch /var/lock/subsys/nginx
   echo -n "nginx starting done"
   return $RETVAL
}

# Stop nginx daemons functions.

stop() {

if [ ! -e /usr/local/nginx/logs/nginx.pid ];then
   echo "nginx not running...."
   exit 1
fi

   echo -n "Stopping $prog: "
   $nginxd -s quit
   sleep 2s
   RETVAL=$?
   echo
   [ $RETVAL = 0 ] && rm -f /var/lock/subsys/nginx /usr/local/nginx/logs/nginx.pid
}

# See how we were called.

case "$1" in

start)
        start
        ;;
stop)
        stop
        ;;

restart)
        stop
        start
        ;;

*)

        echo $"Usage: $prog {start|stop|restart}"
        exit 1

esac
exit $RETVAL