zoukankan      html  css  js  c++  java
  • 用Nginx*Node.js

    安装pm2

    npm install pm2 -g
    
    ln -s /home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 /usr/local/bin/pm2
    

    修改package.json

    "scripts": {
        "test": "echo "Error: no test specified" && exit 1",
        "pm2": "/home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 start /web/mazey.cn/server/app.js"
    }
    
    or
    
    "scripts": {
        "test": "echo "Error: no test specified" && exit 1",
        "pm2": "pm2 start app.js"
    }
    

    启动pm2

    npm run pm2
    

    开机启动pm2

    pm2 save
    
    pm2 startup centos
    

    注意

    pm2 startup centos失败,可尝试pm2 startup

     PM2 detected systemv but you precised centos
     Please verify that your choice is indeed your init system
     If you arent sure, just run : pm2 startup
    

    修改Nginx配置

    vim /etc/nginx/conf.d/*.conf
    
    upstream nodejs {
        server 127.0.0.1:3000;
        keepalive 64;
    }
    server {
        listen 80;
        server_name domain.cn;
        root /web/mazey.cn;
        index index.html index.htm;
        # 网站切到/server下时走nodejs
        location /server {
            proxy_set_header    X-Real-IP $remote_addr;
            proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header    Host  $http_host;
            proxy_set_header    X-Nginx-Proxy true;
            proxy_set_header    Connection "";
            proxy_pass http://nodejs;
        }
        location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ {
            expires 30d;
        }
        location ~ .*.(js|css)?$ {
            expires 1h;
        }
    }
    
    

    相应的 app.js

    const express = require('express')
    const app = express()
    let hi = 'hi'
    
    app.get('/server', (req, res, next) => {
      hi = `Hello Mazey!
    `
      next()
    }, (req, res) => {
      res.send(`
      ${hi}
      ${req.method}
    
      ${req.originalUrl}
    
      ${req.query.id}
    
      `)
    })
    
    const server = app.listen(3000, function () {
      let host = server.address().address
      let port = server.address().port
    
      console.log('Example app listening at http://%s:%s', host, port)
    })
    

    注意

    若报错 Cannot GET /xxx 说明 Express 的路由没配好。

  • 相关阅读:
    jsp-servlet(2)响应HTML文档-书籍管理系统
    jsp-servlet(1)环境搭建(Tomcat和myeclipse)和基本概念
    MySQL(2)数据库 表的查询操作
    MySQL(1) 基本操作(MySQL的启动,表的创建,查询表的结构和表的字段的修改)
    Java 构造器Constructor 继承
    数据结构:链表的操作
    C++指针易错点梳理
    C++操作符重载
    无重复字符的最长子串-LeetCode-第3题-C++
    计算几何模板存储
  • 原文地址:https://www.cnblogs.com/mazey/p/8983160.html
Copyright © 2011-2022 走看看