一个端口配置两个vue和后端服务,nginx以及前后端服务怎么配?

nginx配置重点看server中的内容:

worker_processes  8;
pid /usr/local/nginx/logs/nginx.pid;

events {
    # 此为 Linux 系统特为处理大批量文件描述符而作改进的 poll 事件模型
    use epoll;
    worker_connections  512; # 工作进程的最大连接数量
    # 允许同时接受多个网络连接
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
    keepalive_timeout  65;
    include       gzip.conf;
    server_names_hash_bucket_size 128;
    server_names_hash_max_size 512;
    client_header_timeout 15s;
    client_body_timeout 15s;
    send_timeout 30s;
    client_body_buffer_size 512k;
    client_header_buffer_size 4k;
    client_max_body_size 512k;
    large_client_header_buffers 2 8k;
    proxy_connect_timeout 5s;
    proxy_send_timeout 120s;
    proxy_read_timeout 120s;
    proxy_buffer_size 16k;
    proxy_buffers 4 64k;
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;
    proxy_next_upstream http_502 http_504 http_404 error timeout invalid_header;

    #gzip on;
    #gzip_min_length 1k;
    #gzip_buffers 4 16k;
    #gzip_http_version 1.1;
    #gzip_comp_level 4;
    #gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;
    #gzip_vary on;
    #gzip_disable "MSIE [1-6].";

    server {
        listen       80;
        server_name  localhost;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        
        # 第一个服务的vue项目位置
        location / {
                alias /usr/data/nginx/firstFront/;
		try_files $uri $uri/ /index.html;
	}

        # 第二个服务的vue项目位置
	location /second {
		alias /usr/data/nginx/secondFront/;
		try_files $uri $uri/ /second /index.html;
	}

        # 第一个服务的后端项目
	location ^~ /back {
		# 配置websocket -------start---------
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		# 配置websocket -------end---------
		client_max_body_size 1000M;
		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 Via "nginx";
		proxy_intercept_errors on; #重要,只有这样才能到nginx定义的404页面
		proxy_pass              http://localhost:8080/back/; # 后端项目地址
	}

        # 第二个服务的后端项目
	location ^~ /back/second{
		# 配置websocket -------start---------
		proxy_http_version 1.1;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection "upgrade";
		# 配置websocket -------end---------
		client_max_body_size 1000M;
		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 Via "nginx";
		proxy_intercept_errors on; #重要,只有这样才能到nginx定义的404页面
		proxy_pass              http://localhost:8081/back/; # 后端项目地址
	}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
                root   html;
        }
    }
}

vue前端项目配置

vue第一个项目配置:

# 发布路径
VITE_PUBLIC_PATH = /
#后台接口父地址(必填)
VITE_GLOB_API_URL=/back
#后台接口全路径地址(必填)
VITE_GLOB_DOMAIN_URL=/back

vue第二个项目配置

# 发布路径
VITE_PUBLIC_PATH = /second
#后台接口父地址(必填)
VITE_GLOB_API_URL=/back/second
#后台接口全路径地址(必填)
VITE_GLOB_DOMAIN_URL=/back/second

访问地址

第一个项目:http://domain:80/
第二个项目:http://domain:80/second

