一、Nginx常见问题

一、nginx多server优先级

在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中的每个server_name进行匹配,以此决定到底由哪一个server来处理这个请求,但nginx如何配置多个相同的server_name,会导致server_name出现优先级访问冲突。
1.准备多个配置文件
[root@web01 /etc/nginx/conf.d]# vim test1.conf
server {
  listen 80;
  server_name localhost test1.com;

  location / {
      root /code/test1;
      index index.html;
  }
}

[root@web01 /etc/nginx/conf.d]# vim test2.conf
server {
  listen 80;
  server_name localhost test2.com;

  location / {
      root /code/test2;
      index index.html;
  }
}

[root@web01 /etc/nginx/conf.d]# vim test3.conf
server {
  listen 80;
  server_name localhost test3.com;

  location / {
      root /code/test3;
      index index.html;
  }
}
[root@web01 conf.d]#
2.配置多个站点文件
[root@web01 /etc/nginx/conf.d]# mkdir /code/test{1..3}
[root@web01 /etc/nginx/conf.d]# echo test1111111111111 > /code/test1/index.html
[root@web01 /etc/nginx/conf.d]# echo test2222222222222 > /code/test2/index.html
[root@web01 /etc/nginx/conf.d]# echo test3333333333333 > /code/test3/index.html

3.配置hosts请求页面

10.0.0.7 test1.com test2.com test3.com

#请求域名时,可以请求到域名对应的页面,请求IP时,返回的页面时配置文件中第一个配置的站点
4.多server优先级总结
#在开始处理一个HTTP请求时,Nginx会读取header(请求头)中的host(域名),与每个server中的server_name进行匹配,来决定用哪一个server标签来完成处理这个请求,有可能一个Host与多个server中的server_name都匹配,这个时候就会根据匹配优先级来选择实际处理的server。优先级匹配结果如下:

1.首先选择所有的字符串完全匹配的server_name。(完全匹配)
2.选择通配符在前面的server_name,如 *.test.com (blog.test.com)
3.选择通配符在后面的server_name,如 www.test.* (www.test.com www.test.cn)
4.最后选择使用正则表达式匹配的server_name
5.如果全部都没有匹配到,那么将选择在listen配置项后加入[default_server]的server块
6.如果没写,那么就找到匹配listen端口的第一个Server块的配置文件
5.多server优先级配置测试
#配置
[root@web01 /etc/nginx/conf.d]# cat server1.conf
server {
  listen 80;
  server_name localhost linux.test.com;

  location / {
      root /code/server1;
      index index.html;
  }
}
[root@web01 /etc/nginx/conf.d]# cat server2.conf
server {
  listen 80;
  server_name localhost *.test.com;

  location / {
      root /code/server2;
      index index.html;
  }
}
[root@web01 /etc/nginx/conf.d]# cat server3.conf
server {
  listen 80;
  server_name localhost linux.test.*;

  location / {
      root /code/server3;
      index index.html;
  }
}
[root@web01 /etc/nginx/conf.d]# cat server4.conf
server {
  listen 80;
  server_name localhost ~^linux\.(.*)\.com$;

  location / {
      root /code/server4;
      index index.html;
  }
}
[root@web01 /etc/nginx/conf.d]# cat server5.conf
server {
  listen 80 default_server;
  server_name localhost;

  location / {
      root /code/server5;
      index index.html;
  }
}

#站点文件
[root@web01 /etc/nginx/conf.d]# echo linux.test.com > /code/server1/index.html
[root@web01 /etc/nginx/conf.d]# echo *.test.com > /code/server2/index.html
[root@web01 /etc/nginx/conf.d]# echo linux.test.* > /code/server3/index.html
[root@web01 /etc/nginx/conf.d]# echo '~^linux\.(.*)\.com$' > /code/server4/index.html
[root@web01 /etc/nginx/conf.d]# echo default_server > /code/server5/index.html
6.测试优先级
#配置hosts
10.0.0.7 linux.test.com

#访问测试
压缩配置,测试
压缩配置,测试

二、nginx禁止IP地址访问网站

1.禁止IP访问直接返回错误

