0
点赞
收藏
分享

微信扫一扫

Nginx常见问题 nginx多server优先级总结 禁止ip访问 Nginx优雅显示错误页面 root和alias区别 上传文件大小 try_file路径匹配

慕犹清 2022-08-26 阅读 34


文章目录

  • ​​Nginx常见问题​​
  • ​​nginx多server优先级简介​​
  • ​​准备多个配置文件​​
  • ​​创建创建站点文件​​
  • ​​配置本地hosts并访问​​
  • ​​多server优先级总结​​
  • ​​多server优先级验证​​
  • ​​配置本地hosts并访问​​
  • ​​补充​​
  • ​​禁止ip访问​​
  • ​​禁止ip访问直接返回错误​​
  • ​​引流的方式,访问IP跳转到其他网站​​
  • ​​跳转到指定错误页面​​
  • ​​nginx的包含 include​​
  • ​​nginx路径的 root与alias​​
  • ​​1.root和alias区别​​
  • ​​线上配置​​
  • ​​Nginx调整上传文件大小​​
  • ​​nginx上传文件大小限制配置语法​​
  • ​​nginx长传文件大小限制配置示例​​
  • ​​Nginx try_file路径匹配​​
  • ​​正常的配置文件​​
  • ​​使用try_file的配置​​
  • ​​修改try_file配置​​
  • ​​try_files使用场景​​
  • ​​配置nginx​​
  • ​​安装tomcat​​
  • ​​测试​​
  • ​​另外一个场景(@说明)​​
  • ​​Nginx优雅显示错误页面​​
  • ​​跳转到固定链接​​
  • ​​跳转到固定页面​​
  • ​​访问php找不到文件时错误页面跳转​​

Nginx常见问题

nginx多server优先级简介

在开始处理一个http请求时,nginx会取出header头中的Host变量(域名),与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。

准备多个配置文件

[root@web01 conf.d]# cat server01.conf 
server {
listen 80;
server_name localhost test1.com;

location / {
root /code/node;
index index.html;
}
}
[root@web01 conf.d]# cat server02.conf
server {
listen 80;
server_name localhost test2.com;

location / {
root /code/node;
index index.html;
}
}
[root@web01 conf.d]# cat server03.conf
server {
listen 80;
server_name localhost test3.com;

location / {
root /code/node;
index index.html;
}
}
##检查并重启 nginx -t #会提示警告
[root@web01 ~]# systemctl restart nginx

创建创建站点文件

[root@web01 conf.d]# mkdir /code/node{1..3}
[root@web01 conf.d]# chown -R www.www /code/

[root@web01 conf.d]# echo server01 111 > /code/node/index.html
[root@web01 conf.d]# echo server02 222 > /code/node/index.html
[root@web01 conf.d]# echo server03 333 > /code/node/index.html

配置本地hosts并访问

1.根据ip地址访问   
# 默认从上到下依次匹配

在百度192.168.15.7 会显示 server01 111

如果在把[root@web01 conf.d]

多server优先级总结

1.首先选择所有的'字符串完全匹配''server_name'。(完全匹配  www.mm.xin.com)
2.选择'通配符在前面'的server_name,如 '*.linux.com'
3.选择'通配符在后面'的server_name,如 'www.yangenguang.*'
4.最后选择使用'正则表达式'匹配的server_name,如:'~^www\.(.*)\.com$'
5.如果全部都没有匹配到,那么将选择在'listen配置项'后加入'[default_server]'的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件

多server优先级验证

#完全匹配
#访问的URL:www.test.com
[root@web01 conf.d]# cat server1.conf
server {
listen 80;
server_name localhost www.test.com;

location / {
root /code/test1;
index index.html;
}
}
[root@web01 conf.d]# echo "完全匹配123" > /code/test1/index.html

#通配符在前面
[root@web01 conf.d]# cat server2.conf
server {
listen 80;
server_name localhost *.test.com;

location / {
root /code/test2;
index index.html;
}
}
[root@web01 conf.d]# echo "通配符在前面123" > /code/test2/index.html

#通配符在后面
[root@web01 conf.d]# cat server3.conf
server {
listen 80;
server_name localhost www.test.*;

location / {
root /code/test3;
index index.html;
}
}
[root@web01 conf.d]# echo "通配符在后面123" > /code/test3/index.html

