本文以centos为例

nginx+nginxscript

源码安装nginx

安装必要环境

  1. 安装gcc环境
    sudo yum -y install gcc gcc-c++
  2. 安装 pcre,让nginx支持重写功能,代码如下:
    sudo yum -y install pcre pcre-devel
  3. 安装 zlib,zlib 库提供了很多压缩和解压缩的方式,nginx 使用 zlib 对 http 包内容进行 gzip 压缩,代码如下
    sudo yum -y install zlib zlib-devel
  4. 安装 openssl,安全套接字层密码库,用于通信加密,代码如下:
    sudo yum -y install openssl openssl-devel

解压Nginx安装包并进行安装

下载源码

#下载
wget http://nginx.org/download/nginx-1.16.1.tar.gz
#解压 nginx版本过新可能无法安装
tar -xzf nginx-1.16.1.tar.gz
cd nginx-1.16.1

编译安装

./configure --prefix=/usr/local/nginx  --prefix=后面为安装Nginx安装目录,我这里是的安装目录是/usr/local/nginx
1. make ##编译
2. make install ##安装

源码安装nginxscript

克隆源码

hg clone http://hg.nginx.org/njs

从nginx根目录编译并安装njs

./configure --prefix=/usr/local/nginx --add-dynamic-module=/usr/local/nginx/njs/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module ## njs源码目录
# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
make install ##重新安装nginx

将nginx配置为系统服务并开机自启

配置系统服务+开启自启

  1. 在/usr/lib/systemd/system目录下添加nginx.service,内容如下:

vim /usr/lib/systemd/system/nginx.service


[Unit]
Description=nginx web service
Documentation=http://nginx.org/en/docs/
After=network.target [Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/usr/local/nginx/sbin/nginx -s stop
PrivateTmp=true [Install]
WantedBy=default.target
  1. 添加完成后如果权限有问题需要进行权限设置
chmod 755 /usr/lib/systemd/system/nginx.service
  1. 使用命令
启动: systemctl start nginx
停止: systemctl stop nginx
重启: systemctl restart nginx
重新加载配置文件: systemctl reload nginx
查看nginx状态: systemctl status nginx
开机启动: systemctl enable nginx

enjoy yourself

使用njs实现请求头参数控制

由于这是加载的动态模块所以需要在根下面引入

# 加载njs模块
load_module modules/ngx_http_js_module.so;

在配置文件中(http下面)导入js文件

js_import dreamer from dreamer_header_check.js;

根conf示例


#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;
# 加载njs模块
load_module modules/ngx_http_js_module.so; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; # 引入js
js_import dreamer from dreamer_header_check.js; 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 65; #gzip on; include /opt/conf/nginx/conf/conf.d/*.conf;
}

在具体的配置文件中使用

server {
listen 80;
server_name admin.hnzmr.com;
location /admin {
alias /data/dreamer/admin/dist;
index index.html index.htm;
} location /prod-api/ { ## njs层代理
js_content dreamer.headers_filter;
} location @admin-backend { ## @真实的代理服务
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://adminapi.hnzmr.com;
}
}

js文件

// 获取header里面的参数

// 认证参数 token
const authorization ='authorization';
// APP ID(程序唯一标识)
const appId='appId';
// 设备类型 android ios wx h5 web
const os='os';
// 应用版本号
const version='version'; function headers_filter(r){
r.headersOut['Content-Type'] = "application/json; charset=utf-8";
// 定义要校验的数组
var arr = [appId,os,version];
// 循环数组判断有没有缺少参数
for(var i=0;i<arr.length;i++){
if(!r.headersIn[arr[i]]){
var json={
code:400,
msg:"请求头校验不通过,缺少'"+arr[i]+"'参数"
};
r . return ( 400 , JSON.stringify(json) ) ;
return;
}
}
// 传递下去
/* r.subrequest(r.uri).then((response) => {
r.headersOut["Content-Type"] = "application/json;charset=UTF-8";
r.return(response.status, JSON.stringify(response));
}); */
r.internalRedirect('@admin-backend');
} function dreamer_headers_filter(r){
r.headersOut['Content-Type'] = "application/json; charset=utf-8";
// 定义要校验的数组
var arr = [appId,os,version];
// 循环数组判断有没有缺少参数
for(var i=0;i<arr.length;i++){
if(!r.headersIn[arr[i]]){
var json={
code:400,
msg:"请求头校验不通过,缺少'"+arr[i]+"'参数"
};
r . return ( 400 , JSON.stringify(json) ) ;
return;
}
}
// 传递下去
/* r.subrequest(r.uri).then((response) => {
r.headersOut["Content-Type"] = "application/json;charset=UTF-8";
r.return(response.status, JSON.stringify(response));
}); */
r.internalRedirect('@dreamer-backend');
} export default {headers_filter,dreamer_headers_filter};

