node部署代理服务器全流程

启动node代理服务器:

  • 安装nvm: 如何安装nvm
  • 使用nvm安装node服务,要求版本14以上
  • 安装node中间件:npm install express http-proxy-middleware
  • node代码,所有访问localhost/api/?的请求都会被转发到https://final_host_site.com/?:
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');

const app = express();

const targetUrl = 'https://final_host_site.com';

// 代理配置
const options = {
  target: targetUrl, // 目标主机
  changeOrigin: true, // 需要虚拟托管站点
  pathRewrite: {
    '^/api': '',
  },
  onProxyReq: (proxyReq, req, res) => {
    console.log(`[Proxy] ${req.method} ${req.path}`);
  },
};

// 使用中间件
app.use('/api', createProxyMiddleware(options));

// 监听端口
const PORT = 3000;
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

使用pm2命令管理程序,保证出问题时可以自动重启

npm install pm2 -g

pm2 start server.js --name myapp

pm2 startup

pm2 save

安装lnmp服务:

wget https://soft.lnmp.com/lnmp/lnmp2.0.tar.gz -O lnmp2.0.tar.gz && tar zxf lnmp2.0.tar.gz && cd lnmp2.0 && ./install.sh lnmp
配置nginx:
server
    {
        listen 80;
        #listen [::]:80;
        server_name www.myhost.com;
            location / {
                    proxy_pass http://localhost:3000;
            }

    }

server
    {
        listen 443 ssl http2;
        #listen [::]:443 ssl http2;
        server_name wishapi.vvip.tech ;

        ssl_certificate /usr/local/nginx/conf/ssl/www.myhost.com/fullchain.cer;
        ssl_certificate_key /usr/local/nginx/conf/ssl/www.myhost.com/www.myhost.com.key;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
        ssl_prefer_server_ciphers on;
        ssl_ciphers "TLS13-AES-256-GCM-SHA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES-128-GCM-SHA256:TLS13-AES-128-CCM-8-SHA256:TLS13-AES-128-CCM-SHA256:EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5";
        ssl_session_cache builtin:1000 shared:SSL:10m;
        # openssl dhparam -out /usr/local/nginx/conf/ssl/dhparam.pem 2048
        ssl_dhparam /usr/local/nginx/conf/ssl/dhparam.pem;
            location / {
                    proxy_pass http://localhost:3000;
            }
                access_log  /home/wwwlogs/www.myhost.com.logs;

    }

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注