Py学习  »  NGINX

centos 7.6 —— Nginx 服务优化隐藏版本号-网页压缩-更改进程数-设置连接超时-日志分割-网页压缩-设置网页缓存时间

weixin_42099301 • 3 年前 • 327 次点击  

centos 7.6 —— Nginx 服务优化隐藏版本号

一、Nginx隐藏版本号

手工编译Nginx步骤详情查看之前博客
手工编译Nginx

方法一:源码文件更改版本信息

(1)查看源码信息

[root@localhost opt]# vim /opt/nginx-1.12.2/src/core/nginx.h   


/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_


#define nginx_version      1012002
#define NGINX_VERSION      "1.12.2"
#define NGINX_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"


#endif /* _NGINX_H_INCLUDED_ */
~                                  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

(1)修改源码信息

[root@localhost opt]# vim /opt/nginx-1.12.2/src/core/nginx.h


/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) Nginx, Inc.
 */


#ifndef _NGINX_H_INCLUDED_
#define _NGINX_H_INCLUDED_


#define nginx_version      1012002
#define NGINX_VERSION      "1.1.1"
#define NGINX_VER          "IIS/" 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"


#endif /* _NGINX_H_INCLUDED_ */

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

**(3)验证修改后 版本信息




    
[root@localhost sbin]# iptables -F
[root@localhost sbin]# setenforce 0
[root@localhost sbin]# curl -I http://192.168.75.134
HTTP/1.1 200 OK
Server: IIS/1.1.1
Date: Mon, 10 Aug 2020 07:18:16 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:11:12 GMT
Connection: keep-alive
ETag: "5f30f310-264"
Accept-Ranges: bytes

[root@localhost sbin]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

方法一:修改主配置文件Nginx.conf

