ubuntu 24手动编译nginx源码及添加对ip地址过滤的支持

news/2024/7/8 3:25:26 标签: ubuntu, nginx, 服务器

本文参考了csdn这位博主的文章并修改整理:

https://blog.csdn.net/qq_32262243/article/details/133951973

还是腾讯云ubuntu 24系统,我这里并没有手动下载pcre等源码,直接用ubuntu自带的就可以了,也不需要手动编译openssl等。

一、软件准备

1.1 nginx源码

  这里使用最新的稳定版本1.26.1的源码,下载地址:

https://nginx.org/download/nginx-1.26.1.tar.gz

1.2 geoip2模块源码

Releases · leev/ngx_http_geoip2_module · GitHub

  这个源码是配合nginx源码在做静态编译的时候添加对IP地址过滤支持用的。

1.3 libmaxmindb库

https://github.com/maxmind/libmaxminddb/releases

1.4 IP地址库

https://www.maxmind.com/en/home

注册然后登录,下载country库和city库备用

二、环境准备

   ubuntu 24系统做以下准备

sudo apt-get install build-essential zlib1g-dev libssl-dev libpcre2-dev libmaxminddb-dev libmaxminddb0 mmdb-bin vim 

三、编译安装

  解压缩第一步下载的4个压缩包,解压缩命令是tar -zxvf 压缩包名称,我这里数据盘挂载路径是/data,所以在解压缩以后我把这四个压缩包解压结果移动到了我的/data里面备用,其中geoip2模块的路径是/data/software/nginx/ngx_http_geoip2_module-3.4/,以下先编译安装libmaxmindb

cd /data/libmaxminddb-1.10.0
./configure
make
make install
ldconfig

  然后开始编译nginx

cd /data/nginx-1.26.1
./configure --with-http_realip_module --with-http_sub_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_ssl_module  --with-http_v2_module --with-stream --add-module=/data/software/nginx/ngx_http_geoip2_module-3.4/
make
make install

 这就编译安装成功了,以下开始配置nginx

mkdir -p /data/nginx/logs
mkdir -p /usr/local/nginx/conf/v_host/
mkdir -p /usr/local/nginx/cache

  四、配置nginx

  修改/usr/local/nginx/conf/nginx.conf文件,增加对IP地址过滤的支持


#user  nobody;
worker_processes  auto;

