nginx应用--04--(linux运维18)

1. 搭建网站共享目录

1.1 编写配置文件

www.yq.com

功能模块:autoindex
autoindex

Syntax: autoindex on | off;
Default:
autoindex off;
Context: http, server, location

location / {
    autoindex on;
}

www.conf配置文件

 autoindex on;    --- 开启nginx站点目录索引功能
server {
       listen        80;
       server_name   www.yq.com;
       location / {
         root  /html/www;
         autoindex on;
    }
 }

1.2 删除首页文件,创建共享目录

cd /html/www
rm -f index.html 
mkdir -p {hu1,hu2,hu3}
cd hu1
echo 123 > 1.txt
exho "老男孩教育" > 老男孩.txt
vim /ect/nginx/conf.d/www.conf

server {
       listen        80;
       server_name   www.yq.com;
       location / {
         root  /html/www;
         autoindex on;
    }
 }

systemctl reload nginx

1.3 访问

在这里插入图片描述

1.4 中文乱码问题

在这里插入图片描述
charset utf-8;
http://nginx.org/en/docs/http/ngx_http_charset_module.html#charset

Syntax:	charset charset | off;
Default:	
charset off;
Context:	http, server, location, if in location

www.conf配置文件修改

server {
       listen        80;
       server_name   www.yq.com;
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
 }
systemctl restart nginx 
浏览器访问:
www.yq.com

在这里插入图片描述
假若还是出现乱码可能是浏览器的编码不一样,可以百度搜索修改浏览器的编码

1.5 解决txt文件不能下载问题mime.types

查看/etc/nginx/mime.types
修改:

text/plain                                       txt;

改为:

text/plain                                       php;

文件中有的扩展名信息资源, 进行访问时会直接看到数据信息
文件中没有的扩展名信息资源, 进行访问时会直接下载资源

2. 配置文件别名功能

第一个历程: 编写配置文件 
server_name   www.yq.com yq.com;
第二个历程: 配置好解析信息
访问即可

配置文件:

server {
       listen        80;
       server_name   www.yq.com yq.com;
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}

然后配置/etc/hosts

172.16.1.8 yq.com

测试:

curl yq.com

3. 对网站进行监控

模块:stub_status
http://nginx.org/en/docs/http/ngx_http_stub_status_module.html#stub_status

Syntax:	stub_status;
Default:	—
Context:	server, location


location = /basic_status {
    stub_status;
}

3.1 配置文件编写

vim state.conf
 server {
       listen    80;
       server_name  state.yq.com;
       stub_status;
    }

重启服务

systemctl reload nginx 

配置一下域名解析。hosts,测试
在这里插入图片描述

 Active connections:  激活的连接数信息  4000用户  3500
	accepts: 接收的连接数汇总(综合)  TCP
	handled: 处理的连接数汇总(综合)  TCP
	requests: 总计的请求数量  HTTP协议请求 
    Reading: nginx服务读取请求报文的数量    100人点餐
	Writing: nginx服务响应报文信息数量      100人响应
	Waiting: nginx队列机制,要处理(读取或者响应保存进行保存)   监控

4. 日志功能配置

访问日志: /var/log/nginx/access.log ngx_http_log_module

	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"              请求报文的请求行信息
    $status                 用户访问网站状态码信息
	$body_bytes_sent        显示响应的数据尺寸信息
	$http_referer           记录调用网站资源的连接地址信息(防止用户盗链)                    
                            老男孩nginx---access.log---莫文杰(荒原饮露---老男孩图片链接)---http_referer(链接)
	$http_user_agent        记录用户使用什么客户端软件进行访问页面的  (谷歌 火狐 IE 安卓 iphone)
	$http_x_forwarded_for   ??? 负载均衡

	
	错误日志: /var/log/nginx/error.log  --- Core functionality
	Syntax:	    error_log file [level];  指定错误日志路径以及错误日志记录的级别
    Default:	error_log logs/error.log error;
    Context:	main, http, mail, stream, server, location

    error_log  /var/log/nginx/error.log warn;
	错误级别:
	debug		:调试级别, 服务运行的状态信息和错误信息详细显示     信息越多
	info        :信息级别, 只显示重要的运行信息和错误信息
	notice      :通知级别: 更加重要的信息进行通知说明
	warn        :警告级别: 可能出现了一些错误信息,但不影响服务运行
	error		:错误级别: 服务运行已经出现了错误,需要进行纠正      推荐选择
	crit        :严重级别: 必须进行修改调整
	alert       :严重警告级别: 即警告,而且必须进行错误修改
	emerg       :灾难级别: 服务已经不能正常运行                      信息越少
	
	PS: 日志文件信息需要做切割处理   几个G

实现:

 server {
       listen        80;
       server_name   www.yq.com;
       access_log  /var/log/nginx/www_access.log  main;
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}
systemctl  restart nginx
[root@web02 17:13:08 /var/log/nginx]# ll
total 164
-rw-r----- 1 www   root 139515 Sep 18 17:06 access.log
-rw-r--r-- 1 root  root    287 Jun 25 13:44 access.log-20210917.gz
-rw-r----- 1 nginx root    973 Sep 18 16:25 access.log-20210918.gz
-rw-r----- 1 www   root   2653 Sep 18 16:57 error.log
-rw-r--r-- 1 root  root    207 Jun 25 13:44 error.log-20210917.gz
-rw-r----- 1 nginx root    823 Sep 18 16:24 error.log-20210918.gz
-rw-r--r-- 1 root  root    198 Sep 18 17:12 www_access.log
[root@web02 17:13:08 /var/log/nginx]# cat www_access.log 
192.168.246.1 - - [18/Sep/2021:17:12:52 +0800] "GET / HTTP/1.1" 200 701 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36" "-"

