如何配置网站子域名隐藏端口号?

3,335 阅读1分钟

两个react项目:

  • 管理后台
  • 前台页面

分别用 node 部署(也可以把前台项目部署到nginx)

步骤一

设置子域名解析

admin.test.com ->  47.XX.302.XX 

nginx 代理设置

server {
        listen       80;
        server_name  admin.test.com;
		location / {
			proxy_pass http://127.0.0.1:8090;
            index  index.html index.htm;
        }
      
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

构建管理后端项目

npm run build

pm2 启动 express 项目

pm2 start app.js

app.js 文件内容

const path = require('path');
const express = require('express');
const app = express();

const port = 8090;

app.use(express.static(path.join(__dirname, 'dist')));

app.get('/*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`));

访问

输入 admin.test.com 不需要加端口号 即可访问到 node 启动的 8090 端口服务,同时也隐藏了 8090 端口(输入 test.com:8090 端口也是可以访问到 后端管理服务的)