nginx反向代理vue项目

文章目录


前言

项目描述:前端vue项目、后端Java项目、首页WordPress项目

客户要求:使用宝塔进行部署

需求描述:客户只有一个SSL单域名DV证书要求首页部署wordpress项目作为官网,/system为vue项目,/api为java后端项目

一、创建站点

1.添加站点

域名填写客户域名、根目录指向wordpress项目地址(此处带过不是重点)

2.添加ssl证书

将证书key、pem(crt)内容拷贝到内容中保存即可

二、反向代理vue项目

此处遇到的问题:

1.代理后静态文件(.css,.js)访问不到404

2.vue项目访问后端接口访问不到

1.添加反向代理

代理名称随便取

代理目录意思为解析域名后/xxx的转发到目标URL

默认创建如下

bash 复制代码
location ^~ /system
{
    proxy_pass http://xzzzz:9002/;
    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_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    
    
    set $static_filebdpe8eQS 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filebdpe8eQS 1;
    	expires 1m;
        }
    if ( $static_filebdpe8eQS = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

添加下面配置解决静态文件404问题
注意路径后边一定要加/

bash 复制代码
location ^~ /system.*\.(js|css)?$
{
    proxy_pass http://xxxxxx:9002/;
    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_set_header REMOTE-HOST $remote_addr;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    
    
    set $static_filebdpe8eQS 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
    	set $static_filebdpe8eQS 1;
    	expires 1m;
        }
    if ( $static_filebdpe8eQS = 0 )
    {
    add_header Cache-Control no-cache;
    }
}

2.更改vue项目配置

1.更改router,添加/system路径前缀

js 复制代码
export default new Router({
  mode: 'history', // 去掉url中的#
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes,
  base:'/system'
})

2.更改打包后静态文件的访问地址添加/system路径前缀

vue.config.js
如果是vite构建的则修改base字段

3.修改反向代理配置

解决vue跨域问题

bash 复制代码
	location /prod-api/ {
		proxy_set_header Host $http_host;
		proxy_set_header X-Real-IP $remote_addr;
		proxy_set_header REMOTE-HOST $remote_addr;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_pass http://www.xxx.com/api;
	}
相关推荐
阿巴~阿巴~4 小时前
TCP性能优化秘籍:延迟应答、捎带确认与粘包破解之道
运维·服务器·网络·网络协议·udp·tcp
HuaYi_Sir4 小时前
i.MX6ULL移植uboot Linux buildroot(二)
linux·运维·服务器
曲幽4 小时前
从安装到上线:一份 Nginx 实战指南,让你的 Web 应用稳建安全
python·nginx·flask·fastapi·web·gunicorn·uvicorn
仰望.5 小时前
vue 甘特图 vxe-gantt 如何实现标记删除数据,显示标记删除后行效果,获取已标记的行数据
vue.js·甘特图·vxe-ui
航Hang*5 小时前
第五章:网络系统建设与运维(高级)—— VLAN高级特性
运维·服务器·网络·笔记·计算机网络·华为·ensp
小鹏linux5 小时前
【像素贪吃蛇小游戏】部署文档-linux篇
linux·运维·服务器
小白学大数据5 小时前
未来趋势:AI 时代下 python 爬虫技术的发展方向
运维·人工智能·爬虫·python·自动化
小二·5 小时前
【万字源码级剖析】深入理解 Vue 3 响应式系统:ref、reactive、computed 与 effect 的底层实现
前端·javascript·vue.js
成都犀牛5 小时前
Ubuntu配置nginx
linux·nginx·ubuntu
qdprobot6 小时前
开源的在线串口调试助手支持macOS苹果电脑Windows系统Linux 浏览器webSerial
linux·运维·服务器·人工智能·mixly·小智ai·webserial