2019-05-05-day45 Nginx web应用深入

第1章 Nginx web应用深入

1.1 Nginx功能模块说明

  Nginx软件具有众多功能模块,企业中常用的模块有Nginx核心功能模块和标准的http功能模块集合两个模块。模块化能够使耦合度更低,易于管理。
企业中常用的Nginx http功能模块如下:

Nginx http功能模块 模块说明
ngx_http_core_modul 核心的http配置参数,对应Nginx的配置为HTTP区块部分
ngx_http_access_modul 访问控制模块,用来控制网站用户对Nginx的访问
ngx_http_gzip_modul 压缩模块,对Nginx返回的数据压缩,属于性能优化模块
ngx_http_fastcgi_modul FastCGI模块,和动态应用相关的模块
ngx_http_proxy_modul proxy代理模块
ngx_http_upstream_modul 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查
ngx_http_rewrite_modul URL地址重写模块
ngx_http_limit_conn_modul 限制用户并发连接数及请求数模块
ngx_http_limit_req_modul 根据定义的key限制Nginx请求过程的速率
ngx_http_log_modul 访问日志模块,以指定的格式记录Nginx客户访问日志等信息
ngx_http_auth_basic_modul Web认证模块,设置Web用户通过账号、密码访问Nginx
ngx_http_ssl_modul ssl模块,用于加密的http连接
ngx_http_stub_status_modul 记录Nginx基本访问状态信息等的模块

1.2 虚拟主机分类介绍

1.2.1 虚拟主机的概念

  虚拟主机就是在Web服务里的一个独立的网站站点,这个站点对应独立的域名(也可能是IP或端口),具有独立的程序及资源目录,可以独立地对外提供服务供用户访问。

1.2.2 虚拟主机类型

  1. 基于域名的虚拟主机:就是通过不同的域名区分不同的虚拟主机,是企业中应用最广泛的虚拟主机类型。
  2. 基于端口的虚拟主机:就是通过不同的端口来区分不同的虚拟主机,主要应用在公司内部的网站。
  3. 基于IP的虚拟主机:就是通过不同的IP区分不同的虚拟主机。

1.3 基于域名的虚拟主机实践

1.3.1 配置基于域名的nginx.conf文件内容

[root@web02 ~]# cd /application/nginx/conf/    <===切换到配置文件目录
[root@web02 /application/nginx/conf]# egrep -v "^$|#" nginx.conf.default >nginx.conf   <===过滤包含#的行和空行,并生成文件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  }

删除文件的17到20行内容,并修改文件如下:

 [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  www.etiantian.org;        <===修改域名为www.etiantian.org
    13          location / {
    14              root   html/www;                <===修改域名对应的站点目录
    15              index  index.html index.htm;
    16          }
    17      }
    18  }
[root@web02 /application/nginx/conf]$ nginx -t        <===检查语法,出现ok和successful为成功
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       <===平滑重启

1.3.2 创建域名对应的站点目录及文件

[root@web02 /application/nginx/conf]$ mkdir ../html/www  <===创建对应目录
[root@web02 /application/nginx/conf]$ echo "www.etiantian.org" >../html/www/index.html  <===创建对应文件,并写入内容
[root@web02 /application/nginx/conf]$ cat ../html/www/index.html
www.etiantian.org

1.3.3 将域名和对应的IP信息写入/etc/hosts域名解析文件

[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 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

1.3.4 定义环境变量,可以直接使用nginx命令

[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

1.3.5 linux下测试结果

[root@web02 /application/nginx/conf]# curl www.etiantian.org
www.etiantian.org

1.3.6 基于多个域名的虚拟主机配置如下

 [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  www.etiantian.org;
    13          location / {
    14              root   html/www;
    15              index  index.html index.htm;
    16          }
    17      }
    18      server {                    <===增加两个server标签
    19          listen       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       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]$ mkdir ../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]$ 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]$ tail -1 /etc/hosts
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org
[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

1.4 基于端口虚拟主机实践

[root@web02 /application/nginx/conf]$ cp nginx.conf{,_BaseName}    <===备份文件
[root@web02 /application/nginx/conf]$ vim nginx.conf
    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;
"nginx.conf" 34L, 753C 已写入                                                   
[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      14322/nginx: master 
tcp        0      0 0.0.0.0:81              0.0.0.0:*               LISTEN      14322/nginx: master 
tcp        0      0 0.0.0.0:82              0.0.0.0:*               LISTEN      14322/nginx: master 
[root@web02 /application/nginx/conf]# curl www.etiantian.org
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
[root@web02 /application/nginx/conf]# curl blog.etiantian.org
www.etiantian.org

注:

  1. 基于端口的虚拟主机,先匹配请求的端口,然后匹配Server标签域名,把对应域名下面站点目录下的首页文件发给客户端。如果没有匹配的域名,就把第一个虚拟主机的站点目录下的首页文件发给客户端。
  2. 如果不输入端口号,则默认为80端口,只会返回80端口的虚拟主机的站点目录下的首页文件。

1.5 基于IP的虚拟主机

1.5.1 在网卡上添加多个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  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
        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  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  mtu 1500
        inet 10.0.0.10  netmask 255.255.255.255  broadcast 0.0.0.0

1.5.2 修改虚拟主机配置

[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;   <===增加上IP地址
    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;   <===增加上IP地址
    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;   <===增加上IP地址
    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 -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      14967/nginx: master 
tcp        0      0 10.0.0.9:80             0.0.0.0:*               LISTEN      14967/nginx: master 
tcp        0      0 10.0.0.8:80             0.0.0.0:*               LISTEN      14967/nginx: master 
[root@web02 /application/nginx/conf]$ curl 10.0.0.8
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

1.6 恶意域名解析

1.6.1 什么是恶意域名解析

  一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定。但是,如果通过主机IP能直接访问某网站,那么把域名解析到这个IP也将能访问到该网站,而无需在主机上绑定,也就是说任何人将任何域名解析到这个IP就能访问到这个网站。

1.6.2 恶意域名解析的危害

  假如那域名是不友善的域名,比如曾经指向非法网站,容易引发搜索引擎惩罚,连带IP受到牵连。即使域名没什么问题,但流量也会被劫持到别的域名,从而遭到广告联盟的封杀。

1.6.3 如何防止恶意域名解析

配置文件里的第一个标签内容如下,就不会访问到网站内容了:

server{
listen 80;
server_name _default;
return 500;
}

1.7 优化nginx配置文件

[root@web02 /application/nginx/conf]$ mkdir extra      <===创建目录                                             
[root@web02 /application/nginx/conf]$ sed -n '10,17p' nginx.conf   <===查看第一个server标签内容
    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 <===将第一个server标签内容写入到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]$ sed -i '10,33d' nginx.conf   <===删除原文件的3个标签内容
[root@web02 /application/nginx/conf]$ sed -i '10 i include extra/*.conf; '  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/*.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

1.8 设置别名

在标签内的server_name结尾增加别名内容:

[root@web02 /application/nginx/conf]$ cat extra/01_www.conf 
    server {
        listen       80;
        server_name  www.etiantian.org etiantian.org;
        location / {
            root   html/www;
            index  index.html index.htm;
        }
    }

你可能感兴趣的:(2019-05-05-day45 Nginx web应用深入)