从零开发短视频电商 Nginx一个域名部署多个VUE前端

news/2024/7/7 21:45:22 标签: 前端, nginx, vue.js

这里介绍的是所有请求不管是前端还是后端都是在一个域名下。
但是 但是 但是如果你能控制域名的映射强烈建议你使用多个二级域名来实现

1.打开 Nginx 的配置文件 nginx.conf,该文件通常位于 /etc/nginx/ 或者 /usr/local/nginx/conf/ 中。

2.修改nginx配置如下:

nginx">server {
    listen       80;
    server_name  laker.com www.laker.com; // 设置为自己的域名或 IP 地址
    
    # 官网 http://laker.com
	location / {
            # root类型的注意后面dist不带 "/"
            root   /laker/index/dist; #dist文件的位置(根据自己dist包放置的位置决定) 
			try_files $uri $uri/ /index.html;  #重写 URL,使得所有页面都能正确访问到 index.html
            index  index.html index.htm;
    }
    # 项目一 http://laker.com/mall访问
    location /mall {
    # 项目路径是alias不是root,dist后面有个“/”
            alias  /laker/mall/dist/;
        # 这里重写url,最后的/mall/index.html 跟location呼应
            try_files $uri $uri/ /mall/index.html;
            index  index.html index.htm;
    }
    # 项目二 http://laker.com/mall-admin访问
    location /mall-admin {
        	alias  /laker/mall-admin/dist/;
            index  index.html index.htm;
            try_files $uri $uri/ /mall-admin/index.html; // 重写 URL,使得所有页面都能正确访问到 index.html
    }
    
    # 反向代理后端接口
    # 有跨域示例
    # http://laker.com/mall-api/getUserInfo到http://127.0.0.1:8082/getUserInfo
    # 剔除了路径中的"mall-api"
    location /mall-api/ {
            if ($request_method = 'OPTIONS') {
                add_header Access-Control-Allow-Origin $http_origin;
                add_header Access-Control-Allow-Headers $http_access_control_request_headers;
                add_header Access-Control-Allow-Methods $http_access_control_request_method;
                add_header Access-Control-Allow-Credentials 'true';
                return 204;
            }
            if ($request_method != 'OPTIONS') {
               # 动态$http_origin 或者指定一些域名
                add_header Access-Control-Allow-Origin 'http://localhost:8080' always;
                add_header Access-Control-Allow-Credentials 'true';
            }
            proxy_pass http://127.0.0.1:8082/;
            proxy_connect_timeout 30; #超时时间 单位秒
            proxy_send_timeout 60;
            proxy_read_timeout 60;
     } 
     # 无跨域示例
	 location /mall-admin-api/ {
            proxy_http_version 1.1;
			proxy_set_header Host $http_host;
            proxy_set_header X-Forwarded-Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			#后端服务端口地址:
            proxy_pass http://127.0.0.1:8083/;
		}
}

修改完成后,保存并关闭配置文件。

3.重新加载 Nginx 配置,命令如下:

sudo nginx -s reload

4.在浏览器测试

  • http://laker.com
  • http://laker.com/mall
  • http://laker.com/mall-admin

http://www.niftyadmin.cn/n/5381243.html

相关文章

【nginx实践连载-4】彻底卸载Nginx(Ubuntu)

步骤1:停止Nginx服务 打开终端(Terminal)。停止Nginx服务:sudo systemctl stop nginx步骤2:卸载Nginx软件包 运行以下命令卸载Nginx软件包:sudo apt purge nginx nginx-common nginx-core步骤3&#xff1…

SHERlocked93 的 2023 年终总结

工作之后感觉一年一年过的太快,没有个记录连回忆都无从回忆起,之前的年终总结: SHERlocked93 的 2022 年终总结SHERlocked93 的 2021 年终总结SHERlocked93 的 2020 年终总结SHERlocked93 的 2019 年终总结SHERlocked93 的 2018 年终总结SHER…

PyCharm - Script parameters (脚本参数)

PyCharm - Script parameters [脚本参数] References Run -> Edit Configurations… -> Run/Debug Configurations -> Configuration -> Script parameters 命令行: python display_yolo_log.py ./person_training_log/person_train_log_DIMM40_stdout…

项目开发日志(登录界面):2. LoginTitle组件

LoginTitle组件 样式 说明 属性 属性名含义类型是否必填默认值welcomeTitle欢迎标语String是无mainTitle标题String是无 样式 mainColor -> 主题颜色 代码 <template><div class"logintitle-container"><p class"subtitle">{{ welc…

Leetcode 3040. Maximum Number of Operations With the Same Score II

Leetcode 3040. Maximum Number of Operations With the Same Score II 1. 解题思路2. 代码实现 题目链接&#xff1a;3040. Maximum Number of Operations With the Same Score II 1. 解题思路 这一题的话思路就是一个动态规划&#xff0c;显然对于每一种情况都有3种可能的…

pve系统下从0到1搭建好用的OpenWRT系统

从0到1搭建好用的OpenWRT系统 通过PVE虚拟平台搭建OpenWRT系统在PVE上创建OpenWRT虚拟机下载OpenWRT镜像文件上传镜像到PVE创建虚拟机安装OpenWRT系统修改OpenWRT的ip地址&#xff0c;使得OpenWRT可以被前端访问配置OpenWRT的网关和dns&#xff0c;使系统可以访问外网 修改为国…

使用Docker Compose搭建Redis哨兵架构

搭建Redis哨兵(sentinel) 之前我们通过深入理解REDIS哨兵原理了解了Redis哨兵(sentinel)的原理&#xff0c;今天我们手动部署一个哨兵架构。要在Docker中搭建Redis哨兵(sentinel)架构&#xff0c;需要Redis的主从实例以及哨兵实例。之前我们已经使用Docker Compose搭建Redis主…

探索设计模式的魅力:掌握命令模式-解锁软件设计的‘遥控器’

​&#x1f308; 个人主页&#xff1a;danci_ &#x1f525; 系列专栏&#xff1a;《设计模式》 &#x1f4aa;&#x1f3fb; 制定明确可量化的目标&#xff0c;并且坚持默默的做事。 引言&#xff1a;探索命令模式的奥秘 软件设计领域充满挑战与机遇&#xff0c;命令模式…