Centos6.6下的Nginx1.9.6安装和简单配置

2026-02-27 07:16:43

1、在官方网站nginx.org上下载相应的压缩包

nginx-1.9.6.tar.gz

导入到linux下

2、解压nginx-1.9.6.tar.gz

tar -zxvf nginx-1.9.6.tar.gz

创建连接

ln -sf nginx-1.9.6 nginx

3、进入nginx 输入

./configure

如果出现如下错误

checking for c compiler ... not found

请利用yum 安装gcc

yum install gcc

gcc安装完成后再次运行命令

./configure

4、如果出现新的错误

./configure: error: the HTTP rewrite module requires the PCRE library

再次利用yum源安装缺失的插件

yum install pcre-devel

再次使用命令./configure

5、如果有了新的错误

./configure:error:the HTTP gzip module requires the zlib library

则再次利用yum源安装缺失的插件

yum -y install openssl openssl-devel

再次使用命令./configure

6、配置过后在当前使用过./configure的目录下使用命令

make 

然后再输入

make install

全部完成后,nginx安装完成

7、为了方便系统使用配置环境变量,此步骤不配置也可以,完全是为了方便

打开.bashrc 文件

回到家目录  cd

打开bashrc  vi .bashrc

export NGINX_HOME=/usr/local/nginx

export PATH=$PATH:$NGINX_HOME/sbin

保存退出

引用bashrc

source  .bashrc

配置好以后,输入nginx命令

打开某个浏览器,输入网址,你就能看到welcom nginx的欢迎界面了。

8、既然nginx 已经配置成功,那么下面就需要对nginx做以简单的配置,来支持2台tomcat的负载均衡

9、首先配置2台tomcat虚拟机

我这里的配置是

192.168.0.15:8080

192.168.0.16:8080

顺便提一下,用到的nginx虚拟机的ip

192.168.0.14

10、打开nginx配置文件

vi  /usr/local/nginx/conf/nginx.conf

修改配置文件

#创建进程的用户和用户组

user      zht zht;

#服务进程数量,一般等于CPU数量

worker_processes 1;

#全局错误日志定义,建议开启error级别日志.[ debug | info | notice | warn | error | crit ]

error_log logs/error.log error;

#error_log logs/error.log  notice;

#error_log logs/error.log  info;

#记录进程ID的文件

#pid       logs/nginx.pid;

events {

    #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能.Linux建议使用epoll,FreeBSD建议使用kqueue.

    useepoll;

    #一个worker_processe允许的最近并发连接数量

   worker_connections  1024;

}

upstream site {     

   server 192.168.0.15:8080;     

   server 192.168.0.16:8080;

 }

server{  

        location / {  

#关键在这里 proxy_pass 里面必须在http://后面跟着upstream的名称

           proxy_pass http://site;  

        }  

       

}  

配置好以后,只需要再重启动nginx

nginx -s reload

然后打开浏览器,负载均衡就好了,这里需要说明的是负载均衡在默认的情况下是平均负载,如果需要详细配置,还是需要配合其他文档来参阅。

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