nginx详细应用

一、nginx的基本功能

基本Http服务,可以作为Http代理服务器和反向代理服务器,支持通过缓存加速访问,可以完成简单的负载均衡和容错,支持包过滤功能,支持SSL

高级Http服务,可以进行自定义配置,支持虚拟主机,支持URL重定向,支持网络监控,支持流媒体传输等

邮件代理服务器,支持IMAP/POP3代理服务功能,支持内部SMTP代理服务功能

二、nginx的具体应用

1、nginx的虚拟主机:经过对nginx配置文件的配置,生成虚拟主机;实现不同的域名访问不同的页面

虚拟主机技术:主要应用与http服务;将一台服务器的某项或多个服务内容逻辑划分为多个服务单位,对外表现为多个服务器,从而可以充分利用服务器的硬件资源。具体配置如下 

1)修改nginx配置文件

server {
        listen       80;
        server_name  www.xniu.com;
        location / {
               root   /www1;
               index  index.html;
        }
server {
        listen 80;
        server_name  www.linux.org;

        location /{
                root /www2;
                index index.html;
        }
    }
2)在根目录下建立www1和www2目录,并编辑index.html默认发布文件。并启动nginx服务

3)在物理机中做解析。

--->  vim  /etc/hosts

      server6  www.xniu.com  www.linux.org

4)在真机中测试结果如下:


2、nginx实现https加密

1)编辑nginx配置文件

# HTTPS  server

server {

        listen       443 ssl;
        server_name  www.xniu.com;

        ssl_certificate      cert.pem;    # 把认证的钥匙和密码设置相同
        ssl_certificate_key  cert.pem;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;

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

--->  /usr/local/lnmp/nginx/sbin/nginx  -t    # 没有认证的锁和钥匙

2)生成锁和钥匙

--->  cd  /etc/pki/tls/certs

--->  make cert.pem           # 基本信息自己填写

--->  mv cert.pem  /usr/local/lnmp/nginx/conf/
3)重新加载nginx配置文件,就ok了。

4)在浏览器中进行测试


3、 重定向

3-1、将www.xniu.com和xniu.com重定向到https://www.xniu.com。nginx配置如下:(重定向到443)

server {
        listen       80;
        server_name  www.xniu.com  xniu.com;
        rewrite  ^/(.*)$ https://www.xniu.com/$1 permanent;       # 使用通配符表示。permanent表永久生效

        # $1表示,www.xniu.com/news会访问到https://www.xniu.com/news/index.html目录

        # location / {          # 由于进行域名的重定向,则它发布目录就可以注释另外

        #     root   /www1;
        #     index  index.html;
        # }


3-2、将www.xniu.com重定向到bbs.xniu.com;即重定向到其他访问目录 。配置如下:

server {
        listen       80;
        server_name  www.xniu.com  xniu.com;
        rewrite ^/bbs$  http://bbs.xniu.com;    # 当访问www.xniu.com/bbs的时候,会重定向到bbs.xniu.com的默认发布目录

       

        # 当重定向有条件的时候,就要把自己的发布目录也添加进去(当条件不满足时执行)

        location / {            

               root   /www1;
               index  index.html;
        }
    }
# 添加bbs.xniu.com的服务
server {              
        listen  80;
        server_name  bbs.xniu.com;

        location /{
                root  /bbs;
                index  index.html;
        }
在物理机中添加解析:  172.25.2.6  bbs.xniu.com;并在server6中创建bbs.xniu.com对应的发布目录和文件。最终结果为:

3-3、将www.xniu.com/bbs/index.html 重定向到 bbs.xniu.com/index.html

server {
        listen       80;
        server_name  www.xniu.com  xniu.com;
        rewrite ^/bbs$  http://bbs.xniu.com;              # 当以bbs结尾时,重定向到http://bbs.xniu.com
        rewrite ^/bbs/(.*)$  http://bbs.xniu.com/$1;      # 当为www.xniu.com/bbs/index.html会重定向到对应的index.html

         

        location / {

               root   /www1;
               index  index.html;
        }
}

3-4、反向重定向:当访问bbs.xniu.com的时候,重定向到https://www.xniu.com/bbs

server {
        listen       80;
        server_name  www.xniu.com  xniu.com;
        #rewrite ^/bbs$  http://bbs.xniu.com;
        #rewrite ^/bbs/(.*)$  http://bbs.xniu.com/$1;
        #rewrite  ^/(.*)$ https://bbs.xniu.com/$1 permanent;

        location / {
            root   /www1;
            index  index.html;
        }
server {
        listen  80;
        server_name  bbs.xniu.com;
        rewrite ^/(.*)$ https://www.xniu.com/bbs/$1;         # 访问bbs.xniu.com转到https://www.xniu.com/bbs下
        location /{
                root  /bbs;
                index  index.html;
        }
    }


4、限制客户的访问并发量和下载速率

1)限制客户端的并发数

#gzip  on;
  limit_conn_zone   $binary_remote_addr  zone=addr:10m; 
  location  /download/ {   # 在该目录下存放访问的图片
            limit_conn  addr  1;        #设置并发量为1
  }
#在html目录下建立download目录,并放置一张图片用来访问(注意图片的权限)。然后在物理机中模拟客户端的并发请求,结果如下:

--->  ab -c1 -n 10 http://www.xniu.com/download/test.jpg   # 发送10个请求,并发为1。(此时所有请求均正常接收)

# 在nginx的日志中查看访问记录。我们可以看到访问都是正常的,返回值为200.

--->  tail -n 10 /usr/local/lnmp/nginx/logs/access.log 

172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:13:20 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3
如果并发量为大与1的时候,就会有1个错误请求。我们可以在日志在查看到十个请求只有一个被正常响应了。

--->  ab -c10 -n 10 http://www.xniu.com/download/test.jpg

# 查看日志接收的情况:

--->  tail -n 10 /usr/local/lnmp/nginx/logs/access.log

172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 127785 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 503 537 "-" "ApacheBench/2.3"
172.25.2.250 - - [05/Oct/2018:15:26:05 +0800] "GET /download/test.jpg HTTP/1.0" 200 127785 "-" "ApacheBench/2.3"

2)限制客户端下载速率为50kb

#gzip  on;

  limit_req_zone  $binary_remote_addr  zone=one:10m  rate=1r/s;       

  limit_conn_zone   $binary_remote_addr  zone=addr:10m;          # 限制带宽

  location  /download {

            limit_conn  addr  1;        #设置并发量为1

            limit_rate  50k;            #设置速率为50k
            limit_req  zone=one  burst=5 ;
      }

5、用户访问控制

1)只允许172.25.2.250的主句访问

