Nginx环境的搭建

源码安装

不同发行版使用的安装程序都不同,所以官方提供了源代码,我们需要将原代码编译后再安装。

源程序使用C语言开发,所以需要安装C语言的编译环境。

 

安装Nginx环境和第三方支持库

 

yum install -y gcc-c++

 

yum install -y pcre pcre-devel

 

yum install -y zlib zlib-devel

 

yum install -y openssl openssl-devel

Nginx配置

vim /usr/local/nginx/conf/nginx.conf

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }

虚拟主机

一台物理服务器上运行多个网站。每一个网站都是一个“虚拟”出来的主机。

如下可定位到两个不同位置的资源去:

 server {
        listen       81;
        server_name  localhost;

        location / {
            root   nvpiao;
            index  index.html index.htm;
        }
    }
    
    server {
        listen       82;
        server_name  localhost;

        location / {
            root   prepiao;
            index  index.html index.htm;
        }
    }

 

 基于域名的实现方式

server {
        listen       80;
        server_name  www.nvpiao.com;

        location / {
            root   nvpiao;
            index  index.html index.htm;
        }
    }
    
    server {
        listen       80;
        server_name  www.prepiao.com;

        location / {
            root   prepiao;
            index  index.html index.htm;
        }
    }

反向代理

配置反向代理时要注意格式,{}内的每一行结束时都需要写“;

proxy_passproxy代表代理服务器,pass表示这个代理服务器起请求转发的作用。

upstreamNginx实现负载均衡的一种内置算法名称。

 

upstream sina {
        server 192.168.159.251:8280;
    }

    server {
        listen       80;
        server_name  www.sina.com;

        location / {
            proxy_pass http://sina;
            index  index.html index.htm index.jsp;
        }
    }

 

使用默认权重值,多个参与负载均衡的服务器大致平均分配负载

 upstream baidu {
        server 192.168.159.251:8180;
        server 192.168.159.251:8480;
    }

配置权重值,权重值大的服务器会有较大几率被选中:

server 192.168.159.251:8180 weight=5;

负载均衡的其他技术实现:

Linux virtual server

官网见:http://www.linuxvirtualserver.org/

 

posted @ 2017-09-28 16:27  西北野狼  阅读(155)  评论(0)    收藏  举报