[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;
      server_tokens off;
[root@localhost sbin]# nginx   //端口被占用
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
cnginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
[root@localhost sbin]# sudo fuser -k 80/tcp  //
80/tcp:              12552 12553
[root@localhost sbin]# nginx  //重启Nginx


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

查看Nginx版本信息无验证成功

[root@localhost sbin]# nginx 
[root@localhost sbin]# curl -I http://192.168.75.134
HTTP/1.1 200 OK
Server: nginx   //无版本号
Date: Mon, 10 Aug 2020 07:22:33 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Mon, 10 Aug 2020 07:11:12 GMT
Connection: keep-alive
ETag: "5f30f310-264"
Accept-Ranges: bytes

[root@localhost sbin]# 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

二、修改用户与组

user nobody; 该为 user nginx nginx ;

[root@localhost sbin]# vim /usr/local/nginx.conf
[root@localhost sbin]# vim /usr/local/nginx/conf/nginx.conf


user  nginx nginx;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

三、配置网页缓存时间


[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf


        location ~ \.(gif|jpg|jepg|png|bmp|ico)$ {
            root   html;
            expires 1d;
        }
[root@localhost html]# sudo fuser -k 80/tcp
80/tcp:              12663 12664
[root@localhost html]# nginx 
[root@localhost html]# killall -s HUP nginx

[root@localhost html]# vim index.html 
<img src="xunya.jpg"/>
[root@localhost html]# ll
总用量 52
-rw-r--r--. 1 root root   537 810 15:11 50x.html
-rw-r--r--. 1 root root   636 810 15:46 index.html
-rw-r--r--. 1 root root 42529 810 15:42 xunya.jpg



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

虚拟机win10 验证

在这里插入图片描述

在这里插入图片描述

四、日志分割

[root@localhost html]# cd /opt
[root@localhost opt]# vim fenge.sh

#!/bin/bash
#Filename:fenge.sh
d=$#!/bin/bash
#Filename:fengge.sh
d=$(date -d "-1 day"  "+%Y%m%d")  #显示一天前的时间
logs_path="/var/log/nginx"
pid_path="/usr/local/nginx/logs/nginx.pid"
[ -d $logs_path ] || mkdir -p $logs_path
mv /usr/local/nginx/logs/access.log ${logs_path}/test.com-access.log-$d
kill -HUP $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf

[root@localhost opt]# chmod 777 fenge.sh 
[root@localhost opt]# ./fenge.sh 
[root@localhost opt]# cd /var/log/nginx/
[root@localhost nginx]# ll
总用量 4
-rw-r--r--. 1 root root 1905 810 15:49 test.com-access.log-20200809
[root@localhost nginx]# 



  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

四、网页压缩


[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf


    gzip  on;   #开启gzip压缩功能
    gzip_min_length 1K;   #压缩阀值
    gzip_buffers 4 16K;   # buffer大小为416K缓冲区大小
    gzip_http_version 1.1;  # 压缩版本
    gzip_comp_level 6;  #压缩比率,最小为1,处理速度快,传输速度慢,9最大压缩比,处理速度慢,传输速度快
   gzip_types text/css text/javascript application/javascript image/jpeg image/png image/gif text/plain application/x-javascript image/jpg application/xml application/x-httpd-php application/json;
     gzip_disable "MSIE [1-6]\.";  #配置禁用gzip条件,支持正则,表示IE6以下不启用gzip
     gzip_vary on; #选择支持vary header 可以让前端的缓存服务器缓存经过gzip压缩的页面


[root@localhost html]# systemctl restart nginx.service 

[root@localhost html]# ll
总用量 84
-rw-r--r--. 1 root root   537 810 15:11 50x.html
-rw-r--r--. 1 root root 29221 86 15:26 error.jpg
-rw-


    
r--r--. 1 root root   636 810 15:46 index.html
-rw-r--r--. 1 root root 42529 810 15:42 xunya.jpg
[root@localhost html]# vim index.html 


<img src="xunya.jpg"/>


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

虚拟机win10 验证

清理浏览器的缓存

在这里插入图片描述

五、设置连接超时

  • 200 OK 请求成功(其后是对GET和POST请求的应答文档)
  • 301 Moved Permanently 请求的永久页面跳转
  • 403 Forbidden 禁止访问该页面
  • 404 Not Found 服务器无法找到被请求的页面
  • 500 Internal Server Error 内部服务器错误
  • 502 Bad Gateway 无效网关
  • 503 Service Unavailable 当前服务不可用
  • 504 Gateway Timeout 网关请求超时

Nginx使用keepalive_timeout 来指定keepAlive 的超时时间(timeout)
指定每个TCP连接最多可以保持多长时间。Nginx 的默认值是65秒,有些浏览器最多只保持60秒,若将它设置为0。 就禁止了keepalive连接。


[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf  //在http中添加

 keepalive_timeout  100;
    client_header_timeout 80; # 等待客户端发送请求头的超时时间  超时会发送408错
误
    client_body_timeout 80;  #设置客户端发送请求体超时时间

[root@localhost html]# systemctl restart nginx.service 


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

六、更改进程数

在高并发环境中,需要启动更多的Nginx进程以保证快速响应,用以处理用户的请求,避免造成阻塞,使用 ps aux 命令查看Nginx运行进程的个数

其中 master process 是Nginx的主进程,开启了1个,worker process 是子进程,
也是开启了1个。
修改Nginx的配置文件中的work_processes 参数,一般设为CPU的个数或者核数,在高并发的情况下可设置为CPU的个数或者核数的2倍,可以先查看CPU的核数以确定参数。


[root@localhost html]# cat /proc/cpuinfo | grep -c "physical"
8

[root@localhost html]# ps aux | grep nginx   //cpu 核数

root      69719  0.0  0.0  20544   620 ?        Ss   17:40   0:00 nginx: master process /usr/local/nginx/sbin/nginx  //一个主进程中包含一个字进程
nobody    69720  0.0  0.0  23072  1392 ?        S    17:40   0:00 nginx: worker process
root      70197  0.0  0.0 112724   988 pts/0    S+   18:31   0:00 grep --color=auto nginx

[root@localhost html]# vim /usr/local/nginx/conf/nginx.conf


worker_processes  2; #修改为核数相同或者2倍

work_cpu_affinity 01 10; #设置每个进程由不同CPU处理

[root@localhost html]# systemctl restart nginx.service 

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/72407
 
327 次点击