19@nginx常见问题

NGINX 常见问题

一、nginx多server优先级

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

1、多个配置文件准备测试

#文件一
[root@web01 conf.d]# cat server1.conf 
server {
	listen 80;
	server_name localhost test1.com;
	
	location / {
		root /hzl/server1;
		index index.html;
	}
}
#文件二
[root@web01 conf.d]# cat server2.conf 
server {
	listen 80;
	server_name localhost test2.com;
	
	location / {
		root /hzl/server2;
		index index.html;
	}
}
#文件三
[root@web01 conf.d]# cat server3.conf 
server {
	listen 80;
	server_name localhost test3.com;
	
	location / {
		root /hzl/server3;
		index index.html;
	}
}


You have new mail in /var/spool/mail/root

#配置文件
[root@web01 conf.d]#ll
-rw-r--r-- 1 root root 9564 Mar 10 16:07 server1.conf
-rw-r--r-- 1 root root 9556 Mar 10 16:08 server2.conf
-rw-r--r-- 1 root root 9544 Mar 10 16:08 server3.conf

2、站点文件配置

#创建测试文件
[root@web01 conf.d]# mkdir /hzl/server{1..3}
You have new mail in /var/spool/mail/root
[root@web01 conf.d]# echo "test1" > /hzl/server1/index.html
[root@web01 conf.d]# echo "test2" > /hzl/server2/index.html
[root@web01 conf.d]# echo "test3" > /hzl/server3/index.html
[root@web01 conf.d]# chown -R www.www /hzl/

3、nginx检查并重启

#重复提示信息忽略
[root@web01 conf.d]# nginx -t
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successfu

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

4、localhost访问测试

192.168.15.7

5、多server优先级总结

再开始处理一个HTTP请求时,Nginx会读取header(请求头)中的host,与每个server中的server_name进行匹配,来决定用哪一个server标签来完成处理这个请求 ,有可能一个Host与多个server中的server_name都匹配,这个时候就会根据匹配优先级来选择实际处理的server,优先级匹配结果如下:

1>.首先选择所有的字符串完全匹配的server_name。(完全匹配)
#访问的URL:www.test01.com
server {
	listen 80;
	server_name www.server1.com;
	location / {
		root /hzl/server1;
		index index.html index.php;
	}
}


2>.选择通配符在前面的server_name,如*.mumusir.com www.mumusir.com
#访问的URL:m.test01.com
server {
	listen 80;
	server_name www.server1.com;
	location / {
		root /hzl/server1;
		index index.html index.php;
    }
}




3>.选择通配符在后面的server_name,如mumusir.* mumusir.com mumusir.cn
#访问的URL:www.test01.cn
server {
	listen 80;
	server_name www.server2.com;
	location / {
		root /hzl/server2;
		index index.html index.php;
	}
}



4>.最后选择使用正则表达式匹配的server_name,如~^www\.(.*)\.com$
#访问的URL:www.xxx.com
server {
	listen 80;
	server_name ~^www\.(.*)\.com$;
	location / {
		root /hzl/server2;
		index index.html index.php;
	}

}



5>.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
server {
	listen 80  default_server;
	server_name localhost;
	return 500;
	#location / {
	#	root /hzl/server2;
	#	index index.html index.php;
	#}
}


6>.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件



#注:当出现多个相同的server_name情况下,配置文件排序优先使用则会被调用,所以建议配置相同端口,不同域名,这样不会出现域名访问冲突

二、nginx禁止IP访问网站

当用户通过访问IP或者未知域名访问你得网站的时候, 你希望禁止显示任何有效内容,可以给他返回500,目前国内很多机房都要求网站关闭空主机头,防止未备案的域名指向过来造成麻烦

1、nginx禁止ip访问(直接返回错误)

# 返回错误页面
[root@web01 conf.d]# vim server4.conf 
server {
    listen 80 default_server;
    server_name localhost;
    return 500;
}

2、引流的方式,访问IP(跳转指定的网页)

[root@web01 conf.d]# vim server5.conf 
server {
    listen 80 default_server;
    server_name localhost;
    return 302 https://www.baidu.com;
}

3、返回指定的内容 (跳转到指定错误页面)

