nginx-虚拟主机,访问控制,防盗链,代理服务

来源:互联网 发布:华为路由器联网端口 编辑:程序博客网 时间:2024/05/02 02:11

客户端192.168.0.100客户端访问

http://www.tarena.com /usr/local/nginx/html/onedir/
http://bbs.tarena.com /bbsdir

192.168.0.254
/usr/local/nginx/html/onedir/index.html (123)

配置
1 虚拟主机 (基于域名 基于端口号 基于ip地址)

配置基于域名的虚拟主机(通过主机名区分客户端的访问)
192.168.0.254
cat /etc/hosts
192.168.0.254 www.tarena.com www
192.168.0.254 bbs.tarena.com bbs

http://www.tarena.com /wwwdir index.html (www-page)
http://bbs.tarena.com /bbsdir index.html (bbs-page)
http://mail.tarena.com /maildir index.html (mail-page)

vim nginx.conf
…..
…..

http {      server  {            listen   80;            server_name  www.tarena.com;            location  /  {                   root    /wwwdir ;                   index  index.html ;            }      }     server  {           listen   80;           server_name  bbs.tarena.com;            location   /   {                      root  /bbsdir;                      index  index.html;            }    }}

基于端口的虚拟主机(通过端口区分客户端的访问)
80
http://www.tarena.com /wwwdir index.html ( web)
http://www.tarena.com:8000 /bbsdir index.html (bbs)

