nginx动态匹配路由

背景

我想实现xx.xx.com/mac/tengxun 其中tengxun为客户代码,无论什么穿什么动态客户代码我就是需要跳转到相同的html中

server {
    
    
    listen 80;  # 监听80端口
    server_name client.skwitech.com;
	
		# 动态匹配路径:/mac/{client_code}/index.js
		location ~ ^/mac/([^/]+)/index\.js$ {
    
    
			alias /var/index.js;  # 指向具体的 JS 文件
		}
	
		# 动态匹配路径:/mac/{client_code}/
		location ~ ^/mac/([^/]+)/$ {
    
    
			rewrite ^/mac/([^/]+)/$ /var/index.html break;  # 重写为静态文件路径
			proxy_pass http://127.0.0.1:180;  # 只包含协议和主机,不带路径
			proxy_set_header Host $host;  # 保持原有主机头
			proxy_set_header X-Real-IP $remote_addr;  # 转发客户端真实 IP
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  # 转发原始请求 IP
			proxy_set_header X-Forwarded-Proto $scheme;  # 转发协议(http或https)
			proxy_redirect off;  # 关闭自动重定向
		}
	}

![在这里插入图片描述] Alt

猜你喜欢

转载自blog.csdn.net/AdminPwd/article/details/143157304