[root@web01 conf.d]# vim server6.conf 
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包含的作用是为了简化主配置文件,便于人类可读。
3.两个目录
include /etc/nginx/online/*.conf  #线上使用
mv .cnof  /etc/nginx/offlie/	#下线的配置,临时撤站

四、nginx路径的( root与alias)

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

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

1、root和alias的配置

#使用root时,用户访问http://image.com/picture/1.jpg时,实际上Nginx会到/hzl/picture/目录下找1.jpg文件
[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;

    location /picture {
        root /hzl;
    }
}



#使用alias时,用户访问http://image.com/picture/1.jpg时,实际上Nginx会到/hzl/目录下找1.jpg文件
[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;

    location /picture {
        alias /hzl;
    }
}

2、线上配置(生产实践中)

[root@web01 conf.d]# vim image.conf
server {
    listen 80;
    server_name image.com;
    
    location / {
    	root /hzl;
    	index index.html 1.html test.html
    }

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

五、Nginx try_file路径匹配

1、 try_file简述

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

2、try_files配置案列



#简例详解
location /images/ {
        try_files $uri $uri/ /404.html;
}



#用户请求www.try.file.com/images/1.gif,Nginx 会首先通过用于这个 location,在本地目录中查找这个文件,如果“1.gif”文件不存在,Nginx 会查找“hzl/”目录,即“www.try.file.com/hzl/images/”,如果都不存在,会重定向到“/404.html”



$uri : 访问根目录下的/hzl文件
$uri/: 访问hzl目录
/demo.html : 访问的式根目录下demo.html

#访问顺序:
try_files 一般使用在伪静态的场景中
1>、nginx try_files配置案列一


#1. 配置nginx
[root@lb01 conf.d]# vim try.file.conf 
server {
    listen 80;
    server_name www.try.file.com;
    root /hzl;
    index index.html;

    location / {
        try_files $uri $uri/ /404.html;
    }
}



#2. 创建实例目录与文件drz
[root@lb01 conf.d]# echo try11111 > /hzl/index.html 
[root@lb01 conf.d]# echo '404 404 404' > /hzl/404.html



#3. 尝试访问www.try.file.com
[root@lb01 conf.d]# curl www.try.file.com
404 404 404
#由于访问的是www.try.file.com,而$uri取得是域名后面我们写的内容,它找不到,所以返回后面的内容,即404.html






#4. 尝试访问www.try.file.com/index.html
[root@lb01 conf.d]# curl www.try.file.com/index.html
try11111
#由于访问的是www.try.file.com/index.html,而$uri取到了index.html所以返回/hzl/index.html的内容





#5. 修改配置为
location / {
    try_files $uri $uri/ /404.html;
}




#6. 再次尝试访问www.try.file.com
[root@lb01 conf.d]# curl www.try.file.com
try11111
#我们访问的是www.try.file.com,而$uri我们没有写任何内容,于是他访问的便是“空/”,即匹配到/hzl/index.html

2>、nginx try_files配置案列二
#1. 配置nginx
[root@lb01 conf.d]# vim  try.file1.conf 
server {
    listen 80;
    server_name www.try.file.com;
    root /hzl;
    index index.html;

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

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

#2. 配置后端tomcat
[root@web02 ~]# cd /usr/share/tomcat/webapps/ROOT
[root@web02 ROOT]# echo 'i am tomcat' > index.html
[root@web02 ROOT]# systemctl start tomcat

#3. 把文件都挪走
[root@lb01 code]# mv index.html index1.html /tmp/

#4. 测试访问
[root@lb01 code]# curl http://www.try.file.com/index.html
i am tomca

六、Nginx 调整上传文件大小

1.nginx 上传文件大小限制语法

Syntax:  client_max_body_size size;
Default: client_max_body_size 1m;
Context: http, server, location

2.nginx上传文件大小限制配置

#也可以放入http层,全局生效
server {
    listen 80;
    server_name _;
    client_max_body_size 200m;     #添加至配置文件 
    ......
}

七、Nginx优雅显示错误页面(指定页面显示)

1、重定向到首页

2、跳转到固定的错误页面

1.跳转到网上(跳转网络地址)

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

    location / {
        root /hzl;
        index index.html;
        error_page 404 http://www.baidu.com;    #找不到文件,会跳转到指定的网页
    }
}

2.跳转到本地文件(跳转本地地址)

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

    location / {
        root /hzl;
        index index.html;
        
        #error_page  403 404  /404.jpg;
        error_page 404 /404.jpg;       #指定跳转显示的页面
    }
}

[root@web01 code]# ll 404.jpg 
-rw-r--r-- 1 root root 9564 Mar 10 16:07 404.jpg

3.配置负载证明错误页面是由web端返回的

[root@lb01 conf.d]# vim error1.conf 
upstream errorweb {
    server 172.16.1.7:80;
}

server {
    listen 80;
    server_name www.error1.com;

    location / {
        proxy_pass http://errorweb;   
        include proxy_params;
        access_log /tmp/nginx.log;
    }
}


#localhost测试访问
www.error1.com

4.访问php错误页面跳转

[root@lb01 conf.d]# vim blog.conf 
server {
    listen 80;
    server_name www.error.com;

    location / {
        root /hzl/wordpress;
        index index.php;
        error_page 404 403 /404.jpg;
    }

    location ~* \.php$ {
        root /hzl/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        if (!-e $request_filename) {
            rewrite (.*) /hzl/wordpress/404.jpg;   #跳转指定的错误页面
        }
    }
}



#localhost测试访问
www.error.com

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dhhYaWCp-1620790817604)(C:\Users\86176\AppData\Roaming\Typora\typora-user-images\1620781348060.png)]

posted @ 2021-05-12 11:44  ଲ一笑奈&何  阅读(63)  评论(0编辑  收藏  举报