(http://bbs.tarena.com:8090 /managerdir index.html (manager)

http {server   {              listen  8090;              server_name  bbs.tarena.com;              location   /  {                      root  /managerdir;                      index index.html;              }      }    server   {           listen   8000;           #server_name   www.tarena.com;#只要dns能够解析就可以,主机名可以不写           location   /   {                    root   /bbsdir;                    index  index.html;                     }   }   server   {         listen   80;         #server_name  www.tarena.com;         location   /   {               root  /wwwdir;               index  index.html;        }   }}

基于Ip地址的虚拟主机 (通过客户端访问的ip地址区分访问)
http://192.168.0.254 (eth0) /wwwdir index.html www
http://192.168.0.253 (eth1) /bbsdir index.html bbs
[http://192.168.0.253:8080 (eth1) /manager index.html manager]

     server  {          listen   192.168.0.254:80;           location  /  {                   root     /wwwdir;                   index   index.html;         }       }     server  {         listen   192.168.0.253:80;         location  /  {                  root  /bbsdir;                  index  index.html;          }     }}

基于域名的虚拟主机:发布给公网用户访问的
基于端口/ip地址的虚拟主机 : 把网址的管理页面发布给局域网用户。

练习:
网址服务器ip地址是 :192.168.0.254
客户端访问的地址 网页目录 首页文件名 首页内容
http://www.tarena.com /usr/local/nginx/html/ index.html wwwpage
http://bbs.tarena.com:8000 /bbsdir a.html bbspage

网址服务器限制客户端对自己网页目录的访问 ,location / { …… },默认全局允许,访问控制:
在location中添加:

allow  192.168.0.100;#允许该IPdeny  all;  #默认拒绝所有客户端访问
deny    192.168.0.100;#只禁止该ipallow  all; #默认允许所有客户端访问

用户验证 (客户端访问网页文件时,必须输入正确的用户名和密码才可以访问)

location{...auth_basic    "auth-domain";  # 认证域的名称,其实是提示信息auth_basic_user_file    /usr/local/nginx/conf/authuser.txt; # 指定保存用户名和密码的文件...}

加强安全:只允许从地址 192.168.0.100访问 bbs.tarena.com 的 /bbsdir目录下的网页文件 ,同时访问时要输入用户(是webdamin) 密码(admin88) 才可以访问

rpm  -q  httpd-tools#创建user.txt的软件支持htpasswd -c /usr/local/nginx/user.txt   webadmin#-h查看帮助,用户名为webadmin,回车后提示输入两次密码。再次添加用户时,不用加-c,-c是创建文件的意思server {        listen       8000;        server_name  bbs.tarena.com;        location / {            root   /bbsdir;            index  a.html index.htm;            allow  192.168.0.100;            deny all;            auth_basic "please user and  password";#需要图形化界面支持            auth_basic_user_file  "/usr/local/nginx/user.txt";#当前浏览器关闭之前,密码登陆一直有效        }}

4 防 “盗链”

盗链 : 盗取其他网站的资源,为自己谋利,盗取访问量,访问量属于首次访问网页,不属于资源网站

盗链网站:
web-server 192.168.0.20 ( 盗链254的图片)
service httpd restart

<html>       <body>           <a href="http://www.tarena.com/one.png" > show 20 image </a>       </body></html>

编辑域名解析:
/etc/hosts
192.168.0.20 www.baidu.com www
192.168.0.254 www.tarena.com www

254的nginx网站服务器上防盗链配置:

server    {#语法与shell编程相似,有严格的空格约束,不能少写空格    ......    ......      location  ~* 空格\.(gif|jpg|png|swf|flv|mp3|mp4)$空格{         valid_referers   none   blocked   www.tarena.com    .tarena.com;#~*后面有空格#none      本地访问file:///usr/local/nginx/html/one.png#blocked    经防火墙转换后的地址#主机名       www.tarena.com      网站服务器的主机名#区域名       .tarena.com         公司网站所在的区域         if  (空格$invalid_referer空格) {#$invalid_referer是内置参数,不在valid_refers变量中的            rewrite  ^/     http://www.tarena.com/error.html;#^/表示源地址,重定向到error.html页面            #return 404#也可以返回错误码         }     }    ......    ......}

编辑错误跳转页面:
echo “get out” > /usr/local/nginx/html/error.html
实验中注意浏览器缓存的问题!!要既是清理终端缓存,确保看到正确的效果。

5 使用Nginx做代理服务
client 1.1.1.1 http://1.1.1.254

1.1.1.254 eth1
nginx
0.254 eth0

web_20 web_100

配置文件vim nginx.conf

http {     upstream   "apachegrp"   {   #定义服务器组,可以写多个,可以不调用           server  192.168.0.20:80; #必须写端口号,没有缺省端口           server  192.168.0.100:80;#默认轮询均衡负载     }     server  {              listen  80;              location  /   {                  proxy_pass    http://apachegrp;#调用服务器组,一次只能调用一个服务器组              }     }}

服务器组设置分发策略:
轮询 (默认) 平均分发用户的连接请求
weight 权重值 默认权重值 是1

 upstream  "webgrp" {        server  192.168.0.20:80 weight=3;        server  192.168.0.100:80 weight=2;    }

ip_hash 根据客户端的来源地址 做hash运算 ,根据运算结果分发用户的连接请求。这样可以让每个访客固定访问一个后端服务器 可以解决session的问题.

upstream  "webgrp" {         ip_hash;        server  192.168.0.20:80 ;        server  192.168.0.100:80 ;    }

Fair 按后端服务器的响应时间来分配请求 响应时间短的优先分配
默认不支持,需要在安装的时候配置./confgiure;

upstream  "webgrp" {        fair;        server  192.168.0.20:80 ;        server  192.168.0.100:80   max_fails=3   fail_timeout=30s;    }

设置服务组中服务器的状态:
down: 表示当前server暂时不参与负载,对该服务器进行维护时使用。
backup:当其他所有的非backup机器down或者忙的时候,请求会发给backup机器,所以这台机器压力会最轻,也就是备用机。

upstream  "webgrp" {        server  192.168.0.20:80 backup;#备用机        server  192.168.0.100:80 down;#该机维护中,不参与负载均衡    }

max_fails:允许请求失败的次数(默认为1),当超过此次数时,返回proxy_next_upstream模块定义的错误
fail_timeout :max_fails次失败后,暂停提供服务的时间

upstream  "webgrp" {        fair;        server  192.168.0.20:80 ;        server  192.168.0.100:80   max_fails=3 fail_timeout=30s;#相应请求失败3次,则暂停分发给该服务器服务30秒    }

动静页面分离:
网页文件
动态页面 .php .jsp .net 192.168.0.100 php
安装并启动php:

rpm -qa | grep php
yum -y install php*
service httpd restart

编辑动态页面代码:
vim /var/www/html/a.php

<?php           echo "hello php";        ?>

查看php网页:
elinks –dump http://localhost/a.php

静态页面 .css .xml .html 192.168.0.20 html *
client 1.1.1.1
http://1.1.1.254
http://1.1.1.254/a.html
http://1.1.1.254/a.php

vim nginx.conf

http {      upstream  "webhtml"  {            server  192.168.0.20:80;             server  192.168.0.200:80;         }      upstream  "webphp"  {             server  192.168.0.10:80;             server  192.168.0.100:80;         }      server    {            listen  80;                  location  /  {                  proxy_pass  http://192.168.0.20:80;                  #proxy_pass  http://webhtml;             }           location  ~  \.php$   {                  proxy_pass  http://192.168.0.100:80;                  #proxy_pass  http://webphp;           }     }}
0 0
原创粉丝点击