• 构建Nginx服务器、用户认证、Nginx虚拟主机、Nginx安全虚拟主机、Nginx反向代理


    ###################################################

    Nginx服务器

    Web服务器对比

    UnixLinux平台下

    -ApacheAginxLighttpd----------php/python

    -TomcatIBM WebSphereJboss-----java

    Windows平台下

    -微软公司的IISinternet information server

    Nginx“engine x”

    -是一个高性能的HTTP和反向代理服务器,同时也是一个IMAP/POP3/SMTP

    -官方网站:http://nginx.org(有所有的功能可以查看)

    ########################################

    软件小、运行快、并发高

    Nginx(web服务器)

    nginx是模块化软件

    nginx100功能】

    依赖包,红帽光盘一般都有一个devel的标志

    模块设计的软件nginx

    100功能    网站查看或./configure --help | grep with/without

    默认配置20个功能

    tar  -xf   nginx

    ./configure --with-XXX   启用某个功能

                      --without-XXX   停用某个功能

    http_ssl_module  

    cd /usr/local/nginx

    conf 配置文件

    logs 日志和pid文件

    html 页面

    sbin 程序

    ln -s /usr/local/nginx/sbin/nginx    /usr/sbin/-------tab快捷键的位置

    killall   varnishd

    Nginx 进程管理

    -V 查看编译参数   版本号 、  --prefix=...with-http_ssl_module

    -t 测试默认配置文件(语法) :系统启动和关闭时会自动检测   

    -c 指定配置文件:默认conf/configure...

    nginx  启动服务

    nginx   -s stop  关闭服务

    nginx   -s reload  加载新的配置文件但不会重起,服务必须是开着的

    #########################################

    1 案例1:搭建、升级Nginx服务器

    步骤一:构建Nginx服务器

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

    [root@svr5 ~]# yum –y install gcc pcre-devel openssl-devel    //安装常见依赖包

    [root@svr5 ~]# useradd –s /sbin/nologin nginx

    [root@svr5 ~]# tar  -xf   nginx-1.7.10.tar.gz

    [root@svr5 ~]# cd  nginx-1.7.10

    [nginx-1.7.10]#./configure--prefix=/usr/local/nginx    --with-http_ssl_module   //指定安装路径

    [root@svr5 ~]# make && make install    //编译并安装

    [root@svr5 ~]#ln -s /usr/local/nginx/sbin/nginx    /usr/sbin/     

    [root@svr5 ~]#nginx

    [root@svr5 ~]# netstat  -anptu  |  grep nginx/80

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

    [root@client ~]# curl http://192.168.4.5

    步骤二:升级Nginx服务器

    更新升级nginx(不要make  install会覆盖重要文件 )

    下载1.9

    tar -xf nginx.1.9

    cd nginx.1.9

    ./configure

    make

    备份老的程序

    cp objs/nginx  /usr/local/nginx/sbin/

    nginx.conf配置文件框架全局设置用户进程并发量

    [root@svr5 ~]# tar  -zxvf   nginx-1.9.0.tar.gz

    [root@svr5 ~]# cd nginx-1.9.0

    [root@svr5 nginx-1.9.0]# ./configure    --prefix=/usr/local/nginx   --with-http_ssl_module

    [root@svr5 nginx-1.9.0]# make

    [root@svr5 ~]#cd /opt/lnmp_soft/

    [root@svr5 ~]#rm -rf nginx-1.8.0

    [root@svr5 ~]#tar -xf  nginx-1.8.0.tar.gz

    [root@svr5 ~]#cd nginx-1.8.0/

    [root@svr5 nginx-1.9.0]#./configure --with-http_ssl_module     /路径默认是/usr/local/nginx

    [root@svr5 nginx-1.9.0]#make     /c语言编译成二进制程序

    将新程序替换掉老程序,老程序备份

    [root@svr5 ~]#mv /usr/local/nginx/sbin/nginx     /usr/local/nginx/sbin/nginx.old

    [root@svr5 ~]# cp /opt/lnmp_soft/nginx-1.8.0/objs/nginx    /usr/local/nginx/sbin/

    [root@svr5 ~]#ln -s /usr/local/nginx/sbin/nginx    /usr/sbin/

    [root@svr5 ~]#nginx     –V          //查看版本

    [root@client ~]# firefox http://192.168.4.5

    [root@client ~]# curl http://192.168.4.5

    #########################################

    2 案例2:用户认证

    1.添加认证配置

    全局配置 :进程worker_processes  1默认、并发worker_connections  1024默认、日志logs/error.log默认、用户

    日志:/usr/local/nginx/logs/error.log

    步骤一:修改Nginx配置文件

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

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

    .. ..

    server {

            listen       80;

            server_name  www.a.com;

            auth_basic  "input passwd:";        //认证提示符

            auth_basic_user_file  "/usr/local/nginx/pass";        //认证密码文件

            location / {

                root   html;

                index  index.html index.htm;

            }}

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

    -c 创建密码文件[create]  -m[加密密码]md5

    # yum -y install httpd-tools

    #htpasswd  -cm  /usr/local/nginx/pass    tom     /c创建新文件

    #cat /usr/local/nginx/pass

    #htpasswd  -m  /usr/local/nginx/pass    chen    /m表示md5加密

    3)重启Nginx服务

    [root@client ~]# nginx  –s  reload

    [root@client ~]#vim /etc/hosts

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

    [root@client ~]#curl -u tom:123 http://www.a.com

    #########################################

    3 案例3:Nginx虚拟主机

    虚拟主机:域名、端口、IP(希缺)

    一个server一个虚拟主机

    <virtualhost  192.168.4.11:80>

    http{  server{

    listen  192.168.2.11:80;

    server_name  www.a.com;

    }

    server{

    listen 192.168.4.11:80;

    server_name  www.b.com;

    } }

    ctrl + v    x

    ############################

    步骤一:修改配置文件

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

    [root@svr5 ~]# mkdir   /usr/local/nginx/web

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

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

    [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

    86

    server {

            listen       80;     

            server_name  www.b.com;   

    location / {

                root   web;           //指定网站根路径

                index  index.html index.htm;

            } }

    重启nginx服务

    [root@svr5 ~]#nginx  –s  reload   

    [root@client ~]# vim /etc/hosts

    [root@client ~]# firefox http://www.a.com            //输入密码后可以访问

    #########################################

    4案例4:Nginx安全虚拟主机

    加密网站HTTPS

    http协议是明文协议【apache,nginx,iis

    加密算法

    对称算法:AES,DES      适用于单机,【windows里的RAR加密】不适合网络加码

    非对称算法:RSA,DSA    网站加密只能用非对称

    信息摘要:md5(被破解了),sha128,sha256    做数据完整性校验

    111    111  对称

    123    u0a  非对称

    密码弱---破解:暴力,字典passwd123456...

    linux自带字典[a-z]  cat /usr/share/dict/words  | wc  -l

    md5sum  a.txt   校验码只有文件内容被修改了才会变,文件名称变了也不会变

    e940d6d18c462220554e1782c73eceb4  chen.txt

    for  i  in  `ls  /etc/chen`       

    do

    md5sum  $i >> md5-1.log

    done

     md5-1.log对比md5-2.log

    云盘秒传md5

    https://www.tmall.com

    用户名,密码 [插件--添加例外的脚本]

    www.12306.cn[下载证书,导入证书  word版说明书]

    ######################################

    http+ssl        RSA非对称

    步骤1.生成私钥证书

    [root@svr5 ~]# cd  /usr/local/nginx/conf

    [root@svr5 ~]# openssl genrsa  -out  my.key  openssl  genrsa > my.key      

    [root@svr5 ~]# openssl req -new -x509  -key my.key -out my.crt  //生成证书

    国家名字:CN 省份,城市,公司名称,部门名称,主机名,邮箱

    步骤2.修改配置文件 调用私钥和证书实现网站的加密

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

       

     server {

            listen  443 ssl;               //开启SSL

            server_name  www.c.com;

    ssl_certificate        my.crt;     //指定证书文件

    ssl_certificate_key    my.key;     //指定私钥文件

    ssl_session_timeout  5m;

    ssl_protocols  SSLv2 SSLv3 TLSv1;

    ssl_ciphers  HIGH:!aNULL:!MD5;

    ssl_prefer_server_ciphers   on;

    location / {

    root   html;                     //指定网站根路径

    index  index.html index.htm;

    }}

    步骤3.重起配置测试

    #nginx  -s  reload

    #vim /etc/hosts

    [root@client ~]# firefox https://www.c.com         //信任证书后可以访问

    ##########################################

    4 案例4:Nginx反向代理

    nginx 调度器,不缓存,负载均衡,web高可用(默认算法是轮询)

    自带健康检查web1/web2轮询

    实验4:集群调度器(负载均衡)

    --------------------------------------------------------------

    client eth0 192.168.4.100

    ---------------------------------------------------------------

    Proxy:  eth0 192.168.4.5

    eth1 192.168.2.5

    --------------------------------------------------------------

    Web1: eth1 192.168.2.100

    ---------------------------------------------------------------

    Web2: eth1 192.168.2.200

    -----------------------------------------------------------------

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

    1)部署后端Web1服务器

    [root@web1 ~]# yum  -y  install  httpd

    [root@web1 ~]# echo “192.168.2.100” > /var/www/html/index.html

    [root@web1 ~]# systemctl restart httpd

    2)部署后端Web2服务器

    [root@web2 ~]# yum  -y  install  httpd

    [root@web2 ~]# echo “192.168.2.200” > /var/www/html/index.html

    [root@web2 ~]# systemctl restart httpd

    步骤二:定义网站集群webs,配置Nginx服务器,添加服务器池,实现反向代理功能

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

    [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

    .. ..

    http {

    .. ..

      

    upstream webs {         /名称任意

    ip_hash;       /可注释掉

      server 192.168.2.100   weight=2  max_fails=2   fail_timeout=10  down;

    server 192.168.2.200;

            }

    server {

            listen        80;

            server_name  www.a.com;

                location / {

                proxy_pass http://webs;

    root  html;

    ...

            }}

    2)重启nginx服务

    [root@svr5 ~]#nginx –s reload

    [root@client ~]#vim /etc/hosts

    192.168.4.5        www.a.com

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

    [root@client ~]# curl http://www.a.com  

    [root@web1 ~]# systemctl stop httpd

    [root@client ~]#curl  http://192.168.4.5

    [root@client ~]# curl http://www.a.com     //使用该命令多次访问查看效果

    集群权重:weight=2默认为1 (次),权重越高,访问压力越大。

    最大失败次数:max_fails=2  只连两遍

    失败超时时间:fail_timeout=30   30秒后再连一下

    标记主机宕机:down

    相同的客户端访问相同的服务:ip_hash (重复登录用同一台服务器,不再轮询)

    Top 

     

    案例1:搭建Nginx服务器 

    案例2:用户认证 

    案例3Nginx虚拟主机 

    案例4Nginx反向代理 

    1 案例1:搭建Nginx服务器

    1.1 问题

    IP地址为192.168.4.5的主机上安装部署Nginx服务,并可以将Nginx服务器,要求编译时启用如下功能:

    SSL加密功能

    设置Nginx账户及组名称均为nginx

    可选项:Nginx服务器升级到更高版本。

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

    使用火狐浏览器访问

    使用curl访问

    1.2 方案

    使用2台RHEL6虚拟机,其中一台作为Nginx服务器(192.168.4.5)、另外一台作为测试用的Linux客户机(192.168.4.100),如图-1所示。

    -1

    安装nginx-1.8.0版本时,需要使用如下参数:

    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 ~]# tar  -xf   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_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@svr5 ~]# /usr/local/nginx/sbin/nginx

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

    [root@svr5 ~]# 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 ~]# tar  -zxvf   nginx-1.9.0.tar.gz

    [root@svr5 ~]# cd nginx-1.9.0

    [root@svr5 nginx-1.9.0]# ./configure   

    > --prefix=/usr/local/nginx   

    > --user=nginx   

    > --group=nginx  

    > --with-http_ssl_module

    [root@svr5 nginx-1.9.0]# make            

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

    [root@svr5 nginx-1.9.0]# mv /usr/local/nginx/sbin/nginx  

    >/usr/local/nginx/sbin/nginxold

    [root@svr5 nginx-1.9.0]# cp objs/nginx  /usr/local/nginx/sbin/    //拷贝新版本

    [root@svr5 nginx-1.9.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.4.5

    [root@client ~]# curl http://192.168.4.5

    2 案例2:用户认证

    2.1 问题

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

    访问Web页面需要进行用户认证

    用户名为:tom,密码为:123456

    2.2 方案

    通过Nginx实现Web页面的认证,需要修改Nginx配置文件,在配置文件中添加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;

            }

      }

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

    使用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

    3)重启Nginx服务

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

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

    步骤二:客户端测试

    1)登录192.168.4.100客户端主机进行测试

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

    3 案例3:Nginx虚拟主机

    3.1 问题

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

    实现两个基于域名的虚拟主机,域名分别为www.tarena.combbs.tarena.com

    对域名为bbs.tarena.com的站点进行用户认证,用户名称为tom,密码为123456

    对域名为www.tarena.com的站点进行SSL加密

    3.2 方案

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

    3.3 步骤

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

    步骤一:修改配置文件

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

    [root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf

    .. ..

    server {

            listen       80;          //端口

            server_name  bbs.tarena.com;        //域名

    auth_basic "Input Password:";        //认证提示符

            auth_basic_user_file pass.txt;        //认证密码文件

    location / {

                root   html;                    //指定网站根路径

                index  index.html index.htm;

            }

           

    }

        server {

            listen  443 ssl;                    //开启SSL

            server_name  www.tarena.com;

    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 ~]# cd /usr/local/nginx/conf

    [root@svr5 ~]# openssl genrsa -out cert.key                            //生成私钥

    [root@svr5 ~]# openssl req -new -x509 -key cert.key -out cert.pem  //生成证书

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

    [root@svr5 ~]# mkdir /usr/local/nginx/www

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

    4)重启nginx服务

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

    步骤二:客户端测试

    1)修改客户端主机192.168.4.100的/etc/hosts文件,进行域名解析

    [root@client ~]# vim /etc/hosts

    192.168.4.5    www.tarena.com  bbs.tarena.com

    2)登录192.168.4.100客户端主机进行测试

    [root@client ~]# firefox http://bbs.tarena.com            //输入密码后可以访问

    [root@client ~]# firefox https://www.tarena.com            //信任证书后可以访问

    4 案例4:Nginx反向代理

    4.1 问题

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

    后端Web服务器两台,可以使用httpd实现

    Nginx采用轮询的方式调用后端Web服务器

    两台Web服务器的权重要求设置为不同的值

    最大失败次数为1,失败超时时间为30

    4.2 方案

    使用4台RHEL7虚拟机,其中一台作为Nginx代理服务器,该服务器需要配置两块网卡,IP地址分别为192.168.4.5和 192.168.2.5,两台Web服务器IP地址分别为192.168.2.100和192.168.2.200。客户端测试主机IP地址为 192.168.4.100。如图-2所示。

    -2

    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.2.100 weight=1 max_fails=2 fail_timeout=10;

                    server 192.168.2.200 weight=2 max_fails=2 fail_timeout=10;

            }

    .. ..

    server {

            listen        80;

            server_name  www.tarena.com;

                location / {

                proxy_pass http://webserver;

            }

    }

    2)重启nginx服务

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

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

    1)部署后端Web1服务器

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

    [root@web1 ~]# yum  -y  install  httpd

    [root@web1 ~]# echo “192.168.2.100” > /var/www/html/index.html

    [root@web1 ~]# systemctl restart httpd

    2)部署后端Web2服务器

    [root@web2 ~]# yum  -y  install  httpd

    [root@web2 ~]# echo “192.168.2.200” > /var/www/html/index.html

    [root@web2 ~]# systemctl restart httpd

    步骤三:客户端测试

    1)修改客户端hosts文件

    [root@client ~]# vim /etc/hosts

    .. ..

    192.168.4.5        www.tarena.com

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

    [root@client ~]# curl http://www.tarena.com            //使用该命令多次访问查看效果

  • 相关阅读:
    一个功能完善的资源管理器
    数据结构和算法
    Build Native Mobile Apps in HTML5: PhoneGap from Start to Finish
    HTML5安全:CORS(跨域资源共享)简介
    Start A mobile web app
    ASP.NET MVC4 常见问题集
    ASP.NET MVC4 ASP.NET Web API OAuth2 delegation with Windows Azure Access Control Service
    【转】HTTP access control (CORS)
    ASP.NET MVC4最新特性介绍 (待续)
    C#实现小写金额转换大写的方法
  • 原文地址:https://www.cnblogs.com/fuzhongfaya/p/8952626.html
Copyright © 2020-2023  润新知