• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

node+express编写后端接口,部署到服务器上,并配置nginx+ssl证书,实现httphotoshop访问

武飞扬头像
一溢孤行
帮助1

零、开始前的准备

  • window 要装有 node和npm还有git工具
  • 服务器(阿里云或腾讯云都可)
  • 域名(备案过的)

以上准备没做好的请自行上网查询准备

一、在本地使用node express编写后端接口

express简介及入门教程

1.新建一个项目文件夹

学新通

2.使用vscode打开

学新通

3.安装express框架

3.1初始化npm(出现提示后一直回车即可)

npm init

运行结果:
学新通
运行完之后会出现一个package.json的配置文件,说明初始化npm成功

4.安装express

npm i express

5.一个test.js文件,编写一个简单的 get/post 接口

// 1.引入框架
// node项目采用 common.js,不能采用 import 引入,只能用 require 方式
const {
  response
} = require('express')
const express = require('express')

// 2.创建应用
const app = express()

// 3.创建路由规则
// get请求
app.get('/hello', (request, response) => {
  // 设置响应头 设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  // 设置响应体
  response.send('Hello node')
})

// post请求
app.post('/server', (request, response) => {
  // 设置响应头 设置允许跨域
  response.setHeader('Access-Control-Allow-Origin', '*')
  // 设置所有相应头可用
  response.setHeader('Access-Control-Allow-Headers', '*')
  let query = request.query
  // 设置响应体,返回请求参数
  response.status(200).send(query)
})

// 4.监听端口启动服务
app.listen(8280, () => {
  console.log('服务已经启动,8280端口监听中...,点击http://localhost:8280');
})
学新通

6.运行

在命令行终端输入 node .\test.js 运行node项目
学新通
6.1 测试
学新通

这里的测试工具用的是postman

7.扩展 – 使用nodemon

nodemon用来监视node.js应用程序中的任何更改并自动重启服务,非常适合用在开发环境中。
nodemon将监视启动目录中的文件,如果有任何文件更改,nodemon将自动重新启动node应用程序。

7.1 安装(建议全局安装)

npm install -g nodemon

二、部署到服务器上

1.先将项目部署上传到git仓库中

1.1 新建仓库

学新通

1.2 复制链接

学新通

1.3 上传

依次输入上面的命令
学新通

2.在服务器上拉取项目代码

如果服务器上没有git,要先下载安装
如何在服务器上安装git
学新通
进入项目文件夹

cd node-test

如果服务器上有node,可以直接直接用node运行项目看看
学新通

3.安装pm2,管理node项目

PM2(Process Manager 2 )是具有内置负载均衡器的Node.js应用程序的生产运行时和进程管理器。 它允许您永久保持应用程序活跃,无需停机即可重新加载它们,并促进常见的Devops任务。

3.1 全局安装pm2
npm install pm2 -g

如何在服务器上安装npm和node
pm2命令基本使用
3.2 安装好后使用pm2启动node项目
学新通
可以看到我这里启动了两个node项目
至此,就可以使用你的服务器ip 端口号访问接口了

注意:想通过ip 端口直接访问的话,必须关闭服务器的防火墙,否则访问不了
关闭防火墙命令:systemctl stop firewalld.service

学新通

三、nginx配置域名

1.准备好一个二级域名

登录你的域名管理后台去解析一个二级域名
学新通

2.在服务器上安装nginx

Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。

由于我之前已经安装过nginx,为了演示操作,现在将nginx卸载然后再重新安装

2.1 卸载nginx

2.1.1 首先找到所有的nginx文件夹

find / -name nginx

学新通

2.1.2 删除所有nginx文件

删除之前最好先关闭nginx服务
进入nginx的sbin目录,然后执行
./nginx -s stop

查看nginx运行命令

ps -ef | grep nginx

学新通
删除所有nginx文件

rm -rf /usr/local/nginx /usr/local/nginx/nginx-1.13.7/objs/nginx /usr/local/nginx/sbin/nginx /var/lib/yum/repos/x86_64/7/nginx /var/cache/yum/x86_64/7/nginx /www/server/panel/vhost/nginx
2.2 安装nginx

2.1.1 先查看自己的服务器上是否已经安装 gccpcrezlibopenssl,如果没有安装则先安装

gcc、pcre、zlib、openssl 简介:
gcc是linux下的编译器,它可以编译 C,C ,Ada,Object C和Java等语言
pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
openssl是web安全通信的基石

查看命令:

gcc -v
rpm -qa pcre
yum list installed | grep zlib*
rpm -qa openssl

安装命令:

yum -y install gcc
yum install -y pcre pcre-devel
yum install -y zlib zlib-devel
yum install -y openssl openssl-devel