安装nginx并配置nginxscript(njs)实现请求头验证或者分流的更多相关文章

  1. 【转】linux 编译安装nginx,配置自启动脚本

    linux 编译安装nginx,配置自启动脚本 本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装ng ...

  2. linux 编译安装nginx,配置自启动脚本

    本文章来给各位同学介绍一篇关于linux 编译安装nginx,配置自启动脚本教程,希望有需要了解的朋友可一起来学习学习哦. 在公司的suse服务器装nginx,记录下安装过程: 参照这篇文章:Linu ...

  3. 【Nginx安装】CentOS7安装Nginx及配置

    [Nginx安装]CentOS7安装Nginx及配置 2018年03月05日 11:07:21 阅读数:7073 Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttp ...

  4. CentOS 下 安装 nginx 执行配置命令 ./configure 报错

    CentOS 下 安装 nginx 执行配置命令 ./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx 时提示以下错误: checkin ...

  5. 【Asp.net Core】在 Linux 子系统中安装 nginx 并配置反向代理

    上一篇鸟文中,老周已经介绍过在 Ubuntu 子系统中安装 dotnet-sdk 的方法,本文老周给大伙伴们说说安装 nginx 服务,并配置反向代理.同样,老周假设你从来没有用过 Linux,所以老 ...

  6. linux下安装nginx与配置

    linux系统为Centos 64位 第一步:从http://nginx.org/download/上下载相应的版本(或者wget http://nginx.org/download/nginx-1. ...

  7. CentOS7安装Nginx及配置

    Nginx是一款轻量级的网页服务器.反向代理服务器.相较于Apache.lighttpd具有占有内存少,稳定性高等优势.**它最常的用途是提供反向代理服务.** 安装   在Centos下,yum源不 ...

  8. mac 安装nginx,并配置nginx的运行环境

    1. 安装nginx // 查询有没有nginx brew search nginx //开始安装nignx brew install nginx 2. 检查nignx是否安装成功 nginx -V ...

  9. linux安装nginx并配置负载均衡

    linux上安装nginx比较简单: 前提是需要有gcc或者g++ 1.yum需要的依赖  yum -y install openssl openssl-devel 2.解压pcre库.zlib库   ...

  10. linux下安装nginx和配置

    1.系统:centos6.8 2.安装准备: 安装nginx前,我们首先要确保系统安装了g++.gcc.openssl-devel.pcre-devel和zlib-devel软件,可通过如图所示命令进 ...

随机推荐

  1. 微信支付v3接口的 官方 Java SDK

    啰嗦几句:微信支付v3版接口麻烦吗?在对接微信支付v3接口时,本来是一件很简单的事情,其实微信支付v3接口并不是很复杂,但是微信团队的管理很混乱,给我们开发者带来了巨大的麻烦. 微信支付v3版接口对接 ...

  2. ES6 学习笔记(三)原始值与引用值

    总结: 1.原始值,表示单一的数据,如10,"abc",true等. 1.1. ES的6种原始值: Undefined.Null.Boolean.Number.String.Sym ...

  3. @confirguration(proxyBeanMethods = false)的作用,如何选择Full模式和Lite模式

    @Configuration(proxyBeanMethods = false) //告诉SpringBoot这是一个配置类 == 配置文件 public class MyConfig { @Bean ...

  4. 第2-1-1章 FastDFS分布式文件服务背景及系统架构介绍

    目录 1 背景 1.1 为什么需要分布式文件服务 1.1.1 单机时代 1.1.2 独立文件服务器 1.1.3 分布式文件系统 1.2 什么是FastDFS 2 系统架构 2.1 Tracker集群 ...

  5. C. 连锁商店(状压dp)

    C. 连锁商店 time limit per test 1 second memory limit per test 512 megabytes input standard input output ...

  6. SpringBoot启动流程源码分析

    前言 SpringBoot项目的启动流程是很多面试官面试中高级Java程序员喜欢问的问题.这个问题的答案涉及到了SpringBoot工程中的源码,也许我们之前看过别的大牛写过的有关SpringBoot ...

  7. 关于软件物料清单(SBOM),你所需要了解的一切

    在此前的多篇文章中,我们已经详细地介绍了软件物料清单(SBOM)对于保障软件供应链安全的重要性以及一些注意事项.在本文中,我们将会更深入地介绍SBOM,包括最低要求元素.格式.使用场景以及如何对其进行 ...

  8. K3S 安装及配置

    K3S安装 curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_ ...

  9. RabbitMq发布确认

    RabbitMq发布确认 发布确认原理 生产者将信道设置成 confirm 模式,一旦信道进入 confirm 模式,所有在该信道上面发布的消息都将会被指派一个唯一的 ID(从 1 开始),一旦消息被 ...

  10. AArch32/AArch64系统级内存模型(三)

    1. 内存系统架构 1.1 系统级存储系统体系结构的形式   Armv8的a -profile体系结构包括一个虚拟内存系统体系结构(Virtual Memory System Architecture ...