Nginx性能优化系列 | Nginx的location规则配置详解
- 1. Nginx设置过滤条件
 
1. Nginx设置过滤条件
如果请求一个不存在网站接口路径,为避免被听云检测到过多错误次数触发告警,可以在Nginx层面设置对错误的请求路径直接返回200正确码
# vim /usr/local/nginx/conf/nginx.conf
    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /new1111 {              # 添加上此段内容,/new1111是一个不存在的接口路径
        add_header Content-Type "text/plain;charset=utf-8";
        return 200 "hello";                # 200后面的内容可以随意写,返回200状态码而且还输出对应的内容
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
 
   add_header是一个指令,用于在服务器响应客户端请求时动态添加或设置HTTP头。在Nginx的配置文件中add_header Content-Type 'text/html; charset=utf-8';,这条指令告诉Nginx服务器对于所有响应添加一个Content-Type头,指定内容类型为text/html,并且字符集是utf-8。
# /usr/local/nginx/sbin/nginx -s reload
 

查看下Nginx服务端的日志输出,对于一个不存在的请求路径,输出状态码就输出200了,这样就不会被监控平台检测到异常的请求(如果有非200的请求码监控平台会检测到)
# tail -f /var/log/nginx/access.log 
192.168.223.63 - - [01/May/2024:21:08:42 +0800] "GET /new11111 HTTP/1.1" 200 5 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
                
![[牛客网]——C语言刷题day3](https://img-blog.csdnimg.cn/direct/9c77aa01fd304b11a0e1020f76cdc9b2.png)
















![npm install [Error]](https://img-blog.csdnimg.cn/direct/73ad93f3dd36417b838f58363576b8a1.png)
