美文网首页nginx
nginx 安装和基本配置

nginx 安装和基本配置

作者: 待汝豪杰只是凡夫 | 来源:发表于2017-06-15 00:55 被阅读0次

    contos 7
    下载
    <pre>
    wget http://nginx.org/download/nginx-1.12.0.tar.gz
    </pre>
    解压
    <pre>
    tar -zxvf nginx-1.12.0.tar.gz
    </pre>
    编译安装
    首先:
    <pre>
    ./configure
    </pre>
    安装的时候报错处理
    安装 gcc& gc++:
    <pre>
    yum -y install gcc gcc-c++ autoconf automake
    </pre>
    安装 pcre:
    <pre>
    yum -y install pcre pcre-devel
    </pre>
    安装 zlib: ​
    <pre>
    yum -y install zlib zlib-devel
    </pre>
    其次:
    <pre>
    make
    </pre>
    然后:
    <pre>
    make install​
    </pre>
    启动Nginx命令:
    <pre>
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    </pre>
    简单配置
    1.首先我的服务器上跑了2个tomcat
    一个跑在8080端口,一个跑在8089端口,2个index.html略不同
    2.备份 nginx.conf 文件为新文件 nginx.conf.base​ (防止修改出错无法还原)命令​:
    <pre>
    cp/usr/local/nginx/conf/nginx.conf/usr/local/nginx/conf/nginx.conf.base
    </pre>
    3.​修改nginx.conf
    ​在http节点下添加upstream节点
    a.配置1:按照请求到达时序按权重进行负载均衡(如下:8080端口的服务收到请求数量是8089的两倍)
    <pre>
    upstream mayanyu{
    server 127.0.0.1:8080 weight=2;
    server 127.0.0.1:8089 weight=1;
    }
    </pre>
    b.配置2,按照IP进行负载均衡(可以解决session共享问题)
    <pre>
    upstream mayanyu{
    ip_hash;
    server 127.0.0.1:8080;
    server 127.0.0.1:8089;
    }
    </pre>
    在server下的location下添加一行:
    <pre>
    proxy_pass http://mayanyu;​
    </pre>
    测试配置文件并启动或者重启Nginx
    测试​配置文件:
    <pre>
    /usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
    </pre>
    <pre>
    nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
    nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
    </pre>
    启动:
    <pre>
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    </pre>
    重启:
    <pre>
    /usr/local/nginx/sbin/nginx -s reload
    </pre>
    访问你的服务器80端口:http://ip:80
    刷新之后显示的页面不同,说明负载均衡成功了(我服务器上的2个tomcat的index.html不同)​

    相关文章

      网友评论

        本文标题:nginx 安装和基本配置

        本文链接:https://www.haomeiwen.com/subject/msxyqxtx.html