Linux运维-day47-综合架构-nginx虚拟主机

一、nginx虚拟主机

1个虚拟主机相当于是一个网站
nginx中多个server标签

1.1 nginx相关错误

ping 域名
curl 域名(nginx服务)
nginx配置之后,要检查语法并reload

1.2 虚拟主机的常见类型

基于域名的虚拟主机:不同的域名访问不同虚拟主机(网站)
基于端口:不同的端口访问不同的虚拟主机----网站需要特殊端口的时候使用
基于IP虚拟主机:不同的IP地址访问不同的虚拟主机,在负载均衡的时候也会使用

实例1.2.1 基于域名的虚拟主机的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/nginx.conf 
……
  server   {
        listen       80;
        server_name  www.oldboy.com;
        location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        }  
    }   
  server   {
        listen       80;
        server_name  blog.oldboy.com;
        location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
        }
    }

\\\创建index.html文件   
[root@web01 ~]# for i in www blog ;do echo  $i.oldboy.com>/usr/share/nginx/html/$i/index.html;done

\\\检查语法
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

\\\平滑启动nginx服务
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# cat /usr/share/nginx/html/{www,blog}/index.html
www.oldboy.com
blog.oldboy.com

\\\新添加的域名一定要进行hosts解析
[root@web01 ~]# vim /etc/hosts
……
172.16.1.7      web01 www.oldboy.com blog.oldboy.com 
……

\\\检查域名是否可用
[root@web01 ~]# curl www.oldboy.com  
www.oldboy.com
[root@web01 ~]# blog.oldboy.com

实例1.2.2 基于端口的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/nginx.conf 
……
  server   {
        listen       81;
        server_name  www.oldboy.com;
        location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        }
    }
  server   {
        listen       82;
        server_name  blog.oldboy.com;
        location / {
        root   /usr/share/nginx/html/blog;
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl reload  nginx 

\\\检查配置之后的端口
[root@web01 ~]# ss -lntup|grep nginx
tcp    LISTEN     0      128       *:81                    *:*                   users:(("nginx",pid=51376,fd=10),("nginx",pid=30059,fd=10))
tcp    LISTEN     0      128       *:82                    *:*                   users:(("nginx",pid=51376,fd=11),("nginx",pid=30059,fd=11))
[root@web01 ~]# curl www.oldboy.com
curl: (7) Failed connect to www.oldboy.com:80; Connection refused
[root@web01 ~]# curl http://10.0.0.7:81
www.oldboy.com
[root@web01 ~]# curl http://10.0.0.7:82
blog.oldboy.com
[root@web01 ~]# 

实例1.2.3 基于IP虚拟主机的配置

[root@web01 ~]# vim /etc/nginx/nginx.conf 
……
  server   {
        listen      10.0.0.7:80;
        server_name  www.oldboy.com;
        location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        }
    }
  server   {
        listen       10.0.0.9:80;
        server_name  blog.oldboy.com;
        location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
        }
    }

}
"/etc/nginx/nginx.conf" 49L, 1004C written   
\\检查报错,语法没错,但是里面的指定10.0.0.9这个端口在本地不存在                                                                         
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: [emerg] bind() to 10.0.0.9:80 failed (99: Cannot assign requested address)
nginx: configuration file /etc/nginx/nginx.conf test failed

\\\因为新添加的10.0.0.0.9这个网段在本机没有,需要添加
[root@web01 ~]# ip addr add 10.0.0.9/24 dev eth0 label eth0:1
[root@web01 ~]# ip a s eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:36:af:f4 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet 10.0.0.9/24 scope global secondary eth0:1
       valid_lft forever preferred_lft forever
    inet6 fe80::20c:29ff:fe36:aff4/64 scope link 
       valid_lft forever preferred_lft forever
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl restart nginx
[root@web01 ~]# ss -lntup|grep nginx  \\检查端口
tcp    LISTEN     0      128    10.0.0.9:80                    *:*                   users:(("nginx",pid=51851,fd=7),("nginx",pid=51850,fd=7))
tcp    LISTEN     0      128    10.0.0.7:80                    *:*                   users:(("nginx",pid=51851,fd=6),("nginx",pid=51850,fd=6))
[root@web01 ~]# curl 10.0.0.7:80
www.oldboy.com
[root@web01 ~]# curl 10.0.0.9:80
blog.oldboy.com
[root@web01 ~]# 
1.3 nginx处理用户请求过程※※※

http://nginx.org/en/docs/http/request_processing.html

1>看nginx配置中指定的端口
2>是否有我想要的域名(host)
  如果有,使用这个虚拟主机
  如果没有,使用默认的虚拟主机(第1个)

在这个配置中,nginx只测试请求的头字段“host”,以确定请求>应该路由到哪个服务器。如果它的值与任何服务器名称不匹配,或者请求根本不包含这个头字段,那么nginx将把请求路由到这个端口的默认服务器。在上面的配置中,默认服务器是第一个——这是nginx的标准默认行为。还可以通过listen指令中的default_server参数明确设置默认服务器:

