1.Nginx 安装
1.1下载nginx的压缩包并放到相应的目录下解压
[root@V_shenjingbingweb01 local]# tar -zxvf nginx-1.10.2.tar.gz
1.2进入conf修改nginx.Conf文件
[root@V_shenjingbingweb01 nginx]# cd conf
[root@V_shenjingbingweb01 conf]# vi nginx.conf
修改以下内容:
#添加使用的用户和用户组
user nginx nginx;
worker_processses 2;
#error_log的位置
error_log /usr/local/nginx/logs/nginx_error.log;
#pid的位置
pid /usr/local/nginx/logs/nginx.pid;
events{
#连接的最大线程数
worker_connections 65535;
}
1.3下载openssl-1.0.2l.zip压缩包,放到和nginx同一个目录下,解压并编译
[root@V_shenjingbingweb01 local]# unzip openssl-1.0.2l.zip
[root@V_shenjingbingweb01 local]# cd openssl-1.0.2l
[root@V_shenjingbingweb01 openssl-1.0.2l]# ./config
[root@V_shenjingbingweb01 openssl-1.0.2l]# .make
[root@V_shenjingbingweb01 openssl-1.0.2l]# make install
1.4下载pcre-8.40.zip压缩包,放到和nginx同一个目录下,解压并编译
[root@V_shenjingbingweb01 local]# unzip pcre-8.40.zip
[root@V_shenjingbingweb01 local]# cd pcre-8.40
[root@V_shenjingbingweb01 pcre-8.40]# ./configure
[root@V_shenjingbingweb01 pcre-8.40]# make
[root@V_shenjingbingweb01 pcre-8.40]# make install
1.5下载zlib-1.2.11.zip压缩包,放到和nginx同一个目录下,解压并编译
[root@V_shenjingbingweb01 local]# unzip zlib-1.2.11.zip
[root@V_shenjingbingweb01 local]# cd zlib-1.2.11
[root@V_shenjingbingweb01 zlib-1.2.11]# ./configure
[root@V_shenjingbingweb01 zlib-1.2.11]# make
[root@V_shenjingbingweb01 zlib-1.2.11]# .make install
1.6切换到nginx目录下,编译
[root@V_shenjingbingweb01 nginx]#./configure ./configure --prefix=/usr/local/nginx-1.10.2 --sbin-path=//usr/local/nginx-1.10.2/sbin/nginx --conf-path=/usr/local/nginx-1.10.2/conf/nginx.conf --error-log-path=/usr/local/nginx-1.10.2/logs/error.log --pid-path=/usr/local/nginx-1.10.2/logs/nginx.pid --lock-path=/var/run/nginx.lock --with-pcre=../pcre-8.40 --with-openssl=../openssl-1.0.2l/openssl-1.0.2l --with-zlib=../zlib-1.2.11 --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-file-aio --with-http_v2_module
[root@V_shenjingbingweb01 nginx]#make
[root@V_shenjingbingweb01 nginx]#make install
1.7切换到sbin目录,启动nginx
[root@V_shenjingbingweb01 ~]# cd /usr/local/nginx/sbin/
[root@V_shenjingbingweb01 sbin]# ./nginx
1.8测试输入curl http://localhost:80,如果出现Welcom to nginx 的页面,说明nginx安装成功
[root@V_shenjingbingweb01 sbin]# curl http://localhost:80
1.9设置nginx的全局性
[root@V_shenjingbingweb01 sbin]#cp ngixn /usr/sbin/
#下边的这种方式就可以启动nginx了
[root@V_shenjingbingweb01 ~]# nginx
2配置nginx+tomcat的负载均衡
2.1进入nginx下的conf目录,修改nginx.Conf文件,添加以下内容
[root@V_shenjingbingweb01 nginx]# cd conf
[root@V_shenjingbingweb01 conf]# vi nginx.conf
在http的大括号里加入以下内容
upstream tomcats{
server 195.203.6.51:8090;
server 95.203.6.51:8091;
}
然后在location /{}里加入以下内容:
root html;
index index.html index.htm;
proxy_pass http://tomcats;
proxy_redirect default;
2.2配置完成后,在拥有nginx的机器上访问curl http://localhost:80 就可以随机访问6.51上任意一个tomcat了
[root@V_shenjingbingweb01 ~]# curl http://localhost:80
#此处是我把tomcat的页面修改的简单了,直接显示tomcat1,更方便观看
tomcat1
[root@V_shenjingbingweb01 ~]# curl http://localhost:80
#此处是我把tomcat的页面修改的简单了,直接显示tomcat2,更方便观看
tomcat2
3总结
在安装nginx的时候会遇到很多问题,比如编译时候参数的设置,把需要的参数都设置好,需要的第三方模块也要下载好,放到参数中,其次就是安装过程中如果报错是有关软连接的错误,我们可以建立软连接来解决问题,还有就是nginx的默认端口是80,如果该端口被占用,我呢可以到nginx.conf文件中去修改为其他端口。