Nginx常见问题

一、server_name优先级

server {
    listen       80;
    server_name  testserver1 node1;

    location / {
        root   /opt/app/code1;
        index  index.html index.htm;
    }
}

server {
    listen       80;
    server_name  testserver2 node2;

    location / {
        root   /opt/app/code2;
        index  index.html index.htm;
    }
}

比如说两个nginx配置文件如上,一个是test1.conf,一个是test2.conf,配置相同的server_name,即使产生了冲突,nginx的启动也不会有问题。
对于多个虚拟主机,配置有相同的server_name的话,只是优先读取先读取到的配置(test_server1.conf)。对于IP配置也相同。

二、location优先级

location匹配的优先级从上往下是:优先匹配最精准的。

  • = 进行普通字符精确匹配,完全匹配(匹配到则不继续查找匹配)
  • ^~ 表示普通字符匹配,使用前缀匹配(匹配到则不继续查找匹配)
  • ~~* 表示执行一个正则匹配 (匹配到则继续查找更精确的匹配)
location = /code1/ {
        rewrite ^(.*)$ /code1/index.html break;
    }
    location ~ /code.* {
        rewrite ^(.*)$ /code3/index.html break;
    }
    location ^~ /code {
        rewrite ^(.*)$ /code2/index.html break;
    }

三、try_files使用

作用:按顺序检查文件是否存在。

location / {
    try_files $uri $uri//index.html
}
  • 首先查看uri内容本地是否存在,存在的话则解析返回给用户
  • 没有的话将请求加/,表示将请求进行重定向处理,寻找这个路径下有没有文件
  • 如果还是没有,则交给index.html来处理
location / {
    root /opt/app/code/cache;
    try_files /cache $uri @java_page;
}


location @java_page{
    proxy_pass http://127.0.0.1:9090;
}

http://192.168.x.xxx/index.html

 - 先请求/opt/app/code/cache这个路径下文件是否存在
 - 不存在则请求@java_page对应的location,转发到http://127.0.0.1:9090进行处理

四、alias和root区别

  1. root
location /request_path/image/ {
    root /local_path/image;
}

// 请求:http://www.imooc.com/request_path/image/cat.png
/local_path/image/request_path/image/cat.png
  1. alias
location /request_path/image/ {
    alias /local_path/image;
}

// 请求:http://www.imooc.com/request_path/image/cat.png
/local_path/image/cat.png

五、获取用户真实IP

在这里插入图片描述

六、Nginx常见错误码

  • 用户上传文件限制:client_max_body_size
    Nginx:413 Request Entity Too Large

  • 后端服务无响应:502 bad gateway

  • 后端服务响应超时:504 Gateway Time-out。默认60s

  • 403:访问被拒绝

  • 404:文件没找到

  • 400:请求参数错误

Nginx常见问题 :
https://mp.weixin.qq.com/s/Ly9yaCloL46PRwyp42Udng

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值