<think>嗯,用户想在同一个Nginx服务器上通过一个端口部署多个Vue前后端分离应用。我需要想想怎么实现这个。首先,Vue应用通常是前端静态文件,后端可能是不同的API服务。用户提到只开放一个端口,所以可能需要用Nginx的反向代理或者路径区分不同的应用。 首先,考虑多个前端应用的情况。通常部署静态文件可以通过不同的server块,但用户只用一个端口,所以可能需要用location来区分路径。比如,把每个前端应用放在不同的子路径下,比如/app1、/app2,然后Nginx的root或alias指向各自的dist目录。不过Vue的路由模式如果是history模式的话,可能需要额外的置,比如try_files,避免刷新后404的问题。 然后是后端部分。前后端分离的话,前端需要调用后端API。如果每个应用的后端服务运行在不同的端口,但外部只能通过一个端口访问,那可能需要用Nginx的反向代理,根据路径将请求转发到不同的后端服务。比如,将/api/app1的请求代理到本地的3001端口,/api/app2代理到3002端口。这样前端在调用API时,统一通过同一个端口的不同路径访问各自的后端。 还需要注意跨域问题,不过如果前后端都通过同一个Nginx代理,可能不需要额外处理跨域,因为请求会被Nginx转发,同源策略就不会触发。但需要确保前端请求的API路径正确置了代理。 另外,每个Vue应用在构建时需要设置publicPath,也就是基础路径,这样静态资源才能正确加载。比如,应用1的publicPath设为/app1/,应用2设为/app2/。否则,资源路径可能错误,导致JS或CSS加载失败。 还有,Nginx置中的aliasroot的区别需要注意。使用alias时,路径需要完全匹,而root则是将location路径追加到root路径后。可能需要用alias来精确指定静态文件的目录结构。 举个例子,用户有两个Vue应用,app1app2,各自构建后的文件放在不同的目录。Nginx置里,设置location /app1/ 的alias指向app1的dist,location /app2/ 指向app2的dist。同时,置对应的后端API代理,比如location /api/app1/ 代理到后端服务。 此外,SSL置如果有的话,也需要统一处理,因为只有一个端口,可能用443或80,然后根据域名或路径来区分不同的应用。但用户可能没有提到域名,所以可能用路径区分更合适。 总结一下步骤应该是:1. 构建Vue应用时设置publicPath为各自的子路径。2. Nginx的location块,用alias或root来指定静态资源目录。3. 后端API的反向代理路径。4. 处理Vue的history模式问题,使用try_files。5. 检查静态资源是否正确加载,以及API代理是否正常工作。 可能遇到的问题包括:路径置错误导致资源404,history模式的路由问题,API代理路径不正确,或者缓存问题需要置缓存策略。另外,可能需要重新加载Nginx置,每次修改后都要nginx -s reload。 用户可能需要具体的置示例,所以在回答时要给出一个示例置,涵盖前端后端置部分。还要提醒用户注意Vue项目的置修改,比如vue.config.js中的publicPath,以及路由器的base设置。</think>可以通过 **Nginx 的路径区分(子路径)或反向代理** 实现单端口部署多个前后端分离应用。以下是具体方案: --- ### 📌 核心思路 1. **前端**:将不同 Vue 应用部署在 Nginx 的不同子路径下(如 `/app1`、`/app2`)。 2. **后端**:通过路径前缀区分不同后端服务(如 `/api/app1`、`api/app2` 代理到不同后端端口)。 3. **统一端口**:所有流量通过 `80/443` 等单端口对外暴露。 --- ### 🛠️ 实现步骤 #### 1. 前端 Vue 项目置 - **修改 `vue.config.js`**,设置 `publicPath` 为子路径(关键步骤!): ```javascript module.exports = { publicPath: '/app1/', // 应用1设为 /app1/,应用2设为 /app2/ } ``` - **构建项目**,生成静态文件(`npm run build`),输出到不同目录(如 `dist_app1`、`dist_app2`)。 #### 2. Nginx 置示例 ```nginx server { listen 80; server_name your-domain.com; # 部署第一个应用:路径 /app1 location /app1/ { alias /path/to/dist_app1/; # 使用 alias 精确匹路径 index index.html; try_files $uri $uri/ /app1/index.html; # 解决 Vue History 模式 404 问题 } # 部署第二个应用:路径 /app2 location /app2/ { alias /path/to/dist_app2/; index index.html; try_files $uri $uri/ /app2/index.html; } # 代理后端服务(假设应用1的后端运行在 3001,应用2在 3002) location /api/app1/ { proxy_pass http://localhost:3001/; # 末尾的 / 会去除 /api/app1 前缀 proxy_set_header Host $host; } location /api/app2/ { proxy_pass http://localhost:3002/; proxy_set_header Host $host; } } ``` -- ### 🔍 注意事项 1. **路由模式**: - 若 Vue 使用 `history` 模式,确保 `vue-router` 的 `base` 与 `publicPath` 一致: ```javascript const router = new VueRouter({ mode: 'history', base: '/app1/' // 与应用 publicPath 一致 }) ``` 2. **静态资源路径**: - 检查 CSS、JS 文件是否加载正确,路径错误通常是 `publicPath` 未置或 Nginx 的 `alias/root` 置不当。 3. **后端 API 调用**: - 前端代码中,API 请求地址需匹代理路径(如 `/api/app1/user` 会被 Nginx 转发到 `http://localhost:3001/user`)。 4. **重启 Nginx**: ```bash nginx -s reload ``` --- ### 🌰 实际访问示例 - **应用1**:`http://your-domain.com/app1/` - **应用2**:`http://your-domain.com/app2/` - **应用1 的 API**:前端请求 `/api/app1/data` → Nginx 转发到 `http://localhost:3001/data` 通过这种方式,单端口即可实现多应用共存,且前后端完全分离。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Counter-Strike大牛

创作不易,感谢鼓励。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值