NoteZ_技术博客 NoteZ_技术博客
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)

NoteZ_技术博客

前端界的小学生
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)
  • Web技术

  • Git笔记

  • Linux文档

  • Markdown

  • Nginx文档

    • Nginx 常用配置清单
    • Nginx 配置404错误页面重定向
    • Nginx 配置文件(nginx.conf)
    • Nginx二级目录反向代理网站
    • Nginx出现403跨域后端解决方法
    • Nginx访问权限控制
    • windows下安装nginx和基本配置
    • 在 Linux 下配置 Nginx 方法(启动、停止与重启)
    • 基于wujie(无界)和 Nginx 同一端口下部署多个 Vue 项目
    • 解决 Nginx 下 history 模式面刷新空白(404)问题
    • nginx配合webpack打包实现二级目录访问
    • Nginx通过二级目录映射不同的反向代理
    • Nginx配置WebSocket
    • Nginx配置维护页面
    • Nginx安装ngx_http_ssl_module模块使其支持SSL_https
  • Npm笔记

  • 技术文档

  • 其他文档

  • 前端开发
  • Nginx文档
NoteZ
2020-02-19

Nginx二级目录反向代理网站

场景:域名A下面通过二级目录来匹配不同隐式代理的域名。

server {
    listen 80;
    server_name dev-we-show.fonzie.com;
    location / {
         #index index.html index.htm;
         #root html/;
         #proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://dev_b;
         expires      -1;
         proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;
       #rewrite (.*)/wapindex(.*) /vperson$1/wapindex$2;
       #rewrite (.*)/image(.*) /vperson$1/image$2;
       #rewrite (.*)/js(.*) /vperson$1/js$2;
}

    location /vperson/ {
       proxy_pass https://www.vperson.com/;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

通过域名 dev-we-show.fonzie.com/vperson/,nginx会匹配到反向代理为https://www.vperson.com/的域名上去,因为使用的是绝对引用,这里如果使用相对应用会使https://www.vperson.com/域名变成https://www.vperson.com/vperson/最终访问失败。https://www.vperson.com的首页为:https://vperson.com/wapindex.但是域名dev-we-show.fonzie.com的根下面没有路径wapindex。

所以这里需要使用重定向进行替换,匹配路径为/,打开上面的井号内容,如下:

server {
    listen 80;
    server_name dev-we-show.fonzie.com;
    location / {
         #index index.html index.htm;
         #root html/;
         #proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         proxy_pass http://dev_b;
         expires      -1;
         proxy_connect_timeout 300;
        proxy_send_timeout 300;
        proxy_read_timeout 300;

       rewrite (.*)/wapindex(.*) /vperson$1/wapindex$2;
       rewrite (.*)/image(.*) /vperson$1/image$2;
       rewrite (.*)/js(.*) /vperson$1/js$2;
}
    location /vperson/ {
       proxy_pass https://www.vperson.com/;
    }

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

这样一来在反向代理的时候,用户首先通过dev-we-show.fonzie.com/vperson/访问这台nginx,再通过这台nginx反向代理到https://www.vperson.com/,但这个跳转是隐式的,所以在浏览器的地址栏还是dev-we-show.fonzie.com,问题在于dev-we-show.fonzie.com下面并没有/wapindex的路径,所以我们需要在根/下面添加rewrite通过正则匹配修改,并添加以后路径,然他回去的时候依然是走dev-we-show.fonzie.com/vperson/的路径,而不是dev-we-show.fonzie.com的路径。

vite 项目 router

import { createRouter, createWebHistory ,createWebHashHistory} from 'vue-router'
const routes = [
  {
    path: '/',
    name: 'dendrogram',
    component: () => import('@/views/dendrogram/Index.vue')
  }
];

const router = createRouter({
  history: createWebHashHistory(),
  // history: createWebHistory(),
  base: import.meta.env.BASE_URL,
  routes,
});
export default router;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

vite.config.js

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    }
  },
  base: './',//在生产中服务时的基本公共路径。
})

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#Nginx
上次更新: 2024/01/30, 00:35:17
Nginx 配置文件(nginx.conf)
Nginx出现403跨域后端解决方法

← Nginx 配置文件(nginx.conf) Nginx出现403跨域后端解决方法→

最近更新
01
Gitea数据备份与还原
03-10
02
Linux 中使用 rsync 同步文件目录教程
03-10
03
Linux 使用 rsync 互相传输同步文件的简单步骤
03-08
更多文章>
Theme by Vdoing | Copyright © 2019-2025 NoteZ,All rights reserved | 冀ICP备2021027292号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式