07Web网站 Nginx(第二部分)

1.Nginx虚拟主机

01.nginx虚拟主机介绍

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

02.nginx相关错误

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

03.虚拟主机的常见类型

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

1 基于域名的虚拟主机的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/conf.d/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

2 基于端口的配置

\\\在nginx进行配置
[root@web01 ~]# vim /etc/nginx/conf.d/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 ~]# 

3 基于IP虚拟主机的配置

[root@web01 ~]# vim /etc/nginx/conf.d/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 ~]# 

04.nginx处理用户请求过程※※※

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>

05.nginx核心配置※※※

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

2 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所有的模块:

06.nginx配置文件切割

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

07.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网段可以查看状态,其他网段都不能查看,进行以下设置即可

在这里插入图片描述
案例:
权限控制
基于用户登录配置(简单验证)
1>在status.conf 中配置配置用户及密码

[root@web01 /etc/nginx/conf.d]# cat status.conf 
server{ 
    listen   80; 
    server_name   status.oldboy.com;
    stub_status  on;
    access_log  off;
    auth_basic "Auth access Blog Input your Passwd!";   \\指定用户密码提示
    auth_basic_user_file /etc/nginx/htpasswd;     \\指定用户密码文件

}

2>添加密码文件

[root@web01 /etc/nginx/conf.d]# htpasswd -bc /etc/nginx/htpasswd  oldboy   oldboy
Adding password for user oldboy

3>设置密码文件的权限为600,所有者及属组为nginx

\\修改密码文件的权限为600
[root@web01 /etc/nginx/conf.d]# chmod 600 /etc/nginx/htpasswd

\\修改密码文件的所有者及所有属组为nginx
[root@web01 /etc/nginx/conf.d]# chown nginx.nginx /etc/nginx/htpasswd

5>启动nginx服务

[root@web01 /etc/nginx/conf.d]# systemctl reload nginx

6>浏览器输入域名检查,如图:

08.nginx状态模块

通过监控软件查看nginx的状态
在这里插入图片描述

[root@web01 ~]# curl status.oldboy.com
Active connections: 1 
server accepts handled requests
 23 23 23 
Reading: 0 Writing: 1 Waiting: 0 

\\------------------分别代表的含义-----------------------------------
Active connections: 1     当前的连接数量(已经建立的连接)

server accepts             服务器接收到的请求数量
server handled             服务器处理的请求数量
server requests            用户一共向服务器发出多少请求 

Reading: 0                 当前nginx正在读取的用户请求头的数量 
Writing: 1                 当前nginx正在响应用户请求的数量
Waiting: 0                 当前等待被nginx处理的请求数量 

取出本地的状态码

[root@web01 /etc/nginx/conf.d]# curl  10.0.0.7
HTTP/1.1 200 OK
Server: nginx/1.16.0
Date: Thu, 06 Jun 2019 01:58:40 GMT
Content-Type: text/html
Content-Length: 15
Last-Modified: Wed, 05 Jun 2019 09:52:47 GMT
Connection: keep-alive
ETag: "5cf790ef-f"
Accept-Ranges: bytes

[root@web01 ~]# curl 10.0.0.7|awk 'NR==1{print $2}'
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    16  100    16    0     0  22471      0 --:--:-- --:--:-- --:--:-- 16000

[root@web01 ~]# curl -sI 10.0.0.7|awk 'NR==1{print $2}'
200

curl 常见的参数:
  -s:不显示网页的内容
  -w:什么输出完成后
  -o:把网站页面的内容写入到哪里或黑洞`

09.nginx的location规则

1 location的作用

根据用户请求的URL来执行不同的应用,即URI的内容,即获取的文件后缀不同,如同.html .jsp 等

2 location语法

location[=|~|~*|^~]url{
           ……
        }

3 location语法说明

在这里插入图片描述

4 匹配标识分别代表的含义

在这里插入图片描述

5 location的优先级

[root@web01 /etc/nginx/conf.d]# cat 01-www.conf
server {
    listen       80;
    server_name  www.oldboy.com;
    root   html/www;
    location / {
       return 200  "location / \n";
    }
    location = / {
        return 200 "location = \n";
    }
    location /documents/ {
        return 200 "location /documents/ \n";
    }
    location ^~ /images/ {
        return 200 "location ^~ /images/ \n";
    }
    location ~* \.(gif|jpg|jpeg)$ {
        return 200 "location ~* \.(gif|jpg|jpeg) \n";
    }
    access_log off;
}

在这里插入图片描述

6 location规则应用:

1>限制敏感目录

location /admin{
    deny all;
}

2>区分不同的文件类型

location ~* \.(gif|jpg|jpeg)$ {
    在用户浏览器缓存10年
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Linux搭建Nginx服务器是一项非常重要的技能,因为Nginx是目前最流行的Web服务器之一。搭建Nginx服务器可以让您更好地托管您的网站Web应用程序,并提供更高的性能。 需要注意的是,在搭建之前要确保您的Linux系统已经安装了Nginx软件。这里以Ubuntu Linux操作系统为例,您可以使用以下命令来安装Nginx: sudo apt-get update sudo apt-get install nginx 安装成功后,就可以开始配置Nginx服务器了。 第一步,打开Nginx配置文件。您可以使用以下命令打开默认配置文件: sudo nano /etc/nginx/sites-available/default 第二步,配置服务器。您需要在配置文件中设置您的网站目录、日志文件目录等信息。 例如: server { listen 80 default_server; root /var/www/html; index index.html index.htm index.nginx-debian.html; server_name yourdomain.com www.yourdomain.com; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location / { try_files $uri $uri/ =404; } } 在此示例中,Nginx服务器将使用默认端口80,并将网站文件托管在/var/www/html目录中。 第三步,保存配置文件并重启Nginx服务器。在完成配置后,您可以按Ctrl + X,然后选择“Y”来保存文件。接着,在命令行中输入以下命令: sudo service nginx restart 此时,您的Nginx服务器已经搭建成功,并可以通过您的公网IP地址或域名进行访问。 总之,搭建Nginx服务器并不是一项非常困难的任务,只需要一些基本的Linux命令和Nginx配置的知识即可。通过学习和实践,您将可以轻松地搭建出您的Web服务器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SMP!

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值