「NGINX」- Web 服务器 | Engine X | 高性能的负载均衡器 | 反向代理

认识

官网:https://nginx.org/en/
文档:https://nginx.org/en/docs/
仓库:https://hg.nginx.org/nginx/

Nginx 是一个高性能的 HTTP 和反向代理服务器,它以事件驱动和异步非阻塞的方式运行,能够提供高并发的处理能力。Nginx 最初由俄罗斯的 Igor Sysoev 开发,并于 2004 年首次公开发布。它被设计为一个轻量级、高性能的服务器,能够处理大量的并发连接,并且具有较低的内存占用。

组成

Architecture and scalability

One master and several worker processes; worker processes run under an unprivileged user;

Flexible configuration;

Reconfiguration and upgrade of an executable without interruption of the client servicing;
Support for kqueue (FreeBSD 4.1+), epoll (Linux 2.6+), /dev/poll (Solaris 7 11/99+), event ports (Solaris 10), select, and poll;

The support of the various kqueue features including EV_CLEAR, EV_DISABLE (to temporarily disable events), NOTE_LOWAT, EV_EOF, number of available data, error codes;

The support of various epoll features including EPOLLRDHUP (Linux 2.6.17+, glibc 2.8+) and EPOLLEXCLUSIVE (Linux 4.5+, glibc 2.24+);

sendfile (FreeBSD 3.1+, Linux 2.2+, macOS 10.5+), sendfile64 (Linux 2.4.21+), and sendfilev (Solaris 8 7/01+) support;

File AIO (FreeBSD 4.3+, Linux 2.6.22+);

DIRECTIO (FreeBSD 4.4+, Linux 2.4+, Solaris 2.6+, macOS);

Accept-filters (FreeBSD 4.1+, NetBSD 5.0+) and TCP_DEFER_ACCEPT (Linux 2.4+) support;

10,000 inactive HTTP keep-alive connections take about 2.5M memory;

Data copy operations are kept to a minimum.

构建

安装 | CentOS、Ubuntu、Debian、……

容器镜像

[I] SRC: docker.io/library/nginx:1.25
[I] DST: ccr.ccs.tencentyun.com/d3rm-3rd/docker.io_library_nginx:1.25

Nginx 1.8 on CentOS 7.6

按照道理将应该不会有什么问题,但是在阿里云的 CentOS 系统里找不到这个包;

可以直接从官方的源中安装:

# http://nginx.org/packages/centos/7/x86_64/RPMS/
rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.1-1.el7.ngx.x86_64.rpm

on RHEL/CentOS

Nginx/RHEL/CentOS

# 配置源
cat << 'EOF' > /etc/yum.repos.d/cstm-nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
EOF

# 启用 mainline 版本(此时需要安装 yum-utils 包)
# yum install -y yum-utils
# yum-config-manager --enable nginx-mainline

# 更新缓存
yum makecache

# 安装
yum install -y nginx.x86_64

应用

强制使用 HTTPS 访问

How to force or redirect to SSL in nginx?
https://serverfault.com/questions/250476/how-to-force-or-redirect-to-ssl-in-nginx

返回特定字符串

location @ratelimit_response {
    default_type application/json;
    add_header Content-Type application/json;
    return 429 '{"code": 429,"message": "系统太火爆了,请稍后再试试啦!","data": {"timestamp": $msec}}';
}

改进

Nginx UI | Yet another Nginx Web UI | https://nginxui.com/
https://github.com/0xJacky/nginx-ui

参考

访问 Nginx 主页