nginxのWebページの最適化()
隠すnginxのコンフィギュレーションバージョン番号
-
本番環境では、我々は、非表示にする必要が
Nginx
漏洩セキュリティの脆弱性を回避するために、バージョン番号を -
チェック方法
- 使用
fiddler
中のツールWindows
クライアントビューNginx
バージョン番号 - では
CentOS
、システムの使用して"curl -I 网址”
表示するためのコマンドを
- 使用
- 隠された方法のnginxのバージョン番号
- コンフィギュレーション・ファイル方式を変更します。
- ソースコードの法律を変更します
コンフィギュレーション・ファイル方式を変更します。
Nginx
プロファイルserver_ tokens
オプションの値がに設定されていますoff
[root@www conf]# vi nginx.conf
.....
server_ tokens off; //关闭版本号
.....
[root@www conf]# nginx -t
- 使用してWebサイトを参照してください、サービスを再起動し
curl -I
、コマンド検出を
[root@www conf]# service nginx restart
[root@www conf]# curl -| http://192.168.9.209/
HTTP/1.1 200 OK
Server: nginx
- もし
php
設定ファイルを設定するfastcgi_param SERVER_ SOFTWARE
オプションを。 - 編集
php-fpm
設定ファイルを、fastcgi_param SERVER_ SOFTWARE
対応する値を変更するにはfastcgi_param SERVER_ SOFTWARE nginx
;
設定例
[root@localhost nginx]# curl -I http://192.168.144.133/ //使用命令查看版本号
HTTP/1.1 200 OK
Server: nginx/1.12.2 //显示版本号
Date: Thu, 14 Nov 2019 06:52:14 GMT
Content-Type: text/html
Content-Length: 634
Last-Modified: Thu, 14 Nov 2019 06:24:32 GMT
Connection: keep-alive
ETag: "5dccf320-27a"
Accept-Ranges: bytes
[root@localhost nginx]# vim conf/nginx.conf //进入编辑配置文件
...//省略部分内容...
http {
include mime.types;
default_type application/octet-stream;
server_tokens off; //添加条目关闭版本号
...//省略部分内容...
:wq
[root@localhost nginx]# systemctl restart nginx.service
[root@localhost nginx]# curl -I http://192.168.144.133
HTTP/1.1 200 OK
Server: nginx //版本号隐藏
Date: Thu, 14 Nov 2019 06:56:51 GMT
Content-Type: text/html
Content-Length: 634
Last-Modified: Thu, 14 Nov 2019 06:24:32 GMT
Connection: keep-alive
ETag: "5dccf320-27a"
Accept-Ranges: bytes
ソースコードの法律を変更します
Nginx
ソース・ファイルは、/usr/src/nginx-1.12.0/src/core/nginx.h
あなたが自由に設定することができ、バージョン情報が含まれています-
再コンパイル、インストール、隠されたバージョン情報
-
例:
#define NGINX_ VERSION“1.1.1" 修改版本号为1.1.1 #define NGINX VER "IIS/" 修改软件类型为IIS
- 使用してWebサイトを参照してください、サービスを再起動し
curl -I
、コマンド検出を
設定例
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf //编辑nginx配置文件
...//省略部分内容...
http {
include mime.types;
default_type application/octet-stream;
server_tokens on; //打开上面设置的隐藏版本号条目
...//省略部分内容...
:wq
[root@localhost ~]# cd /opt/nginx-1.12.2/src/core/ //到解压的源码包中更改版本号信息
[root@localhost core]# vim nginx.h
#define nginx_version 1012002
#define NGINX_VERSION "1.1.1" //更改版本号
#define NGINX_VER "nginx/" NGINX_VERSION
:wq
[root@localhost core]# cd /optnginx-1.12.2/
[root@localhost nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module //重新配置nginx
checking for OS
+ Linux 3.10.0-693.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
...//省略部分内容...
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
[root@localhost nginx-1.12.2]# make && make install //重新制作安装nginx
[root@localhost nginx-1.12.2]# systemctl restart nginx.service //重新启动nginx服务
[root@localhost nginx-1.12.2]# curl -I http://192.168.144.133 //查看版本号
HTTP/1.1 200 OK
Server: nginx/1.1.1 //版本号变更
Date: Thu, 14 Nov 2019 07:11:08 GMT
Content-Type: text/html
Content-Length: 634
Last-Modified: Thu, 14 Nov 2019 06:24:32 GMT
Connection: keep-alive
ETag: "5dccf320-27a"
Accept-Ranges: bytes
ユーザーとグループを変更するnginxの
Nginx
ランタイム・プロセスは、ファイルを読むためにWebサイトへのアクセス制御を実現するために、ユーザーとグループの支援を必要としますNginx
デフォルトのnobody
ユーザーアカウントとグループアカウント、一般的に修正する必要があります- 修正方法
- インストール時に指定したユーザーとグループをコンパイルします
- ユーザーとグループを指定するには、設定ファイルを変更します。
指定されたコンパイル、インストール
-
ユーザーアカウントとグループアカウントを作成し、など
nginx
- コンパイルしてインストールするとき
--user
に--group
指定したNginx
ユーザーアカウントとグループアカウントを実行しているサービス
コンフィギュレーション・ファイルの指定にメソッドを変更
- 以下のような新しいユーザーアカウント、
nginx
- マスター設定ファイルを変更する
user
オプションは、ユーザーアカウントを指定します - 再起動
nginx
を有効にするサービス構成 - 使用
ps aux
表示するコマンドをnginx
ユーザアカウントを実行した変更の効果を確認するために、プロセス情報を
[root@www conf]# vi nginx.conf
user nginx nginx;
[root@www conf]# service nginx restart
[root@www conf]# ps aux | grep nginx
root 130034 0.0 0.0 20220 620 ? Ss 19:41 0:00 nginx: master process
/usr/local/sbin/nginx
nginx 130035 0.0 0.0 20664 1512 ? S 19:41 0:00 nginx: worker process
nginxのウェブキャッシングの設定時間
- ときに
Nginx
ウェブページデータがクライアントに返され、キャッシュは、高速なアクセス速度を再三の要求を避けるために、同じ内容を返す直接将来の要求を容易にするための時間を提供することができます - 静的なページの設定と同じように、動的なページは、キャッシュ時間を設定していません
- あなたはできる
Windows
クライアントを使用しfiddler
たWebページのキャッシュ時間を表示します
設定方法
- あなたには、設定ファイルを変更することができる
http
部分、またはserver
セクション、またはlocation
セクションでは、特定のコンテンツの有効期限に追加します
例
- nginxの設定ファイルを変更、追加は場所のセクションでパラメータを満了します
location ~\.(gif|ipg|jepg|png|bmp|ico)$ {
root html;
expires 1d;
}
設定例
[root@localhost ~]# systemctl stop firewalld.service //关闭防火墙
[root@localhost ~]# setenforce 0 //关闭增强性安全功能
[root@localhost ~]# systemctl start nginx.service //启动nginx服务
[root@localhost ~]# netstat -ntap | grep 80 //查看服务端口是否开启
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 1684/nginx: master
[root@localhost ~]# mkdir abc
[root@localhost ~]# mount.cifs //192.168.100.10/lamp-c7 abc/ //将宿主机图片文件夹挂载到abc目录
Password for root@//192.168.100.10/lamp-c7:
[root@localhost ~]# cd abc/ //进入abc目录
[root@localhost abc]# ls
apr-1.6.2.tar.gz Discuz_X2.5_SC_UTF8.zip miao.jpg
apr-util-1.6.0.tar.gz error.png mysql-5.6.26.tar.gz
awstats-7.6.tar.gz httpd-2.4.29.tar.bz2 nginx-1.12.0.tar.gz
cronolog-1.6.2-14.el7.x86_64.rpm LAMP-php5.6.txt php-5.6.11.tar.bz2
[root@localhost abc]# cp miao.jpg /usr/local/nginx/html/ //将图片复制到nginx服务站点
[root@localhost abc]# cd /usr/local/nginx/html/ //进入站点目录
[root@localhost html]# ls
50x.html index.html miao.jpg
[root@localhost html]# vim index.html //编辑网页内容
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<img src="miao.jpg"/> //添加图片
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
:wq
[root@localhost nginx]# vim conf/nginx.conf //编辑配置
..//省略部分内容...
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
..//省略部分内容...
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~\.(gif|jepg|jpg|ico|bmp|png)$ { //编辑缓存条目
root html;
expires 1d;
}
}
..//省略部分内容...
:wq
[root@localhost nginx]# systemctl restart nginx.service //重启nginx服务
- クライアントでWebページを訪問し、キャッシュを表示するために、時間、パケットキャプチャツールを使用してオンになっています
nginxのは、接続タイムアウト達成の設定
- クライアント接続、リソースの無駄で長期占領を回避するために、企業のWebサイトでは、制御接続のアクセス時間を達成するために、適切な接続タイムアウトパラメータを設定することができます
- 接続パラメータを表示するために使用するフィドラーツール
説明するためのタイムアウトパラメータ
-
Keepalive_タイムアウト
- セットの接続がタイムアウトまま - 缶のみ、このパラメータを設定すると、デフォルトでは75秒で、現場の状況に応じて設定する、または閉じ、HTTPセグメント、サーバセグメント、セグメントまたは場所の設定にすることができます
-
クライアントheader_タイムアウト
- 指定したクライアントは、タイムアウトヘッダーを待つ要求を送信します
- クライアントのボディ _timeout
- 読み出し要求本体のタイムアウトを設定します
設定例
[root@localhost nginx-1.12.2]# cd /usr/local/nginx/conf/ //进入nginx配置文件目录
[root@localhost conf]# vim nginx.conf //编辑配置文件
...//省略部分内容...
http {
include mime.types;
default_type application/octet-stream;
server_tokens on;
#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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65 180; //添加客户端超时时间180秒
client_header_timeout 80; //设置客户端头部超时时间
client_body_timeout 80; //设置客户端主题内容超时时间
#gzip on;
server {
listen 80;
server_name localhost;
...//省略部分内容...
:wq
[root@localhost conf]# systemctl restart nginx.service //重启服务