搭建nginx服务器,虚拟主机,反向代理

1 搭建Nginx服务器

1.1 问题

在IP地址为192.168.10. 5的主机上安装部署Nginx服务,并可以将Nginx服务器升级到更高版本,要求编译时启用如下功能:

  • SSL加密功能
  • 查看服务器状态信息功能
  • 设置Nginx账户及组名称均为nginx

然后客户端访问页面验证Nginx Web服务器:

  • 使用火狐浏览器访问
  • 使用curl访问

1.2 方案

使用2台RHEL6虚拟机,其中一台作为Nginx服务器(192.168.10.5)、另外一台作为测试用的Linux客户机(192.168.10.205)

安装Nginx需要先安装nginx-1.7.10版本,在升级至nginx-1.8.0版本时,需要使用如下参数:

  • with-http_stub_status_module:可以提供服务器状态信息
  • with-http_ssl_module:提供SSL加密功能
  • user:指定账户
  • group:指定组

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:构建Nginx服务器

1)使用源码包安装nginx软件包

[root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel        //安装常见依赖包
[root@svr5 ~]# useradd –s /sbin/nologin nginx
[root@svr5 ~]# wget http://nginx.org/download/nginx-1.6.3.tar.gz [root@svr5
~]# tar -zxvf nginx-1.7.10.tar.gz [root@svr5 ~]# cd nginx-1.7.10 [root@svr5 nginx-1.7.10]# ./configure > --prefix=/usr/local/nginx //指定安装路径 > --user=nginx //指定用户 > --group=nginx //指定组 > --with-http_stub_status_module //开启状态统计功能 > --with-http_ssl_module //开启SSL加密功能 .. .. nginx path prefix: "/usr/local/nginx" nginx binary file: "/usr/local/nginx/sbin/nginx" nginx configuration prefix: "/usr/local/nginx/conf" nginx configuration file: "/usr/local/nginx/conf/nginx.conf" nginx pid file: "/usr/local/nginx/logs/nginx.pid" nginx error log file: "/usr/local/nginx/logs/error.log" nginx http access log file: "/usr/local/nginx/logs/access.log" nginx http client request body temporary files: "client_body_temp" nginx http proxy temporary files: "proxy_temp" nginx http fastcgi temporary files: "fastcgi_temp" nginx http uwsgi temporary files: "uwsgi_temp" nginx http scgi temporary files: "scgi_temp" [root@svr5 nginx-1.7.10]# make && make install //编译并安装

2)启用nginx服务

[root@nginx ~]# /usr/local/nginx/sbin/nginx –c /usr/local/nginx/conf/nginx.conf

nginx服务默认通过TCP 80端口监听客户端请求:

[root@nginx ~]# netstat  -anptu  |  grep nginx
tcp        0        0 0.0.0.0:80        0.0.0.0:*        LISTEN        10441/nginx

3)为Nginx Web服务器建立测试首页文件

Nginx Web服务默认首页文档存储目录为/usr/local/nginx/html/,在此目录下建立一个名为index.html的文件:

[root@svr5 ~]# cat  /usr/local/nginx/html/index.html
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>

步骤二:升级Nginx服务器

1)编译新版本nginx软件

[root@svr5 ~]# wget http://nginx.org/download/nginx-1.8.0.tar.gz
[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz [root@svr5 ~]# cd nginx-1.8.0 [root@svr5 nginx-1.8.0]# ./configure > --prefix=/usr/local/nginx > --user=nginx > --group=nginx > --with-http_stub_status_module > --with-http_ssl_module [root@svr5 nginx-1.8.0]# make

2)备份老的nginx主程序,并使用编译好的新版本nginx替换老版本