location / {
         allow  172.25.2.250;     # ip为250的到达之后,会先允许;其他ip主机到达之后,直接deny
         deny all;
         root   /www1;
         index  index.html;
   }

# 使用主机172.25.2.1来访问server6,会出现430访问禁止情况:

2)允许特定网段的主机访问

location / {
        allow  172.25.2.0/24;             # 允许172.25.2.0网段访问
        deny   all;
        root   /www1;
        index  index.html;
  }

# 此时172.25.2.1主机就可以访问了

3)当所有主机访问默认页面时,设置为500报错或进行重定向

server {
        listen       80;
        server_name  _;

        return 500;      # 当其他主机访问,返回500错误

        # rewrite ^(.*) http://www.westos.org permanent;   # 重定向到其他站点

6、nginx中的乱码问题。(默认是不能识别汉语)

server {
        listen       80;
        server_name  localhost;
        charset utf-8;   # 添加字符编码的格式: utf-8;

7、去掉没必要的日志

 access_log   off;

8、防盗链机制:在其他主机上访问server6中的文件

# 在一台含有apache的主机的默认发布目录下,添加如下内容

<html>
<body>
<img src="http://172.25.2.6/download/test.jpg">     # 通过server1访问server6中的图片
</body>
</html>      

# 在server6中nginx的配置文件添加防盗链的访问权限

location ~ .*\.(gif|jpg|png)$ {            # 格式为gif、jpg、png的访问的设定
            expires  30d;   # 图片缓存时间是30d
            valid_referers none  blocked www.xniu.com; 
            if ($invalid_referer) {     # 表示除了www.xniu.com,其他访问均为403
                    return 403;
            }
    }

# 此时403在页面为图片的时候不会有提示;我们可以将显示页面进行重定向(实现人机交互),编辑ngxin配置文件:

location ~ .*\.(gif|jpg|png)$ {
        expires  30d;   # 图片缓存时间是30d
        valid_referers none  blocked www.xniu.com;
        if ($invalid_referer) {
                #return 403;
                rewrite ^/ http://bbs.xniu.com/daolian.jpg; # 重定向到daolian.jpg
            }
     }
server {
        listen  80;
        server_name  bbs.xniu.com;
        #rewrite ^/(.*)$ https://www.xniu.com/bbs/$1;
        location /{
                root  /www2;       # 把防盗链图片放在/www2的目录下
                index  index.html;
        }
    }



猜你喜欢

转载自www.cnblogs.com/uthnb/p/9746005.html