NGINX相关配置

NGINX相关配置

全局配置

NGINX配置信息

nginx 官方帮助文档:http://nginx.org/en/docs/

Nginx的配置文件的组成部分:
主配置文件:/conf/nginx.conf(/nginx/conf/nginx.conf)
子配置文件: include conf.d/*.conf

NGINX相关配置_第1张图片


#事件驱动相关的配置   同步
event {
worker_connections 1024; #一次允许1024个执行
 ...
} 

#http/https 协议相关配置段
http {
server{
location{}
}
 ...
}

#默认配置文件不包括下面两个块
#mail 协议相关配置段
mail {
 ...
}

#stream 服务器相关配置段
stream {负载均衡
 ...
}

修改启动进程数

lscpu |grep -i cpu  #查看cup核数
[root@localhost ~]vim /apps/nginx/conf/nginx.conf
#开启 两核
#user  nobody;
worker_processes  4;  #根据CPU核数修改
worker_processes auto;  #如果设置为auto  就是你真实的cpu数量

NGINX相关配置_第2张图片

关闭或修改版本

关闭版本
vim /apps/nginx/conf/nginx.conf
#修改配置文件  放在   http语句中
http {
    server_tokens  off; 
    }

nginx -s reload

NGINX相关配置_第3张图片

修改版本
#在安装包中
[root@localhost core] vim /opt/nginx-1.18.0/src/core/nginx.h
#define NGINX_VERSION      "9527"
#define NGINX_VER          "http/" NGINX_VERSION

[root@localhost core] vim /opt/nginx-1.18.0/src/http/ngx_http_header_filter_module.c 
static u_char ngx_http_server_string[] = "Server: beijing " CRLF;

[root@localhost nginx-1.18.0]./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module
[root@localhost nginx-1.18.0]make && make install

NGINX相关配置_第4张图片
NGINX相关配置_第5张图片

NGINX相关配置_第6张图片

NGINX相关配置_第7张图片

修改pid文件路径

mkdir /apps/nginx/run/ #创建目录
vim /apps/nginx/conf/nginx.conf #修改配置文件
pid   /apps/nginx/run/nginx.pid; #找到 pid的位置修改 
[root@localhost ~]#nginx -s reload 

CPU与work进程绑定

默认Nginx是不进行进程绑定的,但是绑定了以后可以可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能。

CPU序号:
CPU MASK: 00000001:0号CPU
          00000010:1号CPU
          ................
  		  10000000:7号CPU

ps axo pid,cmd,psr,ni|grep -v grep |grep nginx|sort -n  #这个命令可以查看每个进程占用的是那个CPU
[root@localhost ~]vim /apps/nginx/conf/nginx.conf
worker_cpu_affinity 00000001 00000010 00000100 00001000;
[root@localhost ~]nginx -s reload 

NGINX相关配置_第8张图片

进程的优先级

nice的优先级范围是:-20~19

[root@localhost ~] ps axo pid,cmd,psr,ni|grep nginx|sort -n #查看优先级
[root@localhost ~]vim /apps/nginx/conf/nginx.conf
worker_priority -20; #将优先级调为-20
[root@localhost ~]nginx -s reload 

NGINX相关配置_第9张图片
NGINX相关配置_第10张图片

调试work进程打开的文件个数

[root@localhost ~]vim /apps/nginx/conf/nginx.conf
worker_rlimit_nofile 65536; #一次允许65536访问
events {
    worker_connections  20000;  #最大连接数
}
[root@localhost security]#nginx -s reload

临时修改
[root@localhost ~]#ulimit -n 60000  #只修改当前窗口
永久修改
[root@localhost security]#vim /etc/security/limits.conf #将下面内容直接写在文件末尾
*                soft    core            unlimited
*                hard    core            unlimited
*                soft    nproc           1000000
*                hard    nproc           1000000
*                soft    nofile          1000000
*                hard    nofile          1000000
*                soft    memlock         32000
*                hard    memlock         32000
*                soft    msgqueue        8192000
*                hard    msgqueue        8192000

永久修改后需要重启reboot,才能生效,临时的不用重启

[root@localhost security]ulimit -a #可以查看

NGINX相关配置_第11张图片

NGINX相关配置_第12张图片

NGINX相关配置_第13张图片

服务前后台运行

一般都是后台运行,前台运行容器中会用到
[root@localhost ~]vim /apps/nginx/conf/nginx.conf
daemon off;  #关闭后台运行
root@localhost security]nginx -s reload

你可能感兴趣的:(nginx,运维)