Nginx 1.30.4 编译安装与参数调优指南

 更新时间:2026年09月03日 08:49:54   作者:飒飒  
通过本教程快速掌握Nginx编译安装与环境调优,从版本验证、环境变量配置到systemd服务管理全流程实操,并学会高并发场景下的内核参数优化,助你突破默认限制、提升服务器性能

通过本教程快速掌握Nginx编译安装与环境调优,从版本验证、环境变量配置到systemd服务管理全流程实操,并学会高并发场景下的内核参数优化,助你突破默认限制、提升服务器性能

NX_VER          "nginx/" NGINX_VERSION

#ifdef NGX_BUILD
#define NGINX_VER_BUILD    NGINX_VER " (" NGX_BUILD ")"
#else
#define NGINX_VER_BUILD    NGINX_VER
#endif

#define NGINX_VAR          "NGINX"
#define NGX_OLDPID_EXT     ".oldbin"

5、通过版本命令验证安装是否成功,确认编译参数、依赖版本无误。输出包含Nginx版本、GCC编译版本、OpenSSL版本及已开启的模块,即代表编译安装成功。

[root@localhost nginx-1.30.4]# cd 
[root@localhost ~]# /usr/local/nginx/sbin/nginx -V
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 ~]# echo 'export PATH=$PATH:/usr/local/nginx/sbin' >> /etc/profile
[root@localhost ~]# source /etc/profile
[root@localhost ~]# nginx -v
nginx version: nginx/1.30.4

7、创建systemd服务文件,实现Nginx开机自启、启停、重载管理,适配系统服务管理规范。

[root@localhost ~]# vim /usr/lib/systemd/system/nginx.service

[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 ~]# systemctl daemon-reload
[root@localhost ~]# systemctl start nginx
[root@localhost ~]# systemctl status nginx
● 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 ~]# nginx -t          # 检查配置语法
[root@localhost ~]# nginx -s reload   # 平滑重启
[root@localhost ~]# nginx -s stop     # 快速停止
[root@localhost ~]# nginx -s quit     # 优雅停止

8、备份默认配置文件后,对 nginx.conf 进行高并发调优,适配生产大流量场景,开启压缩、连接优化、状态监控等功能。

[root@localhost ~]# cd /usr/local/nginx/conf/
[root@localhost conf]# ll
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]# cp -a nginx.conf nginx.conf.bak

替换完整优化配置

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
# ------------------------------------
# 全局配置
# ------------------------------------
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 块
# ------------------------------
events {
    use epoll;
    worker_connections 65535;
    multi_accept on;
}

# --------------------------
# HTTP块通用调优
# --------------------------

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 ~]# vim /etc/sysctl.conf

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 ~]# sysctl -p
[root@localhost ~]# cat /proc/sys/fs/file-max 
6553500
[root@localhost ~]# cat /proc/sys/net/ipv4/tcp_tw_reuse 
1

10、调高Nginx用户文件句柄最大限制,解决高并发下文件打开数耗尽问题,修改后需重新登录会话生效。

[root@localhost ~]# vim /etc/security/limits.conf
nginx soft nofile 65535
nginx hard nofile 65535

[root@localhost ~]# ulimit -n
65535

到此这篇关于Nginx 1.30.4 编译安装与参数调优指南的文章就介绍到这了,更多相关Nginx 编译安装与参数调优内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • Nginx内网环境开启https双协议的实现

    Nginx内网环境开启https双协议的实现

    本文主要介绍了Nginx内网环境开启https双协议,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • 快速解决Request entity too large错误问题

    快速解决Request entity too large错误问题

    遇到413 Request Entity Too Large错误?本文详细解释这个HTTP状态码的含义,区分请求文件太大和实体太大的不同情况,并提供nginx的client_max_body_size调整、应用配置检查和服务器状态排查等实用解决方法,帮你快速修复上传大文件失败的问题
    2026-08-08
  • Nginx中CA签名证书的实现

    Nginx中CA签名证书的实现

    本文主要介绍了Nginx中CA签名证书的实现,手把手教你配置HTTPS、部署根证书,实现无警告访问,还能玩转mTLS双向认证,感兴趣的可以了解一下
    2026-08-08
  • keepalived+Nginx实现高可用集群配置

    keepalived+Nginx实现高可用集群配置

    本文详细介绍了如何使用Keepalived和Nginx搭建高可用集群,包括配置Keepalived为主从模式,利用虚拟IP实现故障切换,以及通过自定义Shell脚本监控Nginx状态,确保服务连续性,感兴趣的可以了解一下
    2026-05-05
  • 详解nginx 301跳转到带www域名方法

    详解nginx 301跳转到带www域名方法

    这篇文章主要介绍了详解nginx 301跳转到带www域名方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Nginx速查手册及常见问题

    Nginx速查手册及常见问题

    Nginx是一款轻量级的HTTP服务器,采用事件驱动的异步非阻塞处理方式框架,这让其具有极好的IO性能,时常用于服务端的反向代理和负载均衡,这篇文章主要介绍了Nginx速查手册及常见问题,,需要的朋友可以参考下
    2022-04-04
  • Nginx 服务器开启status页面检测服务状态的方法

    Nginx 服务器开启status页面检测服务状态的方法

    这篇文章主要介绍了Nginx 服务器开启status页面检测服务状态的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2024-01-01
  • Nginx动静分离的示例代码

    Nginx动静分离的示例代码

    动静分离是将网站静态资源与后台应用分开部署,本文主要介绍了Nginx动静分离的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2024-07-07
  • nginx中斜杠(‘/‘)的具体使用

    nginx中斜杠(‘/‘)的具体使用

    在Nginx配置的过程中,斜杠(/)经常使用到,它们不仅可以区分不同的路径,还有其他的作用,本文就详细的介绍了nginx中斜杠(‘/‘)的具体使用,感兴趣的可以了解一下,感兴趣的可以了解一下
    2023-10-10
  • nginx处理http请求实例详解

    nginx处理http请求实例详解

    这篇文章主要介绍了nginx处理http请求实例详解的相关资料,需要的朋友可以参考下
    2017-06-06

最新评论