high-concurent-load
  • Introduction
  • CDN分流+ 加速
    • CDN工作原理
    • CDN核心概念
    • CDN缓存更新策略
    • CDN实战
    • DNS与CDN联系
  • DNS分流
    • DNS介绍
    • DNS配置实战
    • 域名服务模式介绍
  • LVS
    • LVS核心概念
    • LVS实战
  • Nginx教程
    • nginx常见问题汇总
    • nginx注册系统服务
    • nginx安装
    • nginx基础知识
      • nginx工作原理
      • nginx代理
      • nginx的location匹配规则
      • nginx的location匹配实战
      • nginx负载均衡
      • nginx配置expires
      • nginx配置中指令root和alias的区别浅析
    • nginx实战
      • nginx配置文件引用示例
      • nginx超时时间设置
      • nginx实现请求的负载均衡 + keepalived实现nginx的高可用
      • nginx 在已安装的情况下 安装http_image_filter_module
      • nginx配置单个端口对应多个server
      • nginx proxy_pass 路径匹配、URL 重写
      • nginx+tomcat实现动静分离
Powered by GitBook
On this page
  • 需要注意 proxy_pass 后跟的服务器 URL 是否以 / 结尾。
  • URL 重写

Was this helpful?

  1. Nginx教程
  2. nginx实战

nginx proxy_pass 路径匹配、URL 重写

需要注意 proxy_pass 后跟的服务器 URL 是否以 / 结尾。

举例:Nginx 收到 URL 请求 https://nginx_server_name/hello/world,设置的代理路径为 /hello/(即在 location /hello/ 内设置代理)

不以 / 结尾的被代理服务器收到的请求路径是 /hello/world 以 / 结尾的被代理服务器收到的请求路径是 /world

如果是为了在同一个域名下以不同路径分配不同的 APP 应选择后者以 / 结尾

Nginx 的代理 HTTP 版本默认为 1.0,若要设置为 1.1 则需添加下述配置:

proxy_http_version 1.1;
proxy_set_header Connection "";

URL 重写

以下示例: 用来域名重定向、强制 HTTPS。

if ($host != 'lkxed.cn' ) {
  rewrite ^/(.*)$ https://lkxed.cn/$1 permanent;
}

说明: 其中地址末尾的 $1 是前面 ^/(.*)$ 匹配到的第一个分组,也就是域名后跟的完整路径字符串。当我的重写规则这样配置了之后,若请求路径为 http://www.lkxed.cn/app/hello/world,则它会被重定向到 https://lkxed.cn/app/hello/world。一般都会在目标地址末尾加上 $1,作用是不使路径丢失。

Previousnginx配置单个端口对应多个serverNextnginx+tomcat实现动静分离

Last updated 2 years ago

Was this helpful?