nginx域名隐性(地址栏域名不变)跳转
###
1、前提
系统介绍 系统A 域名: http://yhjmp.test.com 部署服务器:192.168.56.20
服务器名称:A-system 系统B 域名: http://yh.test.com或者http://yh.test.com/B-system【本文做了301永久跳转】 部署服务器:192.168.56.140
服务器名称:B-system 系统B附属系统 域名: http://yh.test.com/B-system-bak 部署服务器:192.168.56.140
服务器名称:B-system 需求 两套系统A、B使用统一域名 http://yhjmp.test.com
点击跳转按钮,可访问两套系统中内容,域名不变
示例:浏览器地址栏中输入yhjmp.test.com域名不变访问的内容为yh.test.com上的内容

2、B系统-前端代码
[root@B-system html]# pwd /usr/share/nginx/html [root@B-system html]# tree -L 2 . ├── B-system │ └── index.html ├── B-system-bak └── index.html [root@B-system html]# cat B-system/index.html <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <h1> B ~ 系统</h1> <button onclick="window.location.href='/A-system/index.html'" type="button" id="add">跳转A系统</button> <br /><br /> <button onclick="window.location.href='/B-system/index.html'" type="button" id="add">跳转B系统</button> <br /><br /> <button onclick="window.location.href='/B-system-bak/index.html'" type="button" id="add">跳转B系统附属系统</button> <br /><br /> [root@B-system html]# cat B-system-bak/index.html <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <h1> B ~ 系统 ~ 附属系统</h1> <button onclick="window.location.href='/A-system/index.html'" type="button" id="add">跳转A系统</button> <br /><br /> <button onclick="window.location.href='/B-system/index.html'" type="button" id="add">跳转B系统</button> <br /><br /> <button onclick="window.location.href='/B-system-bak/index.html'" type="button" id="add">跳转B系统附属系统</button> <br /><br />
3、A系统-前端代码
[root@A-system html]# pwd /usr/share/nginx/html [root@A-system html]# tree -L 2 . ├── A-system └── index.html [root@A-system html]# cat A-system/index.html <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <h1> A ~ 系统</h1> <button onclick="window.location.href='/A-system/index.html'" type="button" id="add">跳转A系统</button> <br /><br /> <button onclick="window.location.href='/B-system/index.html'" type="button" id="add">跳转B系统</button> <br /><br /> <button onclick="window.location.href='/B-system-bak/index.html'" type="button" id="add">跳转B系统附属系统</button> <br /><br />
4、B系统nginx配置
[root@B-system ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log notice; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; rewrite_log on; error_log /var/log/nginx/error.log notice; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { server_name yh.test.com; location /B-system { root /usr/share/nginx/html; try_files $uri $uri/ /B-system/index.html; index index.html index.htm; } location /B-system-bak{ root /usr/share/nginx/html; try_files $uri $uri/ /B-system-bak/index.html; index index.html index.htm; } location / { rewrite ^/(.*) http://yh.test.com/B-system permanent; } } }
5、A系统nginx配置
[root@A-system ~]# cat /etc/nginx/nginx.conf # For more information on configuration, see: # * Official English Documentation: http://nginx.org/en/docs/ # * Official Russian Documentation: http://nginx.org/ru/docs/ user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic. include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 4096; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { server_name yhjmp.test.com; location /A-system { root /usr/share/nginx/html; try_files $uri $uri/ /A-system/index.html; index index.html index.htm; } # 域名不变访问B系统资源 location /B-system { proxy_next_upstream error timeout http_503 http_504 http_502; proxy_connect_timeout 500s; proxy_read_timeout 500s; proxy_send_timeout 500s;
# 本次测试做了本地host解析。
# 生产环境未做host解析的两套系统需注释proxy_set_header Host $host;这行代码
#【proxy_set_header Host $host;含义:会代理到本机主机名下的/B-system目录寻找资源,如果没有则报错404】
#【404报错原因:如果客户端请求头中没有携带这个头部,那么传递到后端服务器的请求也不含这个头部。 这种情况下,更好的方式是使用$host变量——它的值在请求包含“Host”请求头时为“Host”字段的值,在请求未携带“Host”请求头时为虚拟主机的主域名】 # proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 重写url:(.*)括号面的.* 既是后面的$1。此location中^(.*)$:则是代表匹配url中/B-system/后面的全部内容 rewrite ^(.*)$ /$1 break; #先改写URI地址
# 反向代理地址:写上域名即可,访问时后面会自行带上/B-system/$1的url proxy_pass http://yh.test.com; } location / { rewrite ^/(.*) http://yhjmp.test.com/A-system permanent; } } }
6、单独访问B系统效果(无法访问到A系统)

7、使用同一域名访问效果(访问A/B系统)

###
分类:
N===nginx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律