[root@svr5 nginx-1.8.0]# cd /usr/local/nginx/sbin/
[root@svr5 sbin]# mv nginx  nginxold                        //备份旧版本
[root@svr5 sbin]# cd /root/ nginx-1.8.0/objs/
[root@svr5 objs]# cp nginx  /usr/local/nginx/sbin/            //拷贝新版本
[root@svr5 objs]# cd /root/nginx-1.0.5
[root@svr5 nginx-1.8.0]# make upgrade                        //升级
/usr/local/nginx/sbin/nginx -t
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
kill -USR2 `cat /usr/local/nginx/logs/nginx.pid`
sleep 1
test -f /usr/local/nginx/logs/nginx.pid.oldbin
kill -QUIT `cat /usr/local/nginx/logs/nginx.pid.oldbin`
[root@svr5 ~]# /usr/local/nginx/sbin/nginx –v                //查看版本

步骤三:客户端访问测试

1)分别使用浏览器和命令行工具curl测试服务器页面

[root@client ~]# firefox http://192.168.10.5
[root@client ~]# curl http://192.168.10.5

2 用户认证及访问控制

2.1 问题

沿用练习一,通过调整Nginx服务端配置,实现以下目标:

  1. 访问Web页面需要进行用户认证
  2. 用户名为:tom,密码为:123456
  3. 网站根目录下首页文档所有主机均可以访问
  4. 为网站创建二级目录/test,并生成index.html文档
  5. test目录下的页面仅192.168.10.205可以访问

2.2 方案

通过Nginx实现Web页面的认证与访问控制,需要修改Nginx配置文件,在location容器中添加allow及deny语句实现访问控制,添加auth语句实现用户认证。最后使用htpasswd命令创建用户及密码即可。

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改Nginx配置文件

1)修改/usr/local/nginx/conf/nginx.conf

[root@pc205 ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       80;
        server_name  localhost;
        auth_basic "Input Password:";        //认证提示符
        auth_basic_user_file pass.txt;        //认证密码文件
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /test {
                allow 192.168.10.205;            //访问控制,仅192.168.10.205可以访问
                deny all;                    //拒绝所有
            index  index.html index.htm;
        }

2)创建二级页面目录,并生成index.html文件

[root@svr5 ~]# mkdir /usr/local/nginx/html/test
[root@svr5 ~]# echo “test” > /usr/local/nginx/html/test/index.html

3)生成密码文件,创建用户及密码

使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。

[root@svr5 ~]# htpasswd -cm /usr/local/nginx/conf/pass.txt tom
New password: 
Re-type new password: 
Adding password for user tom

4)重启Nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx

步骤二:客户端测试

1)登录192.168.4.205主机进行测试

[root@client ~]# firefox http://192.168.10.5                    //输入密码后可以访问
[root@client ~]# firefox http://192.168.10.5/test            //输入密码后可以访问

2)登录非192.168.4.205的其他任意主机测试

[root@client ~]# firefox http://192.168.10.5                //输入密码后可以访问
[root@client ~]# firefox http://192.168.10.5/test        //输入密码后无法访问

3 Nginx虚拟主机

3.1 问题

沿用练习二,配置基于域名的虚拟主机,实现以下目标:

  1. 实现两个基于域名的虚拟主机,域名分别为www.aaaaa.com和bbs.aaaaa.com
  2. 域名为bbs.aaaaa.com的Web服务仅允许192.168.10.205访问
  3. 对域名为bbs.aaaaa.com的站点进行用户认证,用户名称为tom,密码为123456
  4. 对域名为www.aaaaa.com的站点进行SSL加密

3.2 方案

修改Nginx配置文件,添加server容器实现虚拟主机功能;对于需要进行访问控制的虚拟主机添加allow和deny语句;对于需要进行用户认证的虚拟主机添加auth认证语句;对于需要进行SSL加密处理的站点添加ssl相关指令。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改配置文件

