nginxのサービスでは、ユーザーとグループの設定ページのキャッシュと時間、スプリットログを変更、非表示のバージョンを最適化し、接続タイムアウトを設定します
序文
企業情報アプリケーション環境、安全およびサーバ構成パラメータの応答速度で最高のユーザーエクスペリエンスを実現するために、実際の状況に基づいて適切にする必要があります。
nginxのデフォルトのインストールパラメータは、このようなWebキャッシュとして最も基本的なサービスは、また、時間を調整する必要があり、最大の役割のサーバーを再生するために、対応するパラメータをページ圧縮を接続時間を提供することができます。
隠されたバージョン番号
なぜ我々は、バージョン番号を非表示にしますか?
私たちは、の感覚を持っている必要があります。ソフトウェアやサービスのために、その安全性は必ずしも完璧にすることはできません、バグや脆弱性の多様性があり、多かれ少なかれ常にある、これはなるだろうか、人の悪意一方向に、すべての隠されたバージョン番号は、ある程度のソフトウェアやサービスのセキュリティを強化することができます。
そして、nginxの皮のバージョン番号をどのようにそれを設定するには?ここでは、この説明を設定します。
すべての操作は上のnginxの良いスタートの基本的な操作マニュアルのコンパイルの下に実行されます。
1.使用のnginxのバージョン番号を表示するには、curlコマンド
[root@localhost ~]# which curl
/usr/bin/curl
[root@localhost ~]# curl -I http://192.168.68.145/
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 25 Dec 2019 10:31:46 GMT
Content-Type: text/html
Content-Length: 631
Last-Modified: Wed, 25 Dec 2019 10:04:25 GMT
Connection: keep-alive
ETag: "5e033429-277"
Accept-Ranges: bytes
隠されたバージョン番号を設定し、再度検出し(第一の方法)を開始
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -n '16,20p' /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens off;
[root@localhost ~]# service nginx stop
[root@localhost ~]# service nginx start
[root@localhost ~]# curl -I http://192.168.68.145/
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 25 Dec 2019 11:45:58 GMT
Content-Type: text/html
Content-Length: 631
Last-Modified: Wed, 25 Dec 2019 10:04:25 GMT
Connection: keep-alive
ETag: "5e033429-277"
Accept-Ranges: bytes
3.ソース文書偽造のバージョン番号
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
[root@localhost ~]# sed -n '16,20p' /usr/local/nginx/conf/nginx.conf
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
[root@localhost ~]# cd /opt/nginx-1.12.2/src/
[root@localhost src]# ls
core event http mail misc os stream
[root@localhost src]# cd core/
[root@localhost core]# ls
nginx.c ngx_cycle.h ngx_output_chain.c ngx_rwlock.c
nginx.h ngx_file.c ngx_palloc.c ngx_rwlock.h
ngx_array.c ngx_file.h ngx_palloc.h ngx_sha1.c
ngx_array.h ngx_hash.c ngx_parse.c ngx_sha1.h
ngx_buf.c ngx_hash.h ngx_parse.h ngx_shmtx.c
ngx_buf.h ngx_inet.c ngx_parse_time.c ngx_shmtx.h
ngx_conf_file.c ngx_inet.h ngx_parse_time.h ngx_slab.c
ngx_conf_file.h ngx_list.c ngx_proxy_protocol.c ngx_slab.h
ngx_config.h ngx_list.h ngx_proxy_protocol.h ngx_spinlock.c
ngx_connection.c ngx_log.c ngx_queue.c ngx_string.c
ngx_connection.h ngx_log.h ngx_queue.h ngx_string.h
ngx_core.h ngx_md5.c ngx_radix_tree.c ngx_syslog.c
ngx_cpuinfo.c ngx_md5.h ngx_radix_tree.h ngx_syslog.h
ngx_crc32.c ngx_module.c ngx_rbtree.c ngx_thread_pool.c
ngx_crc32.h ngx_module.h ngx_rbtree.h ngx_thread_pool.h
ngx_crc.h ngx_murmurhash.c ngx_regex.c ngx_times.c
ngx_crypt.c ngx_murmurhash.h ngx_regex.h ngx_times.h
ngx_crypt.h ngx_open_file_cache.c ngx_resolver.c
ngx_cycle.c ngx_open_file_cache.h ngx_resolver.h
[root@localhost core]# vim nginx.h
[root@localhost core]# sed -i '15p' nginx.h
[root@localhost core]# vim nginx.h
[root@localhost core]# sed -n '15p' nginx.h
[root@localhost core]# vim nginx.h
[root@localhost core]# sed -n '13p' nginx.h
#define NGINX_VERSION "1.12.2"
注意!上記1.12.2は、他のバージョンで置き換えられ、しかし必要手動で再コンパイルおよびインストールするので、この方法は、第1の時間セットに適していることができます
第二に、ユーザーとグループを変更します
nginx运行时进程需要有用户和组的支持,用以实现对网站文件读取时进行访问控制。主进程由root创建,子进程由指定的用户与组创建。nginx默认使用nobody用户账号与组账号,一般也需要修改。
私たちは手動でコンパイルし、ユーザーとグループを設定することですnginxのインストール時にそのため、一般的には、別の方法は、ユーザーとグループを指定するには、設定ファイルを変更することです。
[root@localhost src]# sed -n '2p' /usr/local/nginx/conf/nginx.conf
#user nobody;
ユーザーnginxのnginxの変更、コメントを外し流用されます。
サービスを再起動します。
第三に、設定ページのキャッシュ時間
目的:サーバーの負担、最適化された接続要求を緩和するために。
[root@promote html]# vim /usr/local/nginx/conf/nginx.conf
[root@promote html]# sed -n '57,59p' /usr/local/nginx/conf/nginx.conf
location ~*\.(jpg|gif|swf)$ {
root html;
expires 1d;
[root@promote html]# service nginx stop
[root@promote html]# service nginx start
[root@promote html]# ls
0.jpg 50x.html error.png index.html
[root@promote html]# sed -n '14,16p' index.html
<h1>Welcome to nginx!</h1>
<img src="0.jpg"/>
<p>If you see this page, the nginx web server is successfully installed and
次のようにテストの結果であることを確認します
第四に、分割されたログ
スクリプトを書いた後、スクリプトを実行するために、定期的なタスクを実行するためのcrontabを使用することができます
[root@promote ~]# vim /opt/fenge.sh
[root@promote ~]# cat /opt/fenge.sh
#!/bin/bash
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 -USR1 $(cat $pid_path)
find $logs_path -mtime +30 | xargs rm -rf
[root@promote ~]# chmod +x /opt/fenge.sh
[root@promote ~]# ls /opt/fenge.sh
/opt/fenge.sh
[root@promote ~]# cd /opt/
[root@promote opt]# ./fenge.sh
[root@promote opt]# cd /var/log/nginx/
[root@promote nginx]# ls
test.com-access.log-20191224
第五に、接続タイムアウトを設定します
接続タイムアウトとは何ですか?
サーバは、接続処理要求を受信していない確立したとき、それは自動的に終了タイムアウト時間を指定することができます
接続タイムアウトの役割
(1) 将无用的连接设置为尽快超时,可以保护服务器的系统资源(CPU、内存、磁盘)
(2) 当连接很多时,及时断掉那些建立好的但又长时间不做事的连接,以减少其占用的服务器资源
(3) 如果**,会不断地和服务器建立连接,因此设置连接超时以防止大量消耗服务器的资源
(4) 如果用户请求了动态服务,则 Nginx 就会建立连接,请求 FastCGI 服务以及后端 MySQL 服务,设置连接超时,使得在用户容忍的时间内返回数据
设置连接超时
(1) keepalive_timeout :该参数用于设置客户端连接保持会话的超时时间,超过这个时间服务器会关闭该连接
(2) client_header_timeout :该参数用于设置读取客户端请求头数据的超时时间,如果超时客户端还没有发送完整的 header 数据,服务器将返回 "Request time out (408)" 错误
(3) client_body_timeout :该参数用于设置读取客户端请求主体数据的超时时间,如果超时客户端还没有发送完整的主体数据,服务器将返回 "Request time out (408)" 错误
(4) send_timeout :用于指定响应客户端的超时时间,如果超过这个时间,客户端没有任何活动,Nginx 将会关闭连接
(5) tcp_nodelay :默认情况下当数据发送时,内核并不会马上发送,可能会等待更多的字节组成一个数据包,这样可以提高 I/O 性能,但是,在每次只发送很少字节的业务场景中,使用 tcp_nodelay 功能,等待时间会比较长
基本配置实例:可以使用抓包工具测试
[root@promote conf]# vim nginx.conf
[root@promote conf]# sed -n '32,34p' nginx.conf
keepalive_timeout 65 180;
client_header_timeout 80;
client_body_timeout 80;
[root@promote conf]# service nginx stop
[root@promote conf]# service nginx start
总结
この記事は、nginxの設定パラメータを最適化、安全性、ユーザー、キャッシュ、ログ、およびこれらの5つの領域を接続するために最適化されています。