nginx rewrite flag Nginx 请求处理流程 + nginx.conf配置文件
0. 基本用法是 rewrite patten replace flag
1. break可以理解为switch中的break,而last可以理解为continue,一个是跳出server{}的匹配规则,一个还将继续匹配之后的规则。
无论使用last还是break,浏览器上面的地址栏不能被改变,而另外两个临时重定向302--redirect、永久重定向--permanent将改变浏览器上地址栏。
2. 官方文档的说明: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
An optional flag parameter can be one of: last stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break stops processing the current set of ngx_http_rewrite_module directives as with the break directive;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://”, “https://”, or “$scheme”;
permanent returns a permanent redirect with the 301 code.
参考:https://cloud.tencent.com/developer/news/65131
3. Nginx 配置的模块结构图
这节课我们介绍了物理机与 Nginx 相关配置的优化,优化的方向无非是对内存、CPU、IO(磁盘 IO 和网络 IO)的优化。
《11|高性能优化:物理机极致优化》
最后再说几个与下游 Web 服务相关的超时配置。
它们分别是 proxy_connect_timeout、proxy_send_timeout、proxy_read_timeout,即连接建立超时时间、发送请求超时时间、读取响应超时时间。
如果不合理设置这些超时时间,就会因为各种网络状况导致 Nginx 与 Web 服务之间数据传输受阻,客户端将会一直等待,体验极差。
所以我们需要根据接口的正常响应时间,设置一个合理的超时时间,等待超过超时配置,再将返回提示给到用户。
具体的设置我也放到汇总配置里了。
#工作进程:根据CPU核数以及机器实际部署项目来定,建议小于等于实际可使用CPU核数 worker_processes 2; #绑核:MacOS不支持。 #worker_cpu_affinity 01 10; #工作进程可打开的最大文件描述符数量,建议65535 worker_rlimit_nofile 65535; #日志:路径与打印级别 error_log logs/error.log error; events { #指定处理连接的方法,可以不设置,默认会根据平台选最高效的方法,比如Linux是epoll #use epoll; #一个工作进程的最大连接数:默认512,建议小于等于worker_rlimit_nofile worker_connections 65535; #工作进程接受请求互斥,默认值off,如果流量较低,可以设置为on #accept_mutex off; #accept_mutex_delay 50ms; } http { #关闭非延时设置 tcp_nodelay off; #优化文件传输效率 sendfile on; #降低网络堵塞 tcp_nopush on; #与客户端使用短连接 keepalive_timeout 0; #与下游服务使用长连接,指定HTTP协议版本,并清除header中的Connection,默认是close proxy_http_version 1.1; proxy_set_header Connection ""; #将客户端IP放在header里传给下游,不然下游获取不到客户端真实IP proxy_set_header X-Real-IP $remote_addr; #与下游服务的连接建立超时时间 proxy_connect_timeout 500ms; #向下游服务发送数据超时时间 proxy_send_timeout 500ms; #从下游服务拿到响应结果的超时时间(可以简单理解成Nginx多长时间内,拿不到响应结果,就算超时), #这个根据每个接口的响应性能不同,可以在每个location单独设置 proxy_read_timeout 3000ms; #开启响应结果的压缩 gzip on; #压缩的最小长度,小于该配置的不压缩 gzip_min_length 1k; #执行压缩的缓存区数量以及大小,可以使用默认配置,根据平台自动变化 #gzip_buffers 4 8k; #执行压缩的HTTP请求的最低协议版本,可以不设置,默认就是1.1 #gzip_http_version 1.1; #哪些响应类型,会执行压缩,如果静态资源放到CDN了,那这里只要配置文本和html即可 gzip_types text/plain; #acccess_log的日志格式 log_format access '$remote_addr - $remote_user [$time_local] "$request" $status ' '"$upstream_addr" "$upstream_status" "$upstream_response_time" userId:"$user_id"'; #加载lua文件 lua_package_path "/Users/~/Documents/seckillproject/demo-nginx/lua/?.lua;;"; #导入其他文件 include /Users/~/Documents/seckillproject/demo-nginx/domain/domain.com; include /Users/~/Documents/seckillproject/demo-nginx/domain/internal.com; include /Users/~/Documents/seckillproject/demo-nginx/config/upstream.conf; include /Users/~/Documents/seckillproject/demo-nginx/config/common.conf; }
用一个例子来演示会更加清晰
分类:
nginx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
2017-11-10 SNAT和DNAT