当前位置: 代码网 > 服务器>服务器>Linux > Nginx 1.30.4 编译安装与参数调优指南

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

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

通过本教程快速掌握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 编译安装与参数调优内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com