Nginx多Server优先级html
准备nginx对应的配置文件java
[root@web01 conf.d]# cat server1.conf server { listen 80; server_name localhost test1.com; location / { root /code/test1; index index.html; } } [root@web01 conf.d]# cat server2.conf server { listen 80; server_name localhost test2.com; location / { root /code/test2; index index.html; } } [root@web01 conf.d]# cat server3.conf server { listen 80; server_name localhost test3.com; location / { root /code/test3; index index.html; } } [root@web01 conf.d]#
准备站点目录nginx
[root@web01 conf.d]# mkdir /code/test{1..3} [root@web01 conf.d]# echo test1 > /code/test1/index.html [root@web01 conf.d]# echo test2 > /code/test2/index.html [root@web01 conf.d]# echo test3 > /code/test3/index.html
检查语法提示冲突,忽略并重启web
[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 successful [root@web01 conf.d]# nginx -s reload 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 [root@web01 conf.d]#
#根据ip访问 #1. 用户第一次访问,读取server1.conf配置返回结果 [root@lb01 ~]# curl 10.0.0.5 test1 #2. 此时将server1.conf修改成server4.conf重启nginx [root@lb01 conf.d]# mv server1.conf server4.conf [root@lb01 conf.d]# nginx -s reload #3. 再次访问时,读取server2.conf配置返回结果 [root@lb01 conf.d]# curl 10.0.0.5 test2
再开始处理一个HTTP请求时,Nginx会读取header(请求头)中的host,与每一个server中的server_name进行匹配,来决定用哪个server标签来完成处理这个请求,有可能一个Host与多个server中的server_name都匹配,这个时候就会根据匹配优先级来选择实际处理的server。优先级匹配结果以下:正则表达式
1.首先选择全部的字符串彻底匹配的server_name。(彻底匹配)
2.选择通配符在前面的server_name,如.haoda.com www.haoda.com3.选择通配符在后面的server_name,如bgx. haoda.com haoda.cn
4.最后选择使用正则表达式匹配的server_name
5.若是所有都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.若是没写,那么就找到匹配listen端口的第一个Server块的配置文件vim
当用户经过访问IP或者未知域名访问你得网站的时候,你但愿禁止显示任何有效内容,能够给他返回500,目前国内不少机房都要求网站关闭空主机头,防止未备案的域名指向过来形成麻烦后端
[root@lb01 conf.d]# cat server4.conf server { listen 80 default_server; #默认优先返回; server_name _; #空主机头或者IP; return 500; #直接返回500错误; }
[root@lb01 conf.d]# cat server4.conf server { listen 80 default_server; server_name _; return 302 http://test1.com; 访问10.0.0.5 就会跳转test1.com }
root与alias路径匹配主要区别在于nginx如何解释location后面的uri,这会使二者分别以不一样的方式将请求映射到服务器文件上,alias是一个目录别名的定义,root则是最上层目录的定义。tomcat
root的处理结果是:root路径+location路径alias的处理结果是:使用alias定义的路径bash
[root@web01 conf.d]# cat image.conf server { listen 80; server_name image.zhp.com; location /images { root /code/img; } } [root@web01 conf.d]# mkdir /code/img/images -p [root@web01 conf.d]# cd /code/img/ [root@web01 img]# ll total 8 drwxr-xr-x 2 root root 6 Sep 2 19:27 images [root@web01 img]# cd images/ [root@web01 images]# ll total 0 [root@web01 images]# rz [root@web01 images]# ll total 28 -rw-r--r-- 1 root root 26765 Aug 5 14:55 4.jpg [root@web01 images]#
[root@lb01 conf.d]# cat image.conf server { listen 80; server_name image.zhp.com; location /images { root /code/img; } }
[root@web01 conf.d]# vim image.conf server { listen 80; server_name image.zhp.com; location / { root /code; } location ~* ^/(.*\.(png|jpg|gif))$ { alias /code/images/$1; } }
nginx的try_file路径匹配,Nginx会按顺序检查文件及目录是否存在(根据 root 和 alias 指令设置的参数构造完整的文件路径),并用找到的第一个文件提供服务。在元素名后面添加斜杠 / 表示这个是目录。若是文件和目录都不存在,Nginx会执行内部重定向,跳转到命令的最后一个 uri 参数定义的 URI 中。服务器
#1. 配置nginx [root@lb01 conf.d]# vim try.conf server { listen 80; server_name try.haoda.com; root /code; index index.html; location / { try_files $uri /404.html; } } #2. 建立实例目录与文件 [root@lb01 conf.d]# echo try11111 > /code/index.html [root@lb01 conf.d]# echo '404 404 404' > /code/404.html #3. 尝试访问try.haoda.com [root@lb01 conf.d]# curl try.haoda.com 404 404 404 #因为访问的是try.haoda.com,而$uri取得是域名后面咱们写的内容,它找不到,因此返回后面的内容,即404.html #4. 尝试访问try.haoda.com/index.html [root@lb01 conf.d]# curl try.haoda.com/index.html try11111 #因为访问的是try.haoda.com/index.html,而$uri取到了index.html因此返回/code/index.html的内容 #5. 修改配置为 location / { try_files $uri $uri/ /404.html; } #6. 再次尝试访问try.haoda.com [root@lb01 conf.d]# curl try.haoda.com try11111 #咱们访问的是try.haoda.com,而$uri咱们没有写任何内容,因而他访问的即是“空/”,即匹配到/code/index.html
location /images/ { try_files $uri $uri/ /404.html; }
用户请求try.haoda.com/images/image1.gif,Nginx 会首先经过用于这个 location,在本地目录中查找这个文件。若是“image1.gif”文件不存在,Nginx 会查找“image1.gif/”目录,即“try.haoda.com/images/image1.gif/”,若是都不存在,会重定向到“/404.html”
[root@web02 ~]# yum install -y tomcat 安装tomcat #1. 配置nginx [root@web01 conf.d]# cat try.conf server { listen 80; server_name try.haoda.com; root /code; index index.html; location / { try_files $uri $uri/ @java; #当$uri和$uri/都匹配不到时,由后端的java来进行处理,名字可自定义,但必定要加@ } location @java { 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://try.haoda.com/index.html i am tomcat
error_page错误日志
#error_page配置的是http这种的网络地址 [root@lb01 conf.d]# cat error.conf server { listen 80; server_name www.haoda.com; root /code; #error_page 404 http://www.baidu.com; location / { index index.html; error_page 404 http://www.baidu.com; } }
[root@lb01 conf.d]# cat error.conf server { listen 80; server_name error.haoda.com; root /code; location / { index index.html; } #error_page 403 404 /404.jpg; error_page 403 404 /404.html; location = /404.html { root /code; index index.html; } }