Nginx 是一款轻量级、高性能、高并发的开源Web服务器、反向代理与负载均衡软件,凭借低资源占用、高稳定性的核心优势,成为当下互联网行业主流的服务部署组件,广泛应用于各类网站及业务系统的Web服务、反向代理、负载均衡场景。其基于宽松的类BSD开源协议发布,可免费商用。本文将完整展示 Nginx 1.30.4 源码编译安装、环境配置、systemd服务管理、内核及配置高性能调优的全套流程,适配CentOS/RHEL 10系列系统,可直接落地应用于企业生产环境。
1、源码编译需要gcc编译工具、正则、加密、压缩等依赖库,先执行依赖安装命令,确保编译环境完整。
[root@localhost ~] [root@localhost ~] Last metadata expiration check: 0:25:59 ago on Wed 02 Sep 2026 10:03:21 AM CST. Package gcc-14.4.1-3.el10.x86_64 is already installed. Package gcc-c++-14.4.1-3.el10.x86_64 is already installed. Package make-1:4.4.1-9.el10.x86_64 is already installed. Package zlib-ng-compat-2.2.3-3.el10.x86_64 is already installed. Package zlib-ng-compat-devel-2.2.3-3.el10.x86_64 is already installed. Package pcre2-10.44-1.el10.3.x86_64 is already installed. Package pcre2-devel-10.44-1.el10.3.x86_64 is already installed. Package openssl-1:3.5.8-1.el10.x86_64 is already installed. Package openssl-devel-1:3.5.8-1.el10.x86_64 is already installed. Package libtool-2.4.7-14.el10.x86_64 is already installed. Dependencies resolved. Nothing to do . Complete!
2、从Nginx官方镜像下载稳定版1.30.4源码包,解压后进入源码目录,为编译做准备。
[root@localhost ~] [root@localhost ~] [root@localhost nginx-1.30.4] total 916K drwxr-xr-x 6 502 wheel 4.0K Sep 2 10:28 auto -rw-r--r-- 1 502 wheel 338K Jul 16 01:24 CHANGES -rw-r--r-- 1 502 wheel 517K Jul 16 01:24 CHANGES.ru -rw-r--r-- 1 502 wheel 5.1K Jul 16 01:20 CODE_OF_CONDUCT.md drwxr-xr-x 2 502 wheel 168 Sep 2 10:28 conf -rwxr-xr-x 1 502 wheel 2.6K Jul 16 01:20 configure drwxr-xr-x 4 502 wheel 72 Sep 2 10:28 contrib -rw-r--r-- 1 502 wheel 4.9K Jul 16 01:20 CONTRIBUTING.md drwxr-xr-x 2 502 wheel 40 Sep 2 10:28 html -rw-r--r-- 1 502 wheel 1.3K Jul 16 01:20 LICENSE drwxr-xr-x 2 502 wheel 21 Sep 2 10:28 man -rw-r--r-- 1 502 wheel 15K Jul 16 01:20 README.md -rw-r--r-- 1 502 wheel 4.8K Jul 16 01:20 SECURITY.md drwxr-xr-x 9 502 wheel 91 Jul 16 01:20 src -rw-r--r-- 1 502 wheel 1.7K Jul 16 01:20 SUPPORT.md
3、为保证服务安全,创建不可登录系统用户运行Nginx,同时创建临时文件目录、日志存储目录,规范文件管理。
[root@localhost ~] [root@localhost ~] [root@localhost ~]
4、自定义Nginx安装路径、日志路径、进程文件路径,同时开启SSL、状态监控、反向代理、流媒体等核心常用模块,适配绝大多数生产场景。
[root@localhost nginx-1.30.4] [root@localhost nginx-1.30.4] --user=nginx \ --group=nginx \ --prefix=/usr/local/nginx \ --sbin-path=/usr/local/nginx/sbin/nginx \ --conf-path=/usr/local/nginx/conf/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --http-client-body-temp-path=/var/tmp/nginx/client_body \ --http-proxy-temp-path=/var/tmp/nginx/proxy \ --http-fastcgi-temp-path=/var/tmp/nginx/fastcgi \ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi \ --http-scgi-temp-path=/var/tmp/nginx/scgi \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_realip_module \ --with-http_sub_module \ --with-http_addition_module \ --with-http_random_index_module \ --with-http_secure_link_module \ --with-http_gunzip_module \ --with-http_gzip_static_module \ --with-stream \ --with-stream_ssl_module [root@localhost nginx-1.30.4] [root@localhost nginx-1.30.4]
默认Nginx会暴露版本号,存在一定安全风险,可通过修改源码文件隐藏版本信息(本文仅展示配置,按需修改)。
5、通过版本命令验证安装是否成功,确认编译参数、依赖版本无误。输出包含Nginx版本、GCC编译版本、OpenSSL版本及已开启的模块,即代表编译安装成功。
[root@localhost nginx-1.30.4] [root@localhost ~] nginx version: nginx/1.30.4 built by gcc 14.4.1 20260724 (Red Hat 14.4.1-3) (GCC) built with OpenSSL 3.5.8 25 Aug 2026 TLS SNI support enabled
6、为方便全局调用nginx命令,将程序路径加入系统环境变量。
[root@localhost ~] [root@localhost ~] [root@localhost ~] nginx version: nginx/1.30.4
7、创建systemd服务文件,实现Nginx开机自启、启停、重载管理,适配系统服务管理规范。
[root@localhost ~] [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx ExecReload=/usr/local/nginx/sbin/nginx -s reload ExecStop=/usr/local/nginx/sbin/nginx -s stop [Install] WantedBy=multi-user.target [root@localhost ~] [root@localhost ~] [root@localhost ~] ● nginx.service - nginx Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; preset: disabled) Active: active (running) since Wed 2026-09-02 10:59:58 CST; 7s ago Invocation: c7d50f2edde24e01b45bd3a7e33d8f8c Process: 14300 ExecStart=/usr/local/nginx/sbin/nginx (code=exited, status=0/SUCCESS) Main PID: 14301 (nginx) Tasks: 2 (limit : 10320) Memory: 1.9M (peak: 2.3M) CPU: 8ms CGroup: /system.slice/nginx.service ├─14301 "nginx: master process /usr/local/nginx/sbin/nginx" └─14302 "nginx: worker process" Sep 02 10:59:58 localhost systemd[1]: Starting nginx.service - nginx... Sep 02 10:59:58 localhost systemd[1]: Started nginx.service - nginx.
重载服务配置并启动
[root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~]
8、备份默认配置文件后,对 nginx.conf 进行高并发调优,适配生产大流量场景,开启压缩、连接优化、状态监控等功能。
[root@localhost ~] [root@localhost conf] total 68 -rw-r--r-- 1 root root 1077 Sep 2 10:33 fastcgi.conf -rw-r--r-- 1 root root 1077 Sep 2 10:34 fastcgi.conf.default -rw-r--r-- 1 root root 1007 Sep 2 10:33 fastcgi_params -rw-r--r-- 1 root root 1007 Sep 2 10:34 fastcgi_params.default -rw-r--r-- 1 root root 2837 Sep 2 10:34 koi-utf -rw-r--r-- 1 root root 2223 Sep 2 10:34 koi-win -rw-r--r-- 1 root root 5349 Sep 2 10:33 mime.types -rw-r--r-- 1 root root 5349 Sep 2 10:34 mime.types.default -rw-r--r-- 1 root root 2630 Sep 2 10:33 nginx.conf -rw-r--r-- 1 root root 2630 Sep 2 10:34 nginx.conf.default -rw-r--r-- 1 root root 636 Sep 2 10:33 scgi_params -rw-r--r-- 1 root root 636 Sep 2 10:34 scgi_params.default -rw-r--r-- 1 root root 664 Sep 2 10:33 uwsgi_params -rw-r--r-- 1 root root 664 Sep 2 10:34 uwsgi_params.default -rw-r--r-- 1 root root 3611 Sep 2 10:34 win-utf [root@localhost conf] [root@localhost conf]
替换完整优化配置
[root@localhost ~] user nginx nginx; worker_processes auto; worker_cpu_affinity auto; worker_rlimit_nofile 65535; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { use epoll; worker_connections 65535; multi_accept on; } 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 1000; client_max_body_size 100M; client_body_buffer_size 128k; client_header_buffer_size 4k; large_client_header_buffers 4 32k; client_header_timeout 10; client_body_timeout 10; send_timeout 10; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; gzip_vary on; include vhost/*.conf; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; allow 0.0.0.0; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location /nginx-status{ stub_status on; allow 127.0.0.1; deny all; } } }
9、高并发场景依赖系统内核参数优化,调整TCP连接、文件句柄等核心参数,突破系统默认限制。
[root@localhost ~] net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.ip_local_port_range = 1024 65535 net.ipv4.tcp_max_syn_backlog = 8192 net.ipv4.tcp_max_tw_buckets = 5000 fs.file-max = 6553500 [root@localhost ~] [root@localhost ~] 6553500 [root@localhost ~] 1
10、调高Nginx用户文件句柄最大限制,解决高并发下文件打开数耗尽问题,修改后需重新登录会话生效。
[root@localhost ~] nginx soft nofile 65535 nginx hard nofile 65535 [root@localhost ~] 65535