#正则表达式匹配
[root@web01 conf.d]# cat server4.conf
server {
listen 80;
server_name localhost ~^www\.(.*)\.com$;

location / {
root /code/test4;
index index.html;
}
}
[root@web01 conf.d]# echo "正则表达式123" > /code/test4/index.html


#配置default_server的配置文件
#如果全部都没有匹配到,那么将选择在listen配置项后加⼊[default_server]的server块 默认的页面
[root@web01 conf.d]# cat server5.conf
server {
listen 80 default_server;
server_name localhost;
return 500;
#location / {
# root /code/test5;
# index index.html;
#}
}
[root@web01 conf.d]# echo "default_server" > /code/test5/index.html

# 放在第一个的配置文件
[root@web01 conf.d]# cat server6.conf
server {
listen 80;
server_name localhost;

location / {
root /code/test6;
index index.html;
}
}
[root@web01 conf.d]# echo "第一个123" > /code/test6/index.html

## 检查并重启 nginx -t
[root@web01 ~]# systemctl restart nginx

配置本地hosts并访问

#配置本地hosts

补充

# 补充   中文乱码  在 /etc/nginx/nginx.conf 里添加一行  
charset utf8; # 添加一行
access_log /var/log/nginx/access.log main;


# 补充 dG 删除全文全部内容
# yum nginx安装 需要修改/etc/nginx/nginx.conf 里面default_server

禁止ip访问

当用户通过访问IP或者未知域名访问你得网站的时候,你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都基本关闭外网,使用内网,需要时在开启。

禁止ip访问直接返回错误

[root@web01 conf.d]# vim yeg.conf 
server {
listen 80 default_server; #默认第一个 default_server
server_name localhost;
return 500;
}

引流的方式,访问IP跳转到其他网站

server {
listen 80 default_server;
server_name localhost;
return 302 https://www.baidu.com;
}

跳转到指定错误页面

server {
listen 80 default_server;
server_name localhost;
default_type text/plain;
return 200 "请使用域名再次访问正规网站!!";
}

nginx的包含 include

一台服务器配置多个网站,如果配置都写在nginx.conf主配置文件中,会导致nginx.conf主配置文件变得非常庞大而且可读性非常的差。那么后期的维护就变得麻烦。 

假设现在希望快速的关闭一个站点,该怎么办?
1.如果是写在nginx.conf中,则需要手动注释,比较麻烦
2.如果是include的方式,那么仅需修改配置文件的扩展名,即可完成注释 Include包含的作用是为了简化主配置文件,便于人类可读。

/etc/nginx/nging.conf #修改nginx的配置文件 *conf文件

