美文网首页
day45 Nginx Web 应用深入

day45 Nginx Web 应用深入

作者: 藏鋒1013 | 来源:发表于2019-05-05 19:02 被阅读0次

    1、Nginx功能模块化

    耦合度更低,易于管理。
    SQA架构,RPC服务都属于低耦合度的技术支持。
    工作中做事学会低耦合。

    企业场景常用的Nginx http功能模块汇总

    Nginx http功能模块 模块说明
    ngx_http_core_module 包括一些核心的 http 参数配置,对应 Nginx 的配置为 HTTP 区块部分
    ngx_http_access_module 访问控制模块,用来控制网站用户对 Nginx 的访问
    ngx_http_gzip_module 压缩模块,对 Nginx 返回的数据压缩,属于性能优化模块
    ngx_http_fastcgi_module FastCGI 模块,和动态应用相关的模块,如 PHP
    ngx_http_proxy_module proxy 代理模块
    ngx_http_upstream_modul e 负载均衡模块,可实现网站的负载均衡和节点的健康检查
    ngx_http_rewrite_module URL 地址重写模块
    ngx_http_limit_conn_module 限制用户并发连接数以及请求数的模块
    ngx_http_limit_req_module 根据定义的 key 限制 Nginx 请求过程的速率
    ngx_http_log_module 访问日志模块,以指定的格式记录 Nginx 客户访问日志等信息
    ngx_http_auth_basic_module Web 认证模块,设置 Web 用户通过账号密码访问 Nginx
    ngx_http_ssl_module ssl 模块,用于加密的 http 连接,如 https
    ngx_http_stub_status_module 记录 Nginx 基本访问状态信息等的模块

    2、虚拟主机分类介绍

    虚拟主机类型

    (1)基于域名的虚拟主机

    所谓基于域名的虚拟主机,意思是通过不同的域名区分不同的虚拟主机,基于域名的虚拟主机是企业应用最广的虚拟主机类型几乎所有对外提供服务的网站使用的都是基于域名的虚拟主机,例如:www.etiantian.org

    (2)基于端口的虚拟主机

    同理,所谓基于端口的虚拟主机,意思是通过不同的端口来区分不同的虚拟主机,此类虚拟主机对应的企业应用主要为公司内部的网站,例如:一些不希望直接对外提供用户访问的网站后台等,访问基于端口的虚拟主机,地址里要带有端口,例如:http:/www.etiantian.org:9000

    (3)基于IP的虚拟主机

    同理,所谓基于IP的虚拟主机,意思就是通过不同的IP来区分不同的虚拟主机,此类虚拟主机对应的企业应用非常少见。一般不同的业务需要使用多IP的场景都会在负载均衡器上进行VIP绑定,而不是在Web服务器上绑定IP来区分不同的虚拟机。

    3、实战

    (1)实战 基于域名的虚拟主机

    [root@web02 ~]# cd /application/nginx/conf/
    [root@web02 /application/nginx/conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf
    [root@web02 /application/nginx/conf]# cat -n nginx.conf
         1  worker_processes  1;
         2  events {
         3      worker_connections  1024;
         4  }
         5  http {
         6      include       mime.types;
         7      default_type  application/octet-stream;
         8      sendfile        on;
         9      keepalive_timeout  65;
        10      server {
        11          listen       80;
        12          server_name  localhost;
        13          location / {
        14              root   html;
        15              index  index.html index.htm;
        16          }
        17          error_page   500 502 503 504  /50x.html;
        18          location = /50x.html {
        19              root   html;
        20          }
        21      }
        22  }
    ###编辑nginx.conf中的内容:删除16-19行,修改12和14行。
    [root@web02/application/nginx/conf]# vim nginx.conf
      1 worker_processes  1;
      2 events {
      3     worker_connections  1024;
      4 }
      5 http {
      6     include       mime.types;
      7     default_type  application/octet-stream;
      8     sendfile        on;
      9     keepalive_timeout  65;
     10     server {
     11         listen       80;
     12         server_name  www.etiantian.org;
     13         location / {
     14             root   html/www;
     15             index  index.html index.htm;
     16         }   
     17     }   
     18 }   
    ~                                                                                            
    ~                                                                                            
    ~                                                                                            
    ~                                                                                            
    ~                                                                                            
    ~                                                                                            
    ~                                                                                            
    "nginx.conf" 18L, 381C written                                             
    [root@web02/application/nginx/conf]# mkdir ../html/www  ###=====》在html目录中创建一个www目录
    [root@web02/application/nginx/conf]# echo "www.etiantian.org" >../html/index.html  ###=====》追加内容到html下的index.html中
    [root@web02/application/nginx/conf]# cat ../html/index.html ###=====》查看一下是否追加成功
    www.etiantian.org
    
    [root@web02 /application/nginx/conf]# cat /etc/hosts
    127.0.0.1    localhost localhost.localdomain localhost4 localhost4.localdomain4
    ::1          localhost localhost.localdomain localhost6 localhost6.localdomain6
    172.16.1.5 lb01
    172.16.1.6 lb02
    172.16.1.7 web01
    172.16.1.8 web02
    172.16.1.9 web03
    172.16.1.31 nfs01
    172.16.1.41 backup
    172.16.1.51 db01 db01.etiantian.org
    172.16.1.61 m01
    [root@web02 /application/nginx/conf]# echo "10.0.0.8 www.etiantian.org" >>/etc/hosts###=====》  追加内容
    [root@web02 /application/nginx/conf]# tail -1 /etc/hosts 
        ###=====》查看一下
    10.0.0.8 www.etiantian.org
    [root@web02 /application/nginx/conf]# ping www.etiantian.org     ###=====》ping一下
    PING www.etiantian.org (10.0.0.8) 56(84) bytes of data.
    64 bytes from www.etiantian.org (10.0.0.8): icmp_seq=1 ttl=64 time=0.050 ms
    
    [root@web02 /application/nginx/conf]# echo 'PATH="/application/nginx/sbin:$PATH"' >>/etc/profile
    [root@web02 /application/nginx/conf]# . /etc/profile
    [root@web02 /application/nginx/conf]# echo $PATH
    /application/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
    [root@web02 /application/nginx/conf]# nginx -t
    nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
    [root@web02 /application/nginx/conf]# nginx -s reload
    [root@web02 /application/nginx/conf]# 
    [root@web02 /application/nginx/conf]# curl www.etiantian.org
    www.etiantian.org
    

    WINDOWS下测试:
    C:\Windows\System32\drivers\etc\hosts
    10.0.0.8 www.etiantian.org

    再添加两个:

    bbs.etiantian.org
    blog.etiantian.org

    ###在nginx.conf文件中再加入两个模块:
    [root@web02/application/nginx/conf]# vim  nginx.conf
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
            server {
                listen       80;
                server_name  bbs.etiantian.org;
                location / {
                    root   html/bbs;
                    index  index.html index.htm;
                }
            }
            server {
                listen       80;
                server_name  blog.etiantian.org;
                location / {
                    root   html/blog;
                    index  index.html index.htm;
                }
            }
    }
    ~                                                                                            
    "nginx.conf" 34L, 708C written                                             
    [root@web02/application/nginx/conf]# mkdir ../html/{bbs,blog}    ###=====》在html中创建两个文件夹,名为bbs和blog
    [root@web02 /application/nginx/conf]# echo "bbs.etiantian.org" >../html/bbs/index.html  ###=====》将内容追加到文件中
    [root@web02 /application/nginx/conf]# echo "blog.etiantian.org" >../html/blog/index.html###=====》将内容追加到文件中
    [root@web02 /application/nginx/conf]# cat ../html/blog/index.html    ###=====》查看一下
    blog.etiantian.org
    [root@web02 /application/nginx/conf]# cat ../html/bbs/index.html    ###=====》查看一下
    bbs.etiantian.org
    [root@web02/application/nginx/conf]#  nginx -t
    nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
    [root@web02/application/nginx/conf]# nginx -s reload    ###=====》平滑重启
    
    
    [root@web02/application/nginx/conf]# ping bbs.etiantian.org
    PING www.etiantian.org (10.0.0.103) 56(84) bytes of data.
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=1 ttl=64 time=0.012 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=2 ttl=64 time=0.050 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=3 ttl=64 time=0.066 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=4 ttl=64 time=0.061 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=5 ttl=64 time=0.061 ms
    ^C
    --- www.etiantian.org ping statistics ---
    5 packets transmitted, 5 received, 0% packet loss, time 4000ms
    rtt min/avg/max/mdev = 0.012/0.050/0.066/0.019 ms
    [root@web02/application/nginx/conf]# ping blog.etiantian.org
    PING www.etiantian.org (10.0.0.103) 56(84) bytes of data.
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=1 ttl=64 time=0.018 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=2 ttl=64 time=0.037 ms
    64 bytes from www.etiantian.org (10.0.0.103): icmp_seq=3 ttl=64 time=0.060 ms
    ^C
    --- www.etiantian.org ping statistics ---
    3 packets transmitted, 3 received, 0% packet loss, time 2001ms
    rtt min/avg/max/mdev = 0.018/0.038/0.060/0.017 ms
    
    [root@web02/application/nginx/conf]# curl blog.etiantian.org
    blog.etiantian.org
    [root@web02/application/nginx/conf]# curl bbs.etiantian.org
    bbs.etiantian.org
    

    (2)基于端口虚拟主机实践

    [root@web02/application/nginx/conf]# vim nginx.conf
        worker_connections  1024;
    }   
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / { 
                root   html/www;
                index  index.html index.htm;
            }   
        }   
            server {
                listen       81;
                server_name  bbs.etiantian.org;
                location / { 
                    root   html/bbs;
                    index  index.html index.htm;
                }   
            }   
            server {
                listen       82;
                server_name  blog.etiantian.org;
                location / { 
    "nginx.conf" 34L, 708C written                                             
    [root@web02/application/nginx/conf]# netstat -lntup|grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7418/nginx: master  
    [root@web02/application/nginx/conf]# nginx -t
    nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
    [root@web02/application/nginx/conf]# nginx -s reload
    [root@web02/application/nginx/conf]# netstat -lntup|grep nginx
    tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      7418/nginx: master  
    tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      7418/nginx: master  
    tcp        0      0 0.0.0.0:82              0.0.0.0:*               LISTEN      7418/nginx: master  
    [root@web02/application/nginx/conf]# curl www.etiantian.org:80
    www.etiantian.org
    [root@web02/application/nginx/conf]# curl bbs.etiantian.org:81
    bbs.etiantian.org
    [root@web02/application/nginx/conf]# curl blog.etiantian.org:82
    blog.etiantian.org
    

    (3)基于IP的虚拟主机实践:

    [root@web02 ~]# ip addr add 10.0.0.9 dev eth0 label eth0:9
    [root@web02 ~]# ip addr add 10.0.0.10 dev eth0 label eth0:10
    [root@web02 ~]# ifconfig 
    eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.8  netmask 255.255.255.0  broadcast 10.0.0.255
            inet6 fe80::20c:29ff:fe12:170c  prefixlen 64  scopeid 0x20<link>
            ether 00:0c:29:12:17:0c  txqueuelen 1000  (Ethernet)
            RX packets 21765  bytes 18029629 (17.1 MiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 12171  bytes 1426129 (1.3 MiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    eth0:9: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.9  netmask 255.255.255.255  broadcast 0.0.0.0
            ether 00:0c:29:12:17:0c  txqueuelen 1000  (Ethernet)
    
    eth0:10: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
            inet 10.0.0.10  netmask 255.255.255.255  broadcast 0.0.0.0
    
    [root@web02 /application/nginx/conf]# cat -n nginx.conf
         1  worker_processes  1;
         2  events {
         3      worker_connections  1024;
         4  }
         5  http {
         6      include       mime.types;
         7      default_type  application/octet-stream;
         8      sendfile        on;
         9      keepalive_timeout  65;
        10      server {
        11          listen       10.0.0.8:80;
        12          server_name  www.etiantian.org;
        13          location / {
        14              root   html/www;
        15              index  index.html index.htm;
        16          }
        17      }
        18      server {
        19          listen       10.0.0.9:80;
        20          server_name  bbs.etiantian.org;
        21          location / {
        22              root   html/bbs;
        23              index  index.html index.htm;
        24          }
        25      }
        26      server {
        27          listen       10.0.0.10:80;
        28          server_name  blog.etiantian.org;
        29          location / {
        30              root   html/blog;
        31              index  index.html index.htm;
        32          }
        33      }
        34  }
    [root@web02 /application/nginx/conf]# nginx -t
    nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
    [root@web02/application/nginx/conf]# nginx -c /application/nginx/conf/nginx.conf
    [root@web02/application/nginx/conf]# nginx -s reload
    [root@web02/application/nginx/conf]# netstat -lntup|grep nginx
    tcp        0      0 10.0.0.10:80            0.0.0.0:*               LISTEN      8642/nginx: master  
    tcp        0      0 10.0.0.9:80             0.0.0.0:*               LISTEN      8642/nginx: master  
    tcp        0      0 10.0.0.103:80           0.0.0.0:*               LISTEN      8642/nginx: master  
    [root@web02/application/nginx/conf]# nginx -s stop
    [root@web02/application/nginx/conf]# nginx
    [root@web02/application/nginx/conf]# netstat -lntup|grep nginx
    tcp        0      0 10.0.0.10:80            0.0.0.0:*               LISTEN      8650/nginx: master  
    tcp        0      0 10.0.0.9:80             0.0.0.0:*               LISTEN      8650/nginx: master  
    tcp        0      0 10.0.0.103:80           0.0.0.0:*               LISTEN      8650/nginx: master  
    [root@web02/application/nginx/conf]# curl 10.0.0.103
    www.etiantian.org
    [root@web02/application/nginx/conf]# curl 10.0.0.9
    bbs.etiantian.org
    [root@web02/application/nginx/conf]# curl 10.0.0.10
    blog.etiantian.org
    

    4、优化Nginx配置文件

    [root@web02/application/nginx/conf]mkdir extra
    [root@web02/application/nginx/conf]
    [root@web02 /application/nginx/conf]# sed -n '10,17p' nginx.conf 
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf]# sed -n '10,17p' nginx.conf >extra/01_www.conf
    [root@web02 /application/nginx/conf]# sed -n '18,25p' nginx.conf 
        server {
            listen       80;
            server_name  bbs.etiantian.org;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf]# sed -n '18,25p' nginx.conf >extra/02_bbs.conf
    [root@web02 /application/nginx/conf]# sed -n '26,33p' nginx.conf
        server {
            listen       80;
            server_name  blog.etiantian.org;
            location / {
                root   html/blog;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf]# sed -n '26,33p' nginx.conf >extra/03_blog.conf
    [root@web02 /application/nginx/conf]# cd extra/
    [root@web02 /application/nginx/conf/extra]# cat 01_www.conf 
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf/extra]# cat 02_bbs.conf 
        server {
            listen       80;
            server_name  bbs.etiantian.org;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf/extra]# cat 03_blog.conf 
        server {
            listen       80;
            server_name  blog.etiantian.org;
            location / {
                root   html/blog;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf/extra]# cd ../
    [root@web02 /application/nginx/conf]# sed -n '10,33p' nginx.conf
        server {
            listen       80;
            server_name  www.etiantian.org;
            location / {
                root   html/www;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  bbs.etiantian.org;
            location / {
                root   html/bbs;
                index  index.html index.htm;
            }
        }
        server {
            listen       80;
            server_name  blog.etiantian.org;
            location / {
                root   html/blog;
                index  index.html index.htm;
            }
        }
    [root@web02 /application/nginx/conf]# sed -i '10,33d' nginx.conf
    [root@web02 /application/nginx/conf]# sed -i '10 i include extra/01_www.conf;\ninclude extra/02_bbs.conf;\ninclude extra/03_blog.conf;'  nginx.conf
    [root@web02 /application/nginx/conf]# 
    [root@web02 /application/nginx/conf]# 
    [root@web02 /application/nginx/conf]# 
    [root@web02 /application/nginx/conf]# 
    [root@web02 /application/nginx/conf]# cat nginx.conf
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
    include extra/01_www.conf;
    include extra/02_bbs.conf;
    include extra/03_blog.conf;
    }
    [root@web02 /application/nginx/conf]# nginx -t
    nginx: the configuration file /application/nginx-1.16.0//conf/nginx.conf syntax is ok
    nginx: configuration file /application/nginx-1.16.0//conf/nginx.conf test is successful
    [root@web02 /application/nginx/conf]# nginx -s reload
    [root@web02 /application/nginx/conf]# curl www.etiantian.org
    www.etiantian.org
    [root@web02 /application/nginx/conf]# curl bbs.etiantian.org
    bbs.etiantian.org
    [root@web02 /application/nginx/conf]# curl blog.etiantian.org
    blog.etiantian.org
    

    5、恶意域名解析

    一:什么是恶意域名解析
      一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定。但是,如果通过主机IP能直接访问某网站,那么把域名解析到这个IP也将能访问到该网站,而无需在主机上绑定,也就是说任何人将任何域名解析到这个IP就能访问到这个网站。
    二:恶意域名解析的危害
      可能您并不介意通过别人的域名访问到您的网站,但是如果这个域名是未备案域名呢?
      假如那域名是不友善的域名,比如曾经指向非法网站,容易引发搜索引擎惩罚,连带IP受到牵连。即使域名没什么问题,但流量也会被劫持到别的域名,从而遭到广告联盟的封杀。
    三:如何防止,配置里第一个标签如下配置:
    server{
    listen 80;
    server_name _default;
    return 500;
    }

    报错:

    [root@web02/application/nginx/conf]# nginx -s reload
    nginx: [error] invalid PID number "" in "/application/nginx-1.16.0//logs/nginx.pid"
    解决方法:
    [root@web02/application/nginx/conf]# nginx -c /application/nginx/conf/nginx.conf
    [root@web02/application/nginx/conf]# nginx -s reload

    [root@web02/application/nginx/conf]# curl www.etiantian.org
    www.etiantian.org
    [root@web02/application/nginx/conf]# curl bbs.etiantian.org
    www.etiantian.org
    [root@web02/application/nginx/conf]# curl blog.etiantian.org
    www.etiantian.org
    解决方法:关闭重新开启
    [root@web02/application/nginx/conf]# nginx -s stop
    [root@web02/application/nginx/conf]# curl bbs.etiantian.org
    bbs.etiantian.org
    [root@web02/application/nginx/conf]# curl blog.etiantian.org
    blog.etiantian.org

    相关文章

      网友评论

          本文标题:day45 Nginx Web 应用深入

          本文链接:https://www.haomeiwen.com/subject/bsgcoqtx.html