Nginx安装、配置虚拟主机、反向代理、负载均衡

1.   nginx安装

下载nginx:

官方网站:

http://nginx.org/

使用的版本是1.8.0版本。

Nginx提供的源码。

1.1. 要求的安装环境

1、需要安装gcc的环境。yum install gcc-c++

2、第三方的开发包。

n  PCRE

       PCRE(Perl Compatible Regular Expressions)是一个Perl库,包括 perl 兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库。

扫描二维码关注公众号,回复: 2433282 查看本文章

yum install -y pcre pcre-devel

注:pcre-devel是使用pcre开发的一个二次开发库。nginx也需要此库。

n  zlib

       zlib库提供了很多种压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库。

yum install -y zlib zlib-devel

n  openssl

       OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序供测试或其它目的使用。

       nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库。

yum install -y openssl openssl-devel

1.2. 安装步骤

第一步:把nginx的源码包上传到linux系统

第二步:解压缩

[root@localhost ~]# tar zxf nginx-1.8.0.tar.gz

第三步:使用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

注意:启动nginx之前,上边将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

[root@localhost sbin]# mkdir /var/temp/nginx/client -p

第四步:make

第五步:make install

1.3. 启动nginx

进入sbin目录

[root@localhost sbin]# ./nginx

关闭nginx:

[root@localhost sbin]# ./nginx -s stop

推荐使用:

[root@localhost sbin]# ./nginx -s quit

重启nginx:

1、先关闭后启动。

2、刷新配置文件:

[root@localhost sbin]# ./nginx -s reload

1.4. 访问nginx

默认是80端口。

注意:是否关闭防火墙。

2.   配置虚拟主机

就是在一台服务器启动多个网站。

如何区分不同的网站:

1、域名不同

2、端口不同

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

Nginx的配置文件:

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

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

 
 

一个server节点就是一个虚拟主机

 

 


    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

 
 

Html是nginx安装目录下的html目录

 

 


        location / {

            root   html;

            index  index.html index.htm;

        }

    }

}

可以配置多个server,配置了多个虚拟主机。

添加虚拟主机:

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

    server {

        listen       81;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-81;

            index  index.html index.htm;

        }

    }

}

重新加载配置文件

[root@localhost nginx]# sbin/nginx -s reload

2.2. 通过域名区分虚拟主机

2.2.1.    什么是域名

域名就是网站。

www.baidu.com

www.taobao.com

www.jd.com

Tcp/ip

Dns服务器:把域名解析为ip地址。保存的就是域名和ip的映射关系。

一级域名:

Baidu.com

Taobao.com

Jd.com

二级域名:

www.baidu.com

Image.baidu.com

Item.baidu.com

三级域名:

1.Image.baidu.com

Aaa.image.baidu.com

一个域名对应一个ip地址,一个ip地址可以被多个域名绑定。

本地测试可以修改hosts文件。

修改window的hosts文件:(C:\Windows\System32\drivers\etc)

可以配置域名和ip的映射关系,如果hosts文件中配置了域名和ip的对应关系,不需要走dns服务器。

2.2.2.    Nginx的配置

#user  nobody;

worker_processes  1;

#error_log  logs/error.log;

#error_log  logs/error.log  notice;

#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '

    #                  '$status $body_bytes_sent "$http_referer" '

    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;

    #tcp_nopush     on;

    #keepalive_timeout  0;

    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       80;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html;

            index  index.html index.htm;

        }

    }

    server {

        listen       81;

        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-81;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  www.taobao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-taobao;

            index  index.html index.htm;

        }

    }

    server {

        listen       80;

        server_name  www.baidu.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            root   html-baidu;

            index  index.html index.htm;

        }

    }

}

域名的配置:

192.168.25.148 www.taobao.com

192.168.25.148 www.baidu.com

3.   反向代理

3.1. 什么是反向代理

正向代理

反向代理:

 

 

反向代理服务器决定哪台服务器提供服务。

返回代理服务器不提供服务器。也是请求的转发。

3.2. Nginx实现反向代理

两个域名指向同一台nginx服务器,用户访问不同的域名显示不同的网页内容。

两个域名是www.sian.com.cn和www.sohu.com

nginx服务器使用虚拟机192.168.101.3

第一步:安装两个tomcat,分别运行在8080和8081端口。

第二步:启动两个tomcat。

第三步:反向代理服务器的配置

upstream tomcat1 {

                         server 192.168.25.148:8080;

    }

    server {

        listen       80;

        server_name  www.sina.com.cn;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat1;

            index  index.html index.htm;

        }

    }

    upstream tomcat2 {

                         server 192.168.25.148:8081;

    }

    server {

        listen       80;

        server_name  www.sohu.com;

 

        #charset koi8-r;

 

        #access_log  logs/host.access.log  main;

 

        location / {

            proxy_pass   http://tomcat2;

            index  index.html index.htm;

        }

    }

第四步:nginx重新加载配置文件

第五步:配置域名

在hosts文件中添加域名和ip的映射关系

192.168.25.148 www.sina.com.cn

192.168.25.148 www.sohu.com

 

4.   负载均衡

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

 upstream tomcat2 {

       server 192.168.25.148:8081;

       server 192.168.25.148:8082;

  }

可以根据服务器的实际情况调整服务器权重。权重越高分配的请求越多,权重越低,请求越少。默认是都是1

 upstream tomcat2 {

       server 192.168.25.148:8081;

       server 192.168.25.148:8082 weight=2;

    }

猜你喜欢

转载自www.cnblogs.com/limn/p/9383476.html