inlcude /etc/nginx/online/*.conf #线上使用的配置

/etc/nginx/offline #保留配置,不启用(下次使用在移动到online中

nginx路径的 root与alias

root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使两者分别以不同的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。

root的处理结果是:root路径+location路径
alias的处理结果是:使用alias定义的路径

1.root和alias区别

[root@web01 conf.d]# vim image.conf
server {
listen 80;
server_name image.com;

location /picture {
root /code;
}
}
#使用root的时候,访问http://image.com/picture/1.gif,实际上会到服务器的/code/picture/目录下面寻找1.gif文件

[root@web01 conf.d]# vim image.conf
server {
listen 80;
server_name image.com;

location /picture {
alias /code;
}
}
#如果使用的是alias,访问http://image.com/picture/1.gif,实际上是到服务器的/code/目录下寻找1.gif文件

线上配置

[root@lb02 conf.d]# cat image.conf 
server {
listen 80;
server_name image.com;

location / {
root /code;
index index.html 1.html test.html
}

location ~* \.(jpg|png|gif)$ {
alias /code/images;
}
}

Nginx调整上传文件大小

在nginx使用上传文件的过程中,通常需要设置文件大小限制,避免出现 413 Request Entity Too Lar

nginx上传文件大小限制配置语法

Syntax:  client_max_body_size size;
Default: client_max_body_size 1m;

nginx长传文件大小限制配置示例

[root@lb02 conf.d]# vim /etc/nginx/nginx.conf

#也可以放入http层,全局生效
server {
listen 80;
server_name _;
client_max_body_size 500m;
}

Nginx try_file路径匹配

nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。如果文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。

#try_files 一般使用在伪静态的场景中

正常的配置文件

[root@web01 conf.d]# vim linux.try.conf 
server {
listen 80;
server_name linux.try.com;

location / {
root /code;
index index.html; #默认是这个
}
}


[root@web01 conf.d]# echo "test try_file" > /code/index.html

# 授权: chown -R www.www /code/
## 检查nginx -t 并重启

[root@web01 conf.d]# systemctl restart nginx

使用try_file的配置

## 保证/code/目录必须有8.png的图片文件 且权限www

[root@web01 conf.d]# vim linux.try.conf
server {
listen 80;
server_name linux.try.com;

location / {
root /code;
try_files $uri /8.png;
}
}


#访问测试:

1.访问域名时 linux.try.com,返回的结果是 8.png
由于请求的是域名,后面没有 uri,那么 $uri 匹配到的就是 "空",匹配不到内容的情况下,返回 8.png

2.访问域名 linux.try.com/index.html,返回的结果是 index.html
由于请求的是linux.try.com/index.html,$uri

修改try_file配置

[root@web01 conf.d]# vim linux.try.conf 
server {
listen 80;
server_name linux.try.com;

location / {
root /code;
try_files $uri $uri/ /8.png #空2格
}
}

#解析 : try_files $uri $uri/ /8.png
$uri :访问根目录下的/code文件
$uri/ :访问code目录
/8.png :访问的是根目录下8.png

#访问测试:

1.访问域名 linux.try.com,返回的结果是 index.html
由于请求的是域名,后面没有 uri,那么 $uri 匹配到的就是 "空"$uri 匹配不到内容的情况下,匹配 $uri/,匹配到的是 "空/",/ 配置的是 /code,那么就回去请求code目录下的 index.html

try_files使用场景

配置nginx

[root@l web03 conf.d]# vim linux.try.conf    #先安装comcat
server {
listen 80;
server_name linux.try.com;
root /code;

location / {
try_files $uri $uri/ @java; #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但一定要加@
}

location @java {
proxy_pass http://172.16.1.8:8080; #配置后端tomcat
}
}

安装tomcat

[root@web03~]# yum install -y tomcat
[root@web03 ~]# cd /usr/share/tomcat/webapps/
[root@web03 webapps]# mkdir ROOT
[root@web03 webapps]# echo "test try_file @java" > ROOT/index.html

测试

[root@web03 ~]# systemctl restart tomcat

另外一个场景(@说明)

[root@l web03 conf.d]# vim linux.try.conf  
server {
listen 80;
server_name _;
location / {
index index.html;
root /www;
try_files $uri @abc;
}

location @abc {
root /www/test;
index index.html;
}

}

## @abc : 指定是重定向到@abc相匹配的location

Nginx优雅显示错误页面

在企业中,访问到的错误页面----1、重定向到首页 2、跳转到固定的错误页面

跳转到固定链接

[root@web01 conf.d]# vim linux.error.conf 
server {
listen 80;
server_name linux.error.com;

location / {
root /code;
index index.html;
error_page 404 http://www.baidu.com;
}
}

[root@web01 conf.d]# echo "error" > /code/index.html

# 授权: chown -R www.www /code/
## 检查nginx -t 并重启

[root@web01 conf.d]# systemctl restart nginx

本地hosts访问 192.168.15.7 linux.error.com


## 成功就自动跳转的index.html 如果错误跳转到百度

跳转到固定页面

error_page : 根据后面的状态码跳转到指定页面,一般用于根据状态码捕捉错误,跳转到指定页面。

[root@web01 conf.d]# vim linux.error.conf 
server {
listen 80;
server_name linux.error.com;

location / {
root /code;
index index.html;
error_page 404 /8.png;
}
}

# 输入这个 http://linux.error.com/sssssss 错误 自动跳转8.png

## 成功就自动跳转的index.html 如果错误跳转到本地8.png

访问php找不到文件时错误页面跳转

[root@web01 conf.d]# vim linux.error.conf 
server {
listen 80;
server_name linux.error.com;
root /code;
index index.php;
error_page 404 /404.jpg;

location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if (!-e $request_filename) { #条件为真则执行下面,‘请求的文件不存在’ -e:等于 !=取反.
rewrite (.*) http://linux.error.com/8.png;
}
}
}


举报
0 条评论