koa入门

188 阅读1分钟

新建文件夹 learn-koa

初始化 npm: npm init
下载 koa:npm install koa

新建文件 app.js

服务器跑起来

// app.js

const port = 8008;
const Koa = require('koa')
const app = new Koa();

// logger

app.use(async (ctx, next) => {
  await next();
  const rt = ctx.response.get('X-Response-Time');
  console.log(`${ctx.method} ${ctx.url} - ${rt}`);
});

// x-response-time

app.use(async (ctx, next) => {
  const start = Date.now();
  await next();
  const ms = Date.now() - start;
  ctx.set('X-Response-Time', `${ms}ms`);
});

// response

app.use(async ctx => {
  ctx.body = 'Hello World';
});

app.listen(port, () => {
  console.log(`Server started on port ${port}`);
});

后台运行代码

forever start -o app.log -e err.log app.js

查看node进程

ps -aux |grep node

用semanage管理SELinux安全策略

sudo semanage port --add --type http_port_t --proto tcp 8008

nginx的配置反向代理:

/etc/nginx/目录(主配置文件)与/etc/nginx/conf.d目录下

sudo vi /etc/nginx/nginx.conf

server {
    listen       80;
    listen       [::]:80;
    server_name  127.0.0.1 localhost;
    root         /usr/share/nginx/html;

    # Load configuration files for the default server block.
    location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Nginx-Proxy true;
            proxy_set_header Connection "";

            proxy_http_version 1.1;
            proxy_set_header Host $host;
            proxy_pass http://localhost:8008;

    }

    error_page 404 /404.html;
    location = /404.html {
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    }
}

nginx 平缓重启

sudo nginx -s reload

快速检测nginx

sh nginx_check.sh

#!/bin/bash

Set the path to the Nginx binary

NGINX_BIN="/usr/sbin/nginx"

Set the path to the Nginx config file

NGINX_CONF="/etc/nginx/nginx.conf"

Set the Nginx listen port (default is 80)

NGINX_PORT=80

Set the firewall rule name (for example, "nginx-http")

FW_RULE_NAME="nginx-http"

Check if Nginx is running

if ps aux | grep -v grep | grep $NGINX_BIN > /dev/null; then echo "Nginx is running."

# Check the Nginx configuration syntax
CONFIG_TEST=$($NGINX_BIN -t -c $NGINX_CONF 2>&1)

if [[ "$CONFIG_TEST" == *"syntax is ok"* ]]; then
    echo "Configuration syntax is OK."
else
    echo "Error in configuration file:"
    echo "$CONFIG_TEST"
fi

else echo "Nginx is not running. Starting it now..."

# Start Nginx
$NGINX_BIN -c $NGINX_CONF

echo "Nginx started."

fi

Check if the firewall rule exists

if sudo firewall-cmd --zone=public --list-ports|grep '80/tcp'; then echo "Firewall rule for Nginx is enabled." else echo "Firewall rule for Nginx is not enabled. Enabling it now..."

# Add a new firewall rule for Nginx
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
sudo firewall-cmd --reload


echo "Firewall rule added."

fi

Check if SELinux is enforcing

if sestatus | grep "SELinux status:" | grep -q "enabled"; then echo "SELinux is enforcing."

# Check if the SELinux policy allows Nginx to use the specified port
if sudo semanage port -l | grep -w http_port_t | grep -w $NGINX_PORT > /dev/null; then
    echo "SELinux policy allows Nginx to use port $NGINX_PORT."
else
    echo "SELinux policy does not allow Nginx to use port $NGINX_PORT. Adding SELinux rule..."
    
    # Add a new SELinux rule to allow Nginx to use the specified port
    sudo semanage port -a -t http_port_t -p tcp $NGINX_PORT
    
    echo "SELinux rule added."
fi

else echo "SELinux is not enforcing." fi

Test homepage access

HOMEPAGE_URL="http://localhost:$NGINX_PORT/"

HOMEPAGE_RESPONSE=(curlconnecttimeout10maxtime10writeout(curl --connect-timeout 10 --max-time 10 --write-out %{http_code} --silent --output /dev/null HOMEPAGE_URL)

if [[ "HOMEPAGERESPONSE"eq200]];thenecho"Homepageisaccessiblefromthismachine."elseecho"Homepageisnotaccessiblefromthismachine.Responsecode:HOMEPAGE_RESPONSE" -eq 200 ]]; then echo "Homepage is accessible from this machine." else echo "Homepage is not accessible from this machine. Response code: HOMEPAGE_RESPONSE" fi