> <pre style="padding: 0px; margin: 0px;">
server { listen 80 **default_server** ; 
server_name example.net www.example.net; 
...  
}</pre>

二、nginx核心配置※※※

2.1 nginx日志格式

log_format:指定nginx日志格式
access_log:开启日志,指定日志文件
  buffer=16k
  flush=time
gzip 压缩
解压:gzip -d
看压缩包中的内容用以下命令:zcat、zless、zgrep、zmore、zegrep ,如:zcat /etc/nginx/conf.d/default.conf.gz

log_format日志格式的详细介绍

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

access_log  /var/log/nginx/access.log  main;

'$remote_addr          \\客户端ip地址
$remote_user          \\远程用户(空) 
[$time_local]          \\时间 
"$request"              \\请求报文的起始行 $request_uri 只取出uri
'$status                  \\状态码 
$body_bytes_sent          \\服务端发给客户端大小(每个文件的大小)
"$http_referer"           \\记录着用户从哪里跳转过来的
'"$http_user_agent"       \\用户浏览器 
"$http_x_forwarded_for"';  \\负载均衡: web服务器用来记录用户真实ip地址  

注:nginx -V:检查nginx所有的模块:

2.2 nginx配置文件切割

include文件包含(引用)

实例2.2.1 ##不同的虚拟主机如何指定不同的日志文件

[root@web01 ~]# vim /etc/nginx/nginx.conf 
……
  server   { 
        listen      80;
        server_name  www.oldboy.com;
        location / { 
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        access_log  /var/log/nginx/access_www.log  main;
        }
    }   
  server   {
        listen       80;
        server_name  blog.oldboy.com;
        location / { 
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
        access_log  /var/log/nginx/access_blog.log  main;
        }
    }   
    
"/etc/nginx/nginx.conf" 51L, 1102C written
[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# ll /var/log/nginx/
total 12
-rw-r--r-- 1 root  root    0 Jun  5 11:48 access_blog.log
-rw-r----- 1 nginx adm  5058 Jun  5 11:20 access.log
-rw-r--r-- 1 root  root    0 Jun  5 11:48 access_www.log
-rw-r----- 1 nginx adm  2013 Jun  5 11:20 error.log
[root@web01 ~]# 

实例2.2.2 在实际工作中会有很多的虚拟主机,都配置到/etc/nginx/nginx.conf 中会配置多的server标签,也不好管理,估将不同的server标签分为不同的文件

[root@web01 ~]# vim /etc/nginx/nginx.conf 
……
    include /etc/nginx/conf.d/*.conf;
}

[root@web01 ~]# vim /etc/nginx/conf.d/01-www.conf
server   {
        listen      80;
        server_name  www.oldboy.com;
▽       location / {
        root   /usr/share/nginx/html/www;
        index  index.html index.htm;
        access_log  /var/log/nginx/access_www.log  main;
        }
 }
                                                                                                                                    
"01-www.conf" [New] 9L, 242C written                                                                                
[root@web01 ~]# vim /etc/nginx/conf.d/02-blog.conf
server   {
        listen       80;
        server_name  blog.oldboy.com;
        location / {
        root   /usr/share/nginx/html/blog;
        index  index.html index.htm;
        access_log  /var/log/nginx/access_blog.log  main;
        } 
 }

[root@web01 ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@web01 ~]# systemctl reload nginx
[root@web01 ~]# ll /etc/nginx/conf.d/
total 16
-rw-r--r-- 1 root root 242 Jun  5 12:00 01-www.conf
-rw-r--r-- 1 root root 246 Jun  5 12:00 02-blog.conf
-rw-r--r-- 1 root root 488 Apr 23 22:34 default.conf.gz
[root@web01 ~]# curl www.oldboy.com
www.oldboy.com
[root@web01 ~]# curl blog.oldboy.com
blog.oldboy.com
[root@web01 ~]#  

注:将默认的站点目录default.conf用gzip压缩,不然会有影响,解压用gzip -d,查看gzip压缩用命令:zcat、zless、zgrep、zmore、zegrep

2.3 nginx状态模块机权限控制

配置状态模块

[root@web01 /etc/nginx/conf.d]# vim status.conf
server{
    listen   80;
    server_name   status.oldboy.com;
    stub_status  on;
    access_log  off;
}                                                                                                                                   
~                                                                                                                                     
"status.conf" [New] 7L, 108C written                                                                                
[root@web01 /etc/nginx/conf.d]# vim /etc/hosts
……
172.16.1.7      web01 www.oldboy.com blog.oldboy.com bbs.oldboy.com status.oldboy.com
                                                                          
[root@web01 /etc/nginx/conf.d]# systemctl reload nginx
[root@web01 /etc/nginx/conf.d]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests
 13 13 13 
Reading: 0 Writing: 1 Waiting: 0 
[root@web01 /etc/nginx/conf.d]# 

如果指定某个ip网段可以查看状态,其他网段都不能查看,进行以下设置即可

16955089-a0be42188d3da0e5.png

16955089-4d18cacd35de77df.png

权限控制

未完……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值