Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

部署方式 #63

Closed
loongzcode opened this issue Apr 3, 2022 · 7 comments
Closed

部署方式 #63

loongzcode opened this issue Apr 3, 2022 · 7 comments

Comments

@loongzcode
Copy link

老哥能不能发一手 nignx的配置吗

@Naccl
Copy link
Owner

Naccl commented Apr 3, 2022

nginx 配置并不是唯一的,需要结合你的部署方式

我只能贴出以下我比较习惯的一种

  • 后端 api 暴露端口为 8070
  • 前端两个 build 后的 dist 使用 express 运行在 pm2 中,cms、view 端口分别为 8071、8072

后端略,具体解释下两个前端,因为我有其它 nodejs 应用,便统一用 pm2 管理,所以这种方式可能并不是最适合你的

前提你的服务器有 nodejs, npm, pm2 这些环境,以 cms 为例

  1. 新建并进入目录 cms

    mkdir cms && cd cms
  2. 将 build 后的 dist 放入 cms 目录下

  3. 新建文件 package.json,内容如下

    {
      "name": "cms",
      "version": "1.0.0",
      "description": "NBlog cms",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "Naccl",
      "license": "MIT",
      "dependencies": {
        "compression": "^1.7.4",
        "connect-history-api-fallback": "^1.6.0",
        "express": "^4.17.1"
      }
    }
  4. 运行 npm install 安装依赖

  5. 新建文件 app.js,内容如下

    const express = require('express')
    const compression = require('compression')
    const history = require('connect-history-api-fallback')
    const app = express()
    
    app.use(compression())
    app.use(history())
    app.use(express.static('./dist'))
    
    app.listen(8071, () => {
      console.log('cms server running at 8071')
    })
  6. pm2 启动服务

    pm2 start app.js --name="nblog-cms"

cms 便运行在 8071 端口下,view 同理,修改 cms -> view 和端口即可

最后 nginx 配置如下(https,高版本 nginx 写法可能为 listen 443 ssl;

server {
  listen 443;
  server_name naccl.top;
  ssl on;

  ssl_certificate     naccl.top.pem;
  ssl_certificate_key naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location / {
    proxy_pass http://127.0.0.1:8072/;
  }
}

server {
  listen 443;
  server_name admin.naccl.top;
  ssl on;

  ssl_certificate     admin.naccl.top.pem;
  ssl_certificate_key admin.naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location / {
    proxy_pass http://127.0.0.1:8071/;
  }
}

server {
  listen 443;
  server_name api.naccl.top;
  ssl on;

  ssl_certificate     api.naccl.top.pem;
  ssl_certificate_key api.naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location /blog/ {
    proxy_pass http://127.0.0.1:8070/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

可以看出写法因人而异,所以一直懒得贴出来🙁

@Naccl Naccl changed the title 老哥能不能发一手 nignx的配置吗 部署方式 Apr 3, 2022
@Naccl Naccl pinned this issue Apr 3, 2022
@Naccl Naccl closed this as completed Apr 3, 2022
@loongzcode
Copy link
Author

谢谢

@loongzcode
Copy link
Author

你好,我前端不做改变,就请求失败,我把前端localhost改成了服务器ip,就只能ip地址加端口号访问了,请问是哪里没配置好吗

@Yuan-Lings
Copy link

老哥你好 请问你的nginx配置好了吗

@loongzcode
Copy link
Author

loongzcode commented Jun 18, 2023 via email

@lcqadmin
Copy link

nginx 配置并不是唯一的,需要结合你的部署方式

我只能贴出以下我比较习惯的一种

  • 后端 api 暴露端口为 8070
  • 前端两个 build 后的 dist 使用 express 运行在 pm2 中,cms、view 端口分别为 8071、8072

后端略,具体解释下两个前端,因为我有其它 nodejs 应用,便统一用 pm2 管理,所以这种方式可能并不是最适合你的

前提你的服务器有 nodejs, npm, pm2 这些环境,以 cms 为例

  1. 新建并进入目录 cms
    mkdir cms && cd cms
  2. 将 build 后的 dist 放入 cms 目录下
  3. 新建文件 package.json,内容如下
    {
      "name": "cms",
      "version": "1.0.0",
      "description": "NBlog cms",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "Naccl",
      "license": "MIT",
      "dependencies": {
        "compression": "^1.7.4",
        "connect-history-api-fallback": "^1.6.0",
        "express": "^4.17.1"
      }
    }
  4. 运行 npm install 安装依赖
  5. 新建文件 app.js,内容如下
    const express = require('express')
    const compression = require('compression')
    const history = require('connect-history-api-fallback')
    const app = express()
    
    app.use(compression())
    app.use(history())
    app.use(express.static('./dist'))
    
    app.listen(8071, () => {
      console.log('cms server running at 8071')
    })
  6. pm2 启动服务
    pm2 start app.js --name="nblog-cms"

cms 便运行在 8071 端口下,view 同理,修改 cms -> view 和端口即可

最后 nginx 配置如下(https,高版本 nginx 写法可能为 listen 443 ssl;

server {
  listen 443;
  server_name naccl.top;
  ssl on;

  ssl_certificate     naccl.top.pem;
  ssl_certificate_key naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location / {
    proxy_pass http://127.0.0.1:8072/;
  }
}

server {
  listen 443;
  server_name admin.naccl.top;
  ssl on;

  ssl_certificate     admin.naccl.top.pem;
  ssl_certificate_key admin.naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location / {
    proxy_pass http://127.0.0.1:8071/;
  }
}

server {
  listen 443;
  server_name api.naccl.top;
  ssl on;

  ssl_certificate     api.naccl.top.pem;
  ssl_certificate_key api.naccl.top.key;

  ssl_session_timeout 30m;
  ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;

  location /blog/ {
    proxy_pass http://127.0.0.1:8070/;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

可以看出写法因人而异,所以一直懒得贴出来🙁

作者求一个联系方式

@lcqadmin
Copy link

大佬?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants