3 Nginx 常用功能
3.15 favicon 图标配置
favicon.ico 文件是浏览器收藏网址时显示的图标,当客户端使用浏览器问页面时,浏览器会自己主动发起请求获取页面的 favicon.ico文件,但是当浏览器请求的favicon.ico文件不存在时,服务器会记录404日志,而且浏览器也会显示404报错
[root@ubuntu ~]# tail /var/log/nginx/access.log | grep favicon
10.0.0.1 - - [01/Feb/2025:11:43:43 +0800] "GET /favicon.ico HTTP/1.1" 404 181
"http://www.m99-josedu.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Edg/121.0.0.0"
10.0.0.1 - - [01/Feb/2025:11:43:43 +0800] "GET /favicon.ico HTTP/1.1" 404 181
"http://www.m99-josedu.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Edg/121.0.0.0"
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location = /favicon.ico {
log_not_found off;
access_log off;
}
}
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location = /favicon.ico {
root /var/www/html/www.m99-josedu.com/static;
expires 7d;
access_log off;
}
}
3.16 Nginx 实现 Https
Nginx 中的 Https 功能需要 ngx_http_ssl_module 模块支持,使用 Yum/apt 安装的 Nginx 中已经包含了该模块的功能,如果使用的是自行通过源码编译安装的 Nginx,需要在编译的时候指定相关编译项
https://nginx.org/en/docs/http/ngx_http_ssl_module.html
ssl on|off;
ssl_buffer_size size;
ssl_certificate file;
ssl_certificate_key file;
ssl_protocols [SSLv2] [SSLv3] [TLSv1] [TLSv1.1] [TLSv1.2] [TLSv1.3];
ssl_session_cache off|none|[builtin[:size]] [shared:name:size];
ssl_session_timeout time;
[root@ubuntu ~]
[root@ubuntu ~]
[root@ubuntu easy-rsa]
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /usr/share/easy-rsa/pki
[root@ubuntu easy-rsa]
pki/
├── openssl-easyrsa.cnf
├── private
├── reqs
└── safessl-easyrsa.cnf
2 directories, 2 files
[root@ubuntu easy-rsa]
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
......
......
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:josedu.com
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/usr/share/easy-rsa/pki/ca.crt
[root@ubuntu easy-rsa]
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
......
......
Common Name (eg: your user, host, or server name) [www.m99-josedu.com]:
Keypair and certificate request completed. Your files are:
req: /usr/share/easy-rsa/pki/reqs/www.m99-josedu.com.req
key: /usr/share/easy-rsa/pki/private/www.m99-josedu.com.key
[root@ubuntu easy-rsa]
Using SSL: openssl OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
......
......
Certificate created at: /usr/share/easy-rsa/pki/issued/www.m99-josedu.com.crt
[root@ubuntu easy-rsa]
[root@ubuntu easy-rsa]
[root@ubuntu easy-rsa]
pki
├── ca.crt
......
......
├── issued
│ └── www.m99-josedu.com.crt
├── openssl-easyrsa.cnf
├── private
│ ├── ca.key
│ └── www.m99-josedu.com.key
......
......
├── reqs
│ └── www.m99-josedu.com.req
......
......
└── www.m99-josedu.com.pem
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
}
server {
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
ssl_certificate /usr/share/easy-rsa/pki/www.m99-josedu.com.pem;
ssl_certificate_key /usr/share/easy-rsa/pki/private/www.m99-josedu.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
server {
listen 80;
server_name www.m99-josedu.com;
return 301 https://$host$request_uri;
rewrite ^(.*) https://$server_name$1 permanent;
}
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
ssl_certificate /usr/share/easy-rsa/pki/www.m99-josedu.com.pem;
ssl_certificate_key /usr/share/easy-rsa/pki/private/www.m99-josedu.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
}
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
ssl_certificate /usr/share/easy-rsa/pki/www.m99-josedu.com.pem;
ssl_certificate_key /usr/share/easy-rsa/pki/private/www.m99-josedu.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
if ($scheme = http) {
return 301 https://$host$request_uri;
rewrite ^(.*) https://$server_name$1 permanent;
}
}
3.17 Nginx 中配置防盗链
盗链是指某站点未经允许引用其它站点上的资源,基于访问安全考虑,Nginx 支持通过 ngx_http_referer_module 模块,检查和过滤 Referer 字段的值,来达到防盗链的效果
HTTP Referer是Header的一部分,当浏览器向Web服务器发送请求的时候,一般会带上Referer,告诉服务器我是从哪个页面链接过来的,服务器借此可以获得一些信息用于处理,例如校验加载图片、文件等来源是否是指定域名,如不是则禁止访问。因此HTTP Referer头信息是可以通过程序来伪装生成的,所以通过Referer信息防盗链并非100%可靠,但是,它能够限制大部分的盗链情况
https://nginx.org/en/docs/http/ngx_http_referer_module.html
valid_referers none |blocked|server_names|string ...;
# Nginx 会使用请求头中的 referer 字段值和 valid_referers 指定的规则进行对比,如果不匹配,会将 $invalid_referer 变量的值设为 1,默认该变量值为空字符串,作用域 server, location
# none 如果请求头中没有 referer 字段,则 $invalid_referer 变量值为空
# blocked 如果请求头中有 referer 字段,但其值不合法(不是以 http 或 https 开头),则 $invalid_referer 变量为空
# server_names 具体主机名,可以写一个或多个,支持正则和通配符,如果请求头中的 referer 字段值与定义的 server_names 匹配,则$invalid_referer 变量为空
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
}
server {
listen 80;
server_name www.abc-123.com;
root /var/www/html/www.abc-123.com;
}
[root@ubuntu ~]# cat /var/www/html/www.m99-josedu.com/test.html
hello world
<img src="http://www.abc-123.com/test.jpg" />
#当访问者在浏览器中打开 http:
#对于http:
利用 valid_referers 指令实现防盗链
server {
listen 80;
server_name www.abc-123.com;
root /var/www/html/www.abc-123.com;
valid_referers none blocked server_names *.test.com ~\.baidu\. ~\.bing\. ~\.so\.;
if ($invalid_referer) {
return 403 "Forbidden Access";
}
}
#access log 中返回 403
[root@ubuntu ~]# tail -1 /var/log/nginx/access.log
10.0.0.1 - - [05/Feb/2025:08:32:42 +0800] "GET /test.jpg HTTP/1.1" 403 16
"http://www.m99-josedu.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Edg/121.0.0.0"
#在浏览器中直接访问 http:
[root@ubuntu ~]# tail -2 /var/log/nginx/access.log
10.0.0.1 - - [05/Feb/2025:08:34:28 +0800] "GET /test.jpg HTTP/1.1" 200 11647 "-"
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/121.0.0.0 Safari/537.36 Edg/121.0.0.0"
#referer 为空,可以访问
[root@ubuntu ~]# curl http:
hello world
#referer 不合法,也可以访问
[root@ubuntu ~]# curl -e "test" http:
hello world
#允许访问的 referer
[root@ubuntu ~]# curl -e "http://www.test.com" http:
hello world
#允许访问的 referer
[root@ubuntu ~]# curl -e "http://www.baidu.com/abc" http:
hello world
#不允许访问的 referer
[root@ubuntu ~]# curl -e "http://www.xyz.com" http:
Forbidden Access
3.18 Nginx 中的 Rewrite
在 Nginx 中,rewrite 指令用于重写 URI,允许 Nginx 修改客户端请求的 URI,基于此,可用该指令实现 URL 重定向,修改请求参数,改变请求含义,改变 URL 结构等,该指令来自于 ngx_http_rewrite_module 模块
ngx_http_rewrite_module 模块是 Nginx 中的一个核心模块,不管是 yum/apt 安装还是编译安装,默认都己经包含在 Nginx 中了,它提供了强大的 URL 重写和重定向功能。主要作用是允许管理员通过配置文件来修改客户端请求的 URI,从而实现重写 URL、重定向请求、更改请求参数等操作
https://nginx.org/en/docs/http/ngx_http_rewrite_module.html
break; #中断当前相同作用域(location)中的其它 ngx_http_rewrite_module 模块的配置和指令,返回到上一作用域继续执行
#该指令后的其它指令和配置还会执行,只中断 ngx_http_rewrite_module 指令,作用域 server, location, if
if (condition) { ... }
# 允许在配置中使用条件判断,使用正则表达式(pcre风格)对变量进行匹配,匹配成功返回true,执行后续指令
# if指令仅能做单次判断,不支持 if else 多分支
# if ($var){ } 这种写法,如果变量对应的值是空字符串或0,就返回false
# nginx 1.0.1之前$变量的值如果以0开头的任意字符串会返回false
# 作用域 server, location
# 支持的运算符
# = 比较变量和字符串是否相等
# != 比较变量和字符串是否不相等
# ~ 区分大小写,是否匹配正则,包含
# !~ 区分大小写,是否不匹配正则,包含
# ~* 不区分大小写,是否匹配正则,包含
# !~* 不区分大小写,是否不匹配正则,不包含
# -f|!-f 判断文件是否存在|不存在
# -d|!-d 判断目录是否存在|不存在
# -x|!-x 判断文件是否可执行|不可执行
# -e|!-e 判断文件(包括文件,目录,软链接)是否存在|不存在
return code [text];
return code URL;
return URL; # 不写code ,默认值为302
# 直接向客户端返回状态码,字符串,或者URL,如果返回的字符串中包含空格,要加引号,如果返回URL,要写完整
# 此指令后的其它指令或配置将不再执行,作用域 server, location, if
rewrite regex replacement [flag];
# 通过正则表达式匹配来改变URI,在一个配置段中可以有一条或多条,按照顺序从上下往下匹配
# 作用域 server, location, if
# 如果有多条规则,被某一条规则命中并替换后,会用新的URI再从头开始逐一匹配,直到没有被命中为止
# 但是重复匹配次数不能超过 10次,否则会报500
# regex PCRE 风格的正则表达式,表示要查找的内容
# replacement 用于替换的字符串
# [flag] 标志位,用于控制 rewrite 指令的行为
last|break|redirect|permanent
# last 如果被当前 rewrite 规则匹配上,替换后结束本轮替换,开始下一轮替换
# break 如果被当前 rewrite 规则匹配上,替换后结束当前代码段的重写替换,后续所有 rewrite 都不执行
# redirect 如果被当前 rewrite 规则匹配上,替换后执行 302 临时重定向
# permanent 如果被当前 rewrite 规则匹配上,替换后执行 301 永久重定向
# last 和 break 在服务器内部实现跳转,客户端浏览器地址栏中的信息不会发生变化
# redirect 和 permanent 在客户端实现跳转,客户端浏览器地址栏中的信息会发生变化
rewrite_log on|off; #是否记录 ngx_http_rewrite_module 模块产生的日志到 error_log 中,默认值 off
#如果开启,需要将 error_log 的级别设为 notice,作用域 http, server, location, if
set $variable value; #设置变量,给变量赋值,作用域 server, location, if
last 和 break
相同点:
不同点:
3.18.2 PCRE 风格正则表达式
PCRE(Perl Compatible Regular Expressions)风格的正则表达式在设计上兼容Perl语言的正则表达式语法,具有灵活且功能强大的特点。下面是一些 PCRE 风格正则表达式中常见的元字符和功能
.
\w
\s
\d
\b
[]
[^]
^
$
*
+
?
{n}
{n,}
{n,m}
{,m}
|
()
\
3.18.3 相关指令测试
break 指令测试
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location /break {
set $var1 josedu;
echo $var1;
break;
set $var2 1234; # set 是属于 ngx_http_rewrite_module 模块指令,此句在 break 之后,不生效
echo "$var1 -- $var2"; # 不输出 var2
return 200 "hello break"; # return 是属于 ngx_http_rewrite_module 模块指令,此句在 break 之后,不生效
}
}
#测试
[root@ubuntu ~]# curl http:
josedu
josedu --
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
ssl_certificate /usr/share/easy-rsa/pki/www.m99-josedu.com.pem;
ssl_certificate_key /usr/share/easy-rsa/pki/private/www.m99-josedu.com.key;
ssl_session_cache shared:sslcache:20m;
ssl_session_timeout 10m;
location /scheme {
if ($scheme = http) {
return 200 "http";
}
if ($scheme = https) {
return 200 "https";
}
}
location /test {
return 200 $scheme;
}
location /file {
if (!-e $request_filename) {
return 200 "$request_filename file not exists";
}
}
}
[root@ubuntu ~]# touch /var/www/html/www.m99-josedu.com/file.txt
#测试
[root@ubuntu ~]# curl http:
http
[root@ubuntu ~]# curl htTP:
http
[root@ubuntu ~]# curl -k htTPS:
https
[root@ubuntu ~]# curl -k https:
https
root@ubuntu ~]# curl http:
http
[root@ubuntu ~]# curl -k https:
https
[root@ubuntu ~]# curl -k https:
/var/www/html/www.m99-josedu.com/file.html file not exists
[root@ubuntu ~]# curl -k https:
/var/www/html/www.m99-josedu.com/file.log file not exists
[root@ubuntu ~]# curl -k https:
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
set $slow 1;
location =/test.img {
if ($slow) {
limit_rate 10k;
}
}
}
[root@ubuntu ~]
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location /return {
if ($http_user_agent ~* curl|wget|ApacheBench) {
return 403 "agent error!";
}
return 200 "success";
}
}
[root@ubuntu ~]# curl http:
agent error!
[root@ubuntu ~]# curl -I http:
HTTP/1.1 403 Forbidden
Server: nginx
Date: Mon, 05 Feb 2025 06:27:39 GMT
Content-Type: application/octet-stream
Content-Length: 12
Connection: keep-alive
#指定 agent
[root@ubuntu ~]# curl -A "chrome" http:
success
[root@ubuntu ~]# curl -IA "chrome" http:
HTTP/1.1 200 OK
Server: nginx
Date: Mon, 05 Feb 2025 06:29:31 GMT
Content-Type: application/octet-stream
Content-Length: 7
Connection: keep-alive
server {
listen 80;
listen 443 ssl;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location = /return_url {
return http://www.baidu.com;
}
}
[root@ubuntu ~]
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@ubuntu ~]
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 05 Feb 2025 06:38:21 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: http://www.baidu.com
[root@ubuntu ~]
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Mon, 05 Feb 2025 06:42:43 GMT
Content-Type: text/html
Content-Length: 138
Connection: keep-alive
Location: http://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Mon, 05 Feb 2025 06:42:43 GMT
Etag: "575e1f59-115"
Last-Modified: Mon, 13 Jun 2021 02:50:01 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
[root@ubuntu ~]# echo "11111" > /var/www/html/www.m99-josedu.com/1.html
[root@ubuntu ~]# echo "22222" > /var/www/html/www.m99-josedu.com/2.html
[root@ubuntu ~]# echo "33333" > /var/www/html/www.m99-josedu.com/3.html
[root@ubuntu ~]# echo "aaaaa" > /var/www/html/www.m99-josedu.com/a.html
[root@ubuntu ~]# echo "bbbbb" > /var/www/html/www.m99-josedu.com/b.html
[root@ubuntu ~]# echo "ccccc" > /var/www/html/www.m99-josedu.com/c.html
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
# 1.html ----> 2.thml
# 2.html ----> 3.html
# 3.html ----> b.html
[root@ubuntu ~]# curl http:
bbbbb
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html;
rewrite /2.html /3.html;
rewrite /b.html /c.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
# 1.html ----> 2.thml
# 2.html ----> 3.html
# 3.html ----> b.html
# b.html ----> c.html
[root@ubuntu ~]# curl http:
ccccc
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html break;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
# 1.html ----> 2.thml
# break 结束当前 server 配置段中的所有 rewrite
[root@ubuntu ~]# curl http:
22222
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
# 1.html ----> 2.thml last 结束当前 location 中的本轮 rewrite,继续执行下一轮 rewrite
# 2.html ----> a.thml 一下轮 location /2.html 的优先级更高
[root@ubuntu ~]# curl http:
aaaaa
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /22.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@ubuntu ~]# echo "22-22" > /var/www/html/www.m99-josedu.com/22.html
# 1.html ----> 22.thml last 结束当前 location 中的本轮 rewrite,继续执行下一轮 rewrite
# 后续没有 22.html 的规则,返回 22.html
[root@ubuntu ~]# curl http:
22-22
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html last;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /1.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
# 1.html ----> 2.thml last
# 2.html ----> 1.thml
# 1.html ----> 2.thml last
# ... 无限循环,最终返回 500 服务器内部错误
[root@ubuntu ~]# curl http:
<html>
<head><title>500 Internal Server Error</title></head>
<body>
<center><h1>500 Internal Server Error</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@ubuntu ~]# tail -1 /var/log/nginx/error.log
2025/02/05 17:24:05 [error] 3477#3477: *1 rewrite or internal redirection cycle
while processing "/2.html", client: 127.0.0.1, server: www.m99-josedu.com,
request: "HEAD /1.html HTTP/1.1", host: "www.m99-josedu.com"
rewirte 指令测试 - 配合 redirectserver {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html redirect;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /1.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@ubuntu ~]
<html>
<head><title>302 Found</title></head>
<body>
<center><h1>302 Found</h1></center>
<hr><center>nginx</center>
</body>
</html>
[root@ubuntu ~]
curl: (47) Maximum (50) redirects followed
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html redirect;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@ubuntu ~]
aaaaa
[root@ubuntu ~]
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 06 Feb 2025 02:12:10 GMT
Content-Type: text/html
Content-Length: 138
Location: http://www.m99-josedu.com/2.html
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 06 Feb 2025 02:12:10 GMT
Content-Type: text/html; charset=utf8
Content-Length: 6
Last-Modified: Mon, 05 Feb 2025 08:26:15 GMT
Connection: keep-alive
ETag: "65c09ba7-6"
Accept-Ranges: bytes
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html redirect;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html redirect;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@ubuntu ~]
aaaaa
[root@ubuntu ~]
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 06 Feb 2025 02:25:42 GMT
Content-Type: text/html
Content-Length: 138
Location: http://www.m99-josedu.com/2.html
Connection: keep-alive
HTTP/1.1 302 Moved Temporarily
Server: nginx
Date: Tue, 06 Feb 2025 02:25:42 GMT
Content-Type: text/html
Content-Length: 138
Location: http://www.m99-josedu.com/a.html
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 06 Feb 2025 02:25:42 GMT
Content-Type: text/html; charset=utf8
Content-Length: 6
Last-Modified: Mon, 05 Feb 2025 08:26:15 GMT
Connection: keep-alive
ETag: "65c09ba7-6"
Accept-Ranges: bytes
rewirte 指令测试 - 配合 permanentserver {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location / {
rewrite /1.html /2.html permanent;
rewrite /2.html /3.html;
}
location /2.html {
rewrite /2.html /a.html permanent;
}
location /3.html {
rewrite /3.html /b.html;
}
}
[root@ubuntu ~]
aaaaa
[root@ubuntu ~]
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 06 Feb 2025 02:38:50 GMT
Content-Type: text/html
Content-Length: 162
Location: http://www.m99-josedu.com/2.html
Connection: keep-alive
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Tue, 06 Feb 2025 02:38:50 GMT
Content-Type: text/html
Content-Length: 162
Location: http://www.m99-josedu.com/a.html
Connection: keep-alive
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 06 Feb 2025 02:38:50 GMT
Content-Type: text/html; charset=utf8
Content-Length: 6
Last-Modified: Mon, 05 Feb 2025 08:26:15 GMT
Connection: keep-alive
ETag: "65c09ba7-6"
Accept-Ranges: bytes
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
if (!-e $request_filename) {
rewrite .* /index.html redirect;
return 302 http://$host;
}
}
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
if ($http_user_agent ~* "android|iphone|ipad") {
rewrite ^(.*)$ http://m.m99-josedu.com/$1 redirect;
}
}
server {
listen 80;
server_name www.m99-magedu.com;
root /var/www/html/www.m99-magedu.com;
if ($remote_addr != "10.0.0.1") {
rewrite ^(.*)$ /msg.html break;
}
}
— END —
阅读原文:原文链接
该文章在 2025/7/1 23:01:01 编辑过