[root@web01 /etc/nginx/conf.d]# vim a_ip.conf
server {
  listen 80 default_server;
  server_name localhost;
  return 500;
}

2.引流的方式跳转页面

[root@web01 /etc/nginx/conf.d]# vim a_ip.conf
server {
  listen 80 default_server;
  server_name localhost;
  rewrite (.*) http://www.baidu.com$1;
   #return 302 http://www.baidu.com$request_uri;
}

3.返回指定内容

[root@web01 /etc/nginx/conf.d]# vim a_ip.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包含的作用是为了简化主配置文件,便于人类可读。

inlcude /etc/nginx/online/*.conf #线上使用的配置
mv /etc/nginx/online/test.conf /etc/nginx/offline/ #保留配置,不启用(下次使用在移动到online中)

四、nginx的root和alias

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

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

1.root配置

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

  location /download {
      root /code;
  }
}

#使用root时,当我请求 http://linux.root.com/download/1.jpg 时,实际上是去找服务器上 /code/download/1.jpg 文件

2.alias配置

[root@web01 ~]# vim /etc/nginx/conf.d/alias.conf 
server {
  listen 80;
  server_name linux.alias.com;

  location /download {
      alias /code;
  }
}

#使用alias时,当我请求 http://linux.root.com/download/1.jpg 时,实际上是去找服务器上 /code/1.jpg 文件

3.企业中的配置

server {
  listen 80;
  server_name image.driverzeng.com;

  location / {
      root /code;
  }

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

#注意:
URL: http://linux.root.com/download/1.jpg
URI: /download/1.jpg
$request_filename: /code/download/1.jpg
$request_uri: /download/1.jpg

五、nginx try_file路径匹配

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

1.配置try_files

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

  location / {
      root /code/try;
       #index index.html index.htm;
      try_files $uri /404.html;
  }
}

2,创建站点

[root@web01 ~]# mkdir /code/try
[root@web01 /code/try]# echo try_file_index.html > index.html
[root@web01 /code/try]# echo '404 404 404' > 404.html

3.访问测试

1.当访问 linux.try.com ,结果返回的是404.html
由于请求的是域名,后面没有URI,所以$uri匹配为空,匹配不到内容则返回404.html

2.当访问 linux.try.com/index.html,返回的结果是 index.html
请求的域名后面URI是/index.html ,所以$uri匹配到 /index.html,返回 /code/try/index.html

4.修改nginx配置

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

  location / {
      root /code/try;
       #index index.html index.htm;
      try_files $uri $uri/ /404.html;
  }
}

1.当访问 linux.try.com ,结果返回的是index.html
请求的是域名,后面没有URI,所以$uri匹配为空
然后 $uri/ 匹配到了 '空'/,相当于请求站点目录,默认返回站点目录下的 index.html
如果仍然匹配不到内容则返回 404.html

5.try_files应用

[root@web01 /code/try]# vim /etc/nginx/conf.d/t_file.conf 
server {
  listen 80;
  server_name linux.try.com;

  location / {
      root /code/try;
       #index index.html index.htm;
      try_files $uri $uri/ @java;
  }

  location @java {
      proxy_pass http://172.16.1.8:8080;
}
}

六、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.跳转到网上

#error_page配置的是http这种的网络地址
[root@web01 conf.d]# cat error.conf
server {
  listen       80;
  server_name linux.error.com;

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

2.跳转本地文件

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

  location / {
      root /code/error;
      index index.html;
      error_page 404 403 /404.jpg;
  }
}

3.访问PHP的错误页面跳转

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

  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) {
          rewrite (.*) http://linux.error.com/404.jpg;
      }
  }
}

第二十章 nginx常见问题的更多相关文章

  1. Linux架构之Nginx 常见问题

    第54章 Nginx常见问题 一.Nginx多Sever优先级 在开始处理一个http请求时,nginx会取出header头中的Host变量,与nginx.conf中每个server的server_n ...

  2. windows 下配置 Nginx 常见问题(转)

    windows 下配置 Nginx 常见问题 因为最近的项目需要用到负载均衡,不用考虑,当然用大名鼎鼎的Nginx啦.至于Nginx的介绍,这里就不多说了,直接进入主题如何在Windows下配置. 我 ...

  3. JavaScript高级程序设计:第二十章

    第二十章 一.语法 JSON的语法可以表示以下三种类型的值: (1)简单值 (2)对象 JSON的对象与javascript字面量有一些不同.例如,在javascript中,前面的对象字面量可以写成下 ...

  4. 第二十章 Django数据库实战

    第二十章 Django数据库实战 第一课 获取单表单数据的三种方式: urls.py中的路由代码: path('busniess',views.busniess), views.py中代码: def ...

  5. linux nginx常见问题及优化,压力测试,tomcat服务器优化

    nginx常见问题 nginx优化全局配置优化[root@web2 nginx]# vim conf/nginx.confuser nobody;worker_processes 1;(与cpu核心数 ...

  6. Gradle 1.12翻译——第二十章. 构建环境

    有关其他已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或访问:http://gradledoc.qiniudn.com ...

  7. “全栈2019”Java多线程第二十章:同步方法产生死锁的例子

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java多 ...

  8. “全栈2019”Java异常第二十章:自定义异常详解

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java异 ...

  9. “全栈2019”Java第二十章:按位与、按位或、异或、反码、位运算

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

随机推荐

  1. python中的n次方表示法 **n

    例题:计算2的n次方,n由用户输入(N次方用**表示)# n=eval(input('手动输入n的值:')) #个人感觉,不确定是int还是float时,用eval来表示,eval后面接表达式# pr ...

  2. 【吴恩达课程使用】anaconda (python 3.7) win10安装 tensorflow 1.8 cpu版

    [吴恩达课程使用]anaconda (python 3.7) win10安装 tensorflow 1.8 目前tensorflow是只支持到python3.6的,anaconda最新版本已经到pyt ...

  3. linux定时删除过期文件

    需求说明 每日凌晨0点定时删除/temp目录下的所有一个月未被访问的文件. 脚本实现 linux 终端输入crontab -e,添加定时任务脚本命令 [root@localhost ~]# cront ...

  4. Spring boot +Thymeleaf 搭建springweb

    对接天猫精灵的时候需要有网关服务器方提供几个页面,服务器已经有了,spring boot的 纯后台的,就加了Thymeleaf   jar包添加几个页面跳转 maven配置 <!-- 引入thy ...

  5. zookeeper(3) 持久化

    zookeeper为了防止,系统宕机或重启导致的数据丢失,会对数据进行定时持久化.有两种持久化方式: 1.为每次事务操作记录到日志文件,这样就可以通过执行这些日志文件来恢复数据. 2.为了加快ZooK ...

  6. 给你的MyBatis-Plus装上批量插入的翅膀

    努力和选择,哪个更重要?关注微信公众号[天开易想]这是一位懂互联网研发和架构的户外.篮球老铁 前言 各位好,我是易哥(thinkYi). 大家有用过MyBatis-Plus(简称MP)的都知道它是一个 ...

  7. Linux常用字段

    cd 切换路径 vim,vi 打开文档 ls  查看文件信息 chmod 修改文件或目录的权限 useradd 添加用户 cat 查看纯文本文件(少内容) rm 删除文件或目录 mv 剪切文件或文件重 ...

  8. vue移动端记录列表滚动如何快速找到是哪个元素产生的滚动

    使用下面的代码粘贴到调试工具中运行一下,然后滚动页面,就可以看到是哪个元素产生的滚动了 function findscroller(element) { element.onscroll = func ...

  9. 使用模拟退火算法优化 Hash 函数

    背景 现有个处理股票行情消息的系统,其架构如下: 由于数据量巨大,系统中启动了 15 个线程来消费行情消息.消息分配的策略较为简单:对 symbol 的 hashCode 取模,将消息分配给其中一个线 ...

  10. JDK 中的栈竟然是这样实现的?

    前面的文章<动图演示:手撸堆栈的两种实现方法!>我们用数组和链表来实现了自定义的栈结构,那在 JDK 中官方是如何实现栈的呢?接下来我们一起来看. 这正式开始之前,先给大家再解释一下「堆栈 ...