error_log   /data/nginx/logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
pid        /var/run/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  /data/nginx/logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;   
    keepalive_timeout  65;
    gzip  on;

    # 配置国家IP库
    geoip2 /data/GeoLite2-Country/GeoLite2-Country.mmdb{
        auto_reload 20m;
        $geoip2_metadate_country_build metadata build_epoch;
        #$geoip2_data_country_code country iso_code;
        $geoip2_country_name country names en;
        $geoip2_data_country_code default=US country iso_code;
    }

    # 配置城市IP库
    geoip2 /data/GeoLite2-City/GeoLite2-City.mmdb {
        auto_reload 20m;
        $geoip2_data_city_name city names en;
        $geoip2_data_province_name subdivisions 0 names en;
        $geoip2_data_province_isocode subdivisions 0 iso_code;
        $geoip2_continent_code continent code;
    }

    #配置规则,默认不允许所有IP访问,只允许中国IP访问
    map $geoip2_data_country_code $allowed_country {
        default no;
        CN yes;
    }

    # 引用v_host中的指定conf配置文件
    include /usr/local/nginx/conf/v_host/*.conf;

}

  在/usr/local/nginx/conf/v_host里面新增自己站点的.conf文件,如xxx.com.conf文件并配置里面的内容:

server {
    listen   443   ssl;
    server_name  xxxx.com;

    ssl_certificate /data/nginx/xxxxxxx.crt;
    ssl_certificate_key /data/nginx/xxxxxxxxx.key;

    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH";
    ssl_ecdh_curve secp384r1;
    ssl_session_cache shared:SSL:10m;
    ssl_stapling on;
    ssl_stapling_verify on;

    client_max_body_size 50M;

    # 添加客户端的IP头
    add_header client-country $geoip2_data_country_code;    

    # 做判断,如果国家不是中国,就返回451状态码给客户端;
    if ($geoip2_data_country_code != CN ) {
        return 451;
    }

    # 做判断,如果匹配到默认不允许的规则,就返回452状态码给客户端;   
    if ($geoip2_data_country_code = no ) {
        return 452;
    }
    
    location / {
        if ($allowed_country = no) {
                return 403;
        }
       #你的正常配置
    }
}

 最后,写一个nginx的systemd服务文件,将nginx配置为systemd 服务程序

vim /etc/systemd/system/nginx.service

[Unit]
Description=NGINX HTTP and reverse proxy server
After=syslog.target network.target nss-lookup.target

[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/bin/kill -s HUP $MAINPID
ExecStop=/usr/bin/kill -s QUIT $MAINPID
# Hardening
#InaccessiblePaths=/etc/gnupg /etc/shadow /etc/ssh
#ProtectSystem=full
#ProtectKernelTunables=yes
#ProtectControlGroups=yes
#SystemCallFilter=~@clock @cpu-emulation @debug @keyring @module @mount @obsolete @raw-io
#MemoryDenyWriteExecute=yes
#RestrictAddressFamilies=AF_INET AF_INET6 AF_UNIX
#RestrictRealtime=yes

[Install]
WantedBy=multi-user.target

systemctl enable nginx 就可以了。


http://www.niftyadmin.cn/n/5536095.html

相关文章

如何利用AI撰写短文案获客?分享6大平台和3大步骤!

从去年开始,很多大厂都在裁员,原因就是因为AI的火爆,替代了很多机械式的劳动力。以前很多人可以通过机械式的工作来摸鱼,现在AI完成的效率比人工的要高很多倍。 国内好用的AI平台非常多,有时候也可以使用几个AI平台结合…

#### golang中【堆】的使用及底层 ####

声明,本文部分内容摘自: Go: 深入理解堆实现及应用-腾讯云开发者社区-腾讯云 数组实现堆 | WXue 堆(Heap)是实现优先队列的数据结构,Go提供了接口和方法来操作堆。 应用 package mainimport ("container/heap&q…

产品经理系列1—如何实现一个电商系统

具体笔记如下,主要按获客—找货—下单—售后四个部分进行模块拆解

安全和加密常识(6)Base64编码方式

文章目录 什么是 Base64编码原理编解码示例应用什么是 Base64 Base64 是一种用于将二进制数据编码为仅包含64种ASCII字符的文本格式的编码方法,注意,它不是加密算法。它设计的目的主要是使二进制数据能够通过只支持文本的传输层(如电子邮件)进行传输。Base64常用于在需要处…

ExtendSim在商业和服务行业中的仿真

仿真使企业能够做出明智的、数据驱动的预测,从而指导决策、产生积极成果并建立竞争优势。 精益分析 使用 ExtendSim 中的精益分析方法对欧洲的供应链网络进行建模,一家制造商实现了对最终客户的服务水平提高了 98%,而且现在可以在库存减少约 …

Python 3.x 下的 3D 游戏引擎

在 Python 3.x 中,有几个比较流行的用于开发 3D 游戏的引擎和库。虽然 Python 自身不是一个主流的游戏开发语言,但是可以通过这些库和引擎结合其它语言或者底层渲染引擎来实现复杂的游戏开发。 1、问题背景 在 Linux 系统中,尤其是 Debian 7…

艺活网DIY手工制作网站源码 工艺制作教程平台源码,带数据

帝国CMS仿《手艺活》DIY手工制作网源码,仿手艺活自适应手机版模板。 带数据库和图片资源,一共5个G大小,下载需耐心。 92开发 手艺活网DIY手工制作网站源码 创意手工艺品制作教程平台系统帝国h5自适应手机端 是一套展示各种 DIY 小物品精美又…

PyCharm 如何设置作者信息

1、点击pycharm右上角的齿轮,选择settings 2、选择editor 3、选择 Editor File and Code Templates 4、选择作者信息的文件类型,中间选择框选择Python Script 5、然后在右边的输入框中输入相关的信息 # -*- coding: utf-8 -*- """ Time …