Nginx web应用深入

1,Nginx功能模块化

耦合度更低,易于管理。SQA架构。RPC服务都属于低耦合的技术模式。

2,虚拟主机分类介绍

3,实践

基于域名的虚拟主机
[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  }
[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  }
[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
[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 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]# /application/nginx/sbin/nginx ^C
[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

[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 {
    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]# 
[root@web02 /application/nginx/conf]# 
[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]# 
[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]# vim /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
10.0.0.8 www.etiantian.org bbs.etiantian.org blog.etiantian.org
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
~                                                                                                                        
"/etc/hosts" 12L, 396C 已写入                                                                          
[root@web02 /application/nginx/conf]# ping bbs.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.022 ms
64 bytes from www.etiantian.org (10.0.0.8): icmp_seq=2 ttl=64 time=0.056 ms
^C
--- www.etiantian.org ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 0.022/0.039/0.056/0.017 ms
[root@web02 /application/nginx/conf]# ping blog.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.228 ms
64 bytes from www.etiantian.org (10.0.0.8): icmp_seq=2 ttl=64 time=0.029 ms
^C
--- www.etiantian.org ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 0.029/0.128/0.228/0.100 ms
[root@web02 /application/nginx/conf]# 
[root@web02 /application/nginx/conf]# 
[root@web02 /application/nginx/conf]# 
[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
基于端口虚拟主机:
[root@web02 ~]# cd /application/nginx/conf/
[root@web02 /application/nginx/conf]# cp nginx.conf{,_BaseName}
[root@web02 /application/nginx/conf]# ls 
fastcgi.conf            index.html  mime.types.default   scgi_params           win-utf
fastcgi.conf.default    koi-utf     nginx.conf           scgi_params.default
fastcgi_params          koi-win     nginx.conf_BaseName  uwsgi_params
fastcgi_params.default  mime.types  nginx.conf.default   uwsgi_params.default
[root@web02 /application/nginx/conf]# vim nginx.conf

[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]# netstat -lntup|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      14322/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      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 bbs.etiantian.org
www.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
基于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 -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]# 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

一,什么是恶意域名解析

一般情况下,要使域名能访问到网站需要两步,第一步,将域名解析到网站所在的主机,第二步,在web服务器中将域名与相应的网站绑定。但是,如果通过主机IP能直接访问某网站,那么把域名解析到这个IP也将能访问到该网站,而无需在主机上绑定,也就是说任何人将任何域名解析到这个IP就能访问到这个网站。
二,恶意域名解析的危害
假如那域名是不友善的域名,比如曾经指向非法网站,容易引发搜索引擎惩罚,连带IP受到牵连。即使域名没什么问题,但流量也会被劫持到别的域名,从而遭到广告联盟的封杀。
三,如何防止,配置里第一个标签如下配置
server{
listen 80;
server_name _default;
return 500;
}
优化nginx配置文件
[root@web02 /application/nginx/conf]# mkdir extra                                                   
[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]# 
[root@web02 /application/nginx/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]# 

[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
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,294评论 6 493
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,493评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 157,790评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,595评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,718评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,906评论 1 290
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,053评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,797评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,250评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,570评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,711评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,388评论 4 332
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,018评论 3 316
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,796评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,023评论 1 266
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,461评论 2 360
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,595评论 2 350

推荐阅读更多精彩内容

  • 1、Nginx功能模块化 耦合度更低,易于管理。SQA架构,RPC服务都属于低耦合度的技术支持。工作中做事学会低耦...
    藏鋒1013阅读 195评论 0 0
  • 第1章 Nginx web应用深入 1.1 Nginx功能模块说明   Nginx软件具有众多功能模块,企业中常用...
    淺唱吶鍛慯阅读 255评论 0 2
  • 《老男孩Linux运维》笔记 隐藏Nginx软件版本号 一般来说,软件的漏洞都和版本有关。因此要尽量隐藏对访问用户...
    Zhang21阅读 3,634评论 0 28
  • Nginx简介 解决基于进程模型产生的C10K问题,请求时即使无状态连接如web服务都无法达到并发响应量级一万的现...
    魏镇坪阅读 1,994评论 0 9
  • - 我喜欢过一个男孩 - 高三的时候换了教学楼 回家的路也就变了一条 不经意的看到了一个瘦高个子 长长的腿 稳健的...
    傻子的理想阅读 178评论 0 0