5. location的匹配机制

模块说明: ngx_http_core_module

5.1 优雅的显示错误页面

location /{
root /html/www;
error_page 404 /yq.jpg;
}

5.2 location的详细配置

location = /精确匹配,优先级01
location /默认匹配 优先级04
location /yq按照目录进行匹配 优先级03
location ^~ /yq/优先匹配 优先级02
location ~*.(gifjpg

6.nginx实现页面跳转功能

6.1 跳转功能: http_rewrite_module

https://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite

Syntax:	rewrite regex replacement [flag];
Default:	—
Context:	server, location, if

语法:

server {
    ...
    rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last;
    rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra  last;
    return  403;
    ...
}

6.2 实现

编辑配置文件
www.conf

server {
       listen        80;
       server_name   www.yq.com;
       rewrite ^/(.*) http://www.yq.com/$1 permanent; 
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}

重启:

systemctl restart nginx

配置一下hosts,

6.2.1 使用centos的curl命令则配置/etc/hosts文件

172.16.1.78  yq.com 

测试:

curl yq.com

结果

[root@web02 16:31:54 /etc/nginx/conf.d]# curl yq.com
192.168.246.8 bbs.yq.com

可以看到显示的并不是www.yq.com ,因为没有yq.com的server_name;查看cat /etc/nginx/conf.d/*.conf

[root@web02 16:31:56 /etc/nginx/conf.d]# cat /etc/nginx/conf.d/*.conf
server {
       listen        80;
       server_name   bbs.yq.com;
       location  / {
         root  /html/bbs;
         index index.html;
       }
    }
server {
       listen        80;
       server_name   blog.yq.com;
       location  / {
         root  /html/blog;
         index index.html;
       }
    }
server {
       listen        80;
       server_name   www.yq.com;
       rewrite ^/(.*) http://www.yq.com/$1 permanent; 
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}

bbs在第一个所以跳转到了第一个,这个时候我们可以配置别名的方式进行设置

server {
       listen        80;
       server_name   www.yq.com yq.com;
       rewrite ^/(.*) http://www.yq.com/$1 permanent; 
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}
systemctl restart nginx
curl yq.com

结果:

[root@web02 16:40:20 /etc/nginx/conf.d]# curl yq.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>
curl -Lv yq.com  

会出现无线跳转的情况

6.2.2 使用浏览器访问则配置windows电脑的hosts文件,配置虚拟机的ip

192.168.246.8  yq.com

6.3 跳转的方式

1.永久跳转 permanent 301 会将跳转的信息进行缓存
2.临时跳转 redirect 302 不会缓存跳转信息

6.4 无线跳转的解决办法

1.利用不通的server区块配置打破循环

server {
 server_name=oldboy.com;
 rewrite ^/(.*) http://www.yq.com/$1 permanent;
}

6.4.1 实现

配置文件

server{
    listen 80;
    server_name yq.com;
    rewrite ^/(.*) http://www.yq.com/$1 permanent;   
}

server {
       listen        80;
       server_name   www.yq.com;
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}

重启:

systemctl restart nginx 
curl -Lv yq.com 

结果:

[root@web02 16:45:49 /etc/nginx/conf.d]# curl  -Lvyq.com
curl: option -Lvyq.com: expected a proper numerical parameter
curl: try 'curl --help' or 'curl --manual' for more information
[root@web02 16:45:57 /etc/nginx/conf.d]# curl  -Lv yq.com
* About to connect() to yq.com port 80 (#0)
*   Trying 172.16.1.8...
* Connected to yq.com (172.16.1.8) port 80 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: yq.com
> Accept: */*
> 
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.20.1
< Date: Sat, 18 Sep 2021 08:46:00 GMT
< Content-Type: text/html
< Content-Length: 169
< Connection: keep-alive
< Location: http://www.yq.com/
< 
* Ignoring the response-body
* Connection #0 to host yq.com left intact
* Issue another request to this URL: 'http://www.yq.com/'
* About to connect() to www.yq.com port 80 (#1)
*   Trying 172.16.1.8...
* Connected to www.yq.com (172.16.1.8) port 80 (#1)
> GET / HTTP/1.1
> User-Agent: curl/7.29.0
> Host: www.yq.com
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.20.1
< Date: Sat, 18 Sep 2021 08:46:00 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< 
<html>
<head><title>Index of /</title></head>
<body>
<h1>Index of /</h1><hr><pre><a href="../">../</a>
<a href="hu/">hu/</a>                                                17-Sep-2021 12:42                   -
<a href="hu1/">hu1/</a>                                               17-Sep-2021 13:49                   -
<a href="hu2/">hu2/</a>                                               17-Sep-2021 13:32                   -
<a href="hu3/">hu3/</a>                                               17-Sep-2021 13:32                   -
<a href="index.html.bak">index.html.bak</a>                                     17-Sep-2021 12:16                  25
</pre><hr></body>
</html>
* Connection #1 to host www.yq.com left intact

2.if判断实现循环的打破

if ($host ~* "^yq.com$") {
      rewrite ^/(.*) http://www.yq.com/$1 permanent;
    }

配置文件:

server {
       listen        80;
       server_name   www.yq.com;
       if ($host ~* "^yq.com$") {
        rewrite ^/(.*) http://www.yq.com/$1 permanent;
        }
       location / {
         root  /html/www;
         autoindex on;
         charset utf-8;
    }
}

其他步骤如上。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

长安有故里y

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值