首页 > 技术文章 > linux上安装nginx

yuanpeng-java 2018-11-22 09:44 原文

1.nginx 的安装及下载

1.1 下载 nginx  

  官方地址:http://nginx.org/  版本:1.14.1

1.2 linux 安装 nginx 所需环境

  安装 gcc:  yum install -y gcc-c++  

  安装pcre,zlib,openssl:  yum install -y pcre pcre-devel zlib zlib-devel openssl openssl-devel

1.3 linux 上安装 nginx

  上传nginx 压缩包到 linux,解压nginx  tar zxf nginx-1.14.1.tar.gz

  进入nginx 目录 cd nginx-1.14.1

  使用 configure 命令创建 makefile

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

    创建文件夹   mkdir /var/temp/nginx/client -p

  编译   make

  安装  make install

#编译和安装过程出现如下提示,无须处理,继续执行后续操作
make[1]: 离开目录“/root/nginx-1.14.1

  测试      启动 /usr/local/nginx/sbin/nginx  关闭 /usr/local/nginx/sbin/nginx -s quit  重启 /usr/local/nginx/sbin/nginx -s reload  访问(80端口) 172.19.73.20

2.nginx 上配置虚拟主机

通过 ip 地址端口不同或者域名不同,访问不同的tomcat

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

2.1  通过端口区分不同的虚拟机

server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
    server {
        listen       81;
        server_name  localhost;
        location / {
            root   html-81;
            index  index.html index.htm;
        }
    }

访问: http://172.19.73.20:81/  http://172.19.73.20:80/

2.2 通过域名区分不同的虚拟机(需要配置本地host文件)

server {
        listen       80;
        server_name  www.taobao.com;
        location / {
            root   html-taobao;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.baidu.com;
        location / {
            root   html-baidu;
            index  index.html index.htm;
        }
    }

配置本地 host 文件

172.19.73.20 www.taobao.com
172.19.73.20 www.baidu.com

3.nginx 上配置反向代理

公网 ip反向代理服务器,两个域名访问同一台服务器,服务器将域名代理到不同的 tomcat上显示不同的网页

安装两个 tomcat,分别运行在8080和8081的端口

修改端口号:vi tomcat-02/conf/server.xml

<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443"/>
<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443"/>

启动 tomcat:进入 bin 目录, sh startup.sh

查看启动状态: 进入 conf 目录,查看 tail -f catalina.out

访问:172.19.73.20:8080 /8081

反向代理服务器配置

   upstream tomcat1 {
    server localhost:8080;
    }
    upstream tomcat2 {
    server localhost:8081;
    }
    server {
        listen       80;
        server_name  www.sina.com.cn;
        location / {
            proxy_pass   http://tomcat1;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.sohu.com;
        location / {
            proxy_pass   http://tomcat2;
            index  index.html index.htm;
        }
    }

nginx 重新加载配置文件 nginx -s reload

配置域名

172.19.73.20 www.sohu.com
172.19.73.20 www.sina.com.cn

4.nginx 上配置负载均衡

一个服务由多条服务器提供,需要把负载分配到不同的服务器处理,这就是负载均衡

#weight 代表权重,假设有12条请求,有2/3的请求访问8082的端口
upstream tomcat2 {
    server 172.19.13.20:8080;
    server 172.19.13.20:8081 weight=2;
    }

测试:发送多条请求为 www.sohu.com

5.nginx 配置高可用

5.1环境配置:

两台nginx,一主一备:172.19.73.20和172.19.73.21
两台tomcat服务器:172.19.73.22、172.19.73.23

5.2 分别在一主一备上面安装 keepalive,详见在 linux 上安装 keepalive

5.3 配置 keepalive

5.4 测试 

启动初始化

服务宕机

主机宕机

 

推荐阅读