1)修改Nginx服务配置,添加相关虚拟主机配置如下

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
server {
        listen       192.168.10.5:80;          //端口
        server_name  bbs.aaaaa.com;        //域名
allow 192.168.10.205;                //访问控制,仅192.168.10.205可以访问
deny all;                            //拒绝所有 
auth_basic "Input Password:";        //认证提示符
        auth_basic_user_file pass.txt;        //认证密码文件
location / {
            root   bbs;                        //指定网站根路径
            index  index.html index.htm;
        }
       
}
    server {
        listen       192.168.10.5:443;
        server_name  www.aaaaa.com;
ssl    on;                                //开启SSL
ssl_certificate        cert.pem;     //指定证书文件 
ssl_certificate_key    cert.key;     //指定私钥文件 
ssl_session_timeout  5m; 
ssl_protocols  SSLv2 SSLv3 TLSv1; 
ssl_ciphers  HIGH:!aNULL:!MD5; 
ssl_prefer_server_ciphers   on; 
location / { 
root   www;                     //指定网站根路径
index  index.html index.htm;
}
}

2)生成私钥与证书

[root@svr5 ~]# openssl genrsa -out cert.key 2048        //生成私钥
[root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem  //生成证书
[root@svr5 ~]# cp {cert.key,cert.pem}  /usr/local/nginx/conf

3)创建网站根目录及对应首页文件

[root@svr5 ~]# mkdir /usr/local/nginx/{www,bbs}
[root@svr5 ~]# echo “www” > /usr/local/nginx/www/index.html
[root@svr5 ~]# echo “bbs” > /usr/local/nginx/bbs/index.html

4)重启nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx

步骤二:客户端测试

1)修改/etc/hosts文件,进行域名解析

[root@client ~]# vim /etc/hosts
192.168.10.5    www.aaaaa.com  bbs.aaaaa.com

2)登录192.168.4.205主机进行测试

[root@client ~]# firefox http://bbs.aaaaa.com            //输入密码后可以访问
[root@client ~]# firefox https://www.aaaaa.com            //信任证书后可以访问

3)登录非192.168.4.205的其他任意主机测试

[root@client ~]# firefox http://bbs.aaaaa.com            //无法访问
[root@client ~]# firefox https://www.aaaaa.com            //信任证书后可以访问

4 Nginx反向代理

4.1 问题

使用Nginx实现Web反向代理功能,实现如下功能:

  • 后端Web服务器两台,可以使用httpd实现
  • Nginx采用轮询的方式调用后端Web服务器
  • 两台Web服务器的权重要求设置为不同的值
  • 最大失败次数为2,失败超时时间为30秒

4.2 方案

使用3台RHEL6虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,IP地址分别为172.16.0.254和192.168.10.5,两台Web服务器IP地址分别为192.168.10.205和192.168.10.200。测试主机IP地址为172.16.0.1。

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置Nginx服务器,添加服务器池,实现反向代理功能

1)修改/usr/local/nginx/conf/nginx.conf配置文件

[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
http {
.. ..
upstream webserver {
                server 192.168.10.205 weight=2 max_fails=2 fail_timeout=10;
                server 192.168.10.200 weight=1 max_fails=2 fail_timeout=10;
        }
.. ..
server {
        listen        80;
        server_name  www.aaaaa.com;
            location / {
            proxy_pass http://webserver;
        }
}

2)重启nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx –s stop
[root@svr5 ~]# /usr/local/nginx/sbin/nginx

步骤二:部署实施后端Web服务器

1)部署后端Web1服务器

后端Web服务器可以简单使用yum方式安装httpd实现Web服务,为了可以看出后端服务器的不同,可以将两台后端服务器的首页文档内容设置为不同的内容。

[root@web1 ~]# yum  -y  install  httpd
[root@web1 ~]# echo192.168.10.205” > /var/www/html/index.html
[root@web1 ~]# service httpd start

2)部署后端Web2服务器

[root@web2 ~]# yum  -y  install  httpd
[root@web2 ~]# echo192.168.10.200” > /var/www/html/index.html
[root@web2 ~]# service httpd start

步骤三:客户端测试

1)修改客户端hosts文件

[root@client ~]# vim /etc/hosts
.. ..
172.16.0.254        www.aaaaa.com

2)使用浏览器访问代理服务器测试轮询效果

[root@client ~]# curl http://www.aaaaa.com            //使用该命令多次访问查看效果
原文地址:https://www.cnblogs.com/linyouyi/p/9829944.html