2.1.2 安装nginx

cd /usr/local/
wget http://nginx.org/download/nginx-1.1.10.tar.gz
tar -zxvf nginx-1.1.10.tar.gz
cd nginx-1.1.10
./configure
make
make install

2.1.3 启动nginx

cd /usr/local/nginx/sbin
./nginx
# 查看 nginx 配置文件是否正确
./nginx -t
# 如果输出为以下两行,则正确
# nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
# nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 

2.1.4 nginx的其他命令

# 以下命令均在 nginx 的 sbin 目录下执行
# 重启nginx服务
./nginx -s reload
# 停止 nginx 服务
./nginx -s stop
# 查看 nginx 运行状态
ps -ef | grep nginx

3.配置域名

3.1 检查防火墙端口是否开放

这里要开放80端口,因此可以使用防火墙命令查看80端口是否开放
如果防火墙已经关闭的话,要先开启防火墙

systemctl start firewalld

查看80端口开放情况

firewall-cmd --query-port=80/tcp

如果未开放则使用以下命令开放

firewall-cmd --zone=public --add-port=80/tcp --permanent

最后重启防火墙

firewall-cmd --reload
3.2 修改 nginx.conf 配置

切换到 /usr/local/nginx/conf/ 目录
修改 nginx.conf 文件
找到 server 服务中 listen 80的代码段
更改 server_namelocation 字段为自己的域名和 loaclhost 端口服务即可
原文件内容:


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        #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;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
学新通

修改配置后的文件内容

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
	  listen 80;
	  server_name test.xxxxxx.com; # 此处填写你分配的二级域名
	  location / {
		  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://127.0.0.1:8280;  # 你本机的提供http服务的地址
	  }
	}

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443;
    #    server_name  localhost;

    #    ssl                  on;
    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_timeout  5m;

    #    ssl_protocols  SSLv2 SSLv3 TLSv1;
    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers   on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
学新通

配置完后回到 sbin 目录查看配置文件是否修正确

./nginx -t

如果输出 is ok,则表示配置文件修改成功
重启 nginx 服务

./nginx -s reload

测试接口
学新通

至此,我们已经将我们的 ip 通过 nginx 代理转发到我们的二级域名上来了
但是这是 http 链接,因此前面会有不安全的提示
下面来配置 ssl 证书,让我们的链接变成安全的 https 链接

四、配置ssl证书

1.先去你的域名控制台申请ssl证书

学新通
审核通过后下载 nginx 证书
学新通

2.将证书文件夹中的pem和key文件上传到nginx.conf的目录下

直接上传到nginx.conf的目录下
学新通
在nginx.conf文件中添加一个新的server

server {
        listen       443 ssl;
        server_name  test.xxxxxxxx.com;

		# 你的域名的pem文件
        ssl_certificate      /test_ssl/test.xxxxxx.com_bundle.pem;
        # 你的域名的key文件
        ssl_certificate_key  /test_ssl/test.xxxxxx.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        # ssl_ciphers  HIGH:!aNULL:!MD5;
        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:8280;
        }
    }
学新通

检查配置文件是否正确
学新通
这表示 nginx 没有 http_ssl_module 模块

3.添加ssl模块

进入 nginx 安装路径

cd /usr/local/nginx-1.1.10

附加–with-http_ssl_module

./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

配置完成后,运行命令

make

这里不要进行make install,否则就是覆盖安装
然后备份原有已安装好的nginx

cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

然后将刚刚编译好的nginx覆盖掉原有的nginx(这个时候nginx要停止状态)

cp ./objs/nginx /usr/local/nginx/sbin/

检查是否配置ssl成功
学新通
进入sbin目录,重新运行nginx

./nginx

4.修改nginx.conf配置文件

添加以下server

server {
        listen       443 ssl;
        server_name  test.xxxxxx.com;

		# 你的域名的pem文件
        ssl_certificate      /test_ssl/test.xxxxx.com_bundle.pem;
        # 你的域名的key文件
        ssl_certificate_key  /test_ssl/test.xxxxxx.com.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        # ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
	
		ssl_prefer_server_ciphers  on;

        location / {
            proxy_pass       http://127.0.0.1:8280;
        }
    }
学新通

配置好后检查nginx.conf文件是否正确,如果正确,则重启nginx服务
注意:如果重启后还是访问不了相应的https接口的话,就关掉nginx,然后再重新启动

# nginx 停止命令
/usr/local/nginx/sbin/nginx -s stop
# nginx 启动命令
/usr/local/nginx/sbin/nginx

重启nginx后访问https链接
学新通
接口请求成功!!!

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgkifcj
系列文章
更多 icon
同类精品
更多 icon
继续加载