標籤:軟體 虛擬機器 include 資訊 版本號碼
實驗環境:
虛擬機器
CentOS 6.8
Nginx基本安全最佳化
1.調整參數隱藏Nginx軟體版本號碼資訊
http
{
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens off; 為關閉狀態,不顯示具體版本號碼
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
2.更改源碼隱藏Nginx軟體名稱及版本號碼
先修改以下幾個檔案:
/home/skyboy/tools/nginx-1.10.2/src/core/nginx.h
/home/skyboy/tools/nginx-1.10.2/src/http/ngx_http_header_filter_module.c
/home/skyboy/tools/nginx-1.10.2/src/http/ngx_http_special_response.c
修改後編譯軟體,使其生效。
3.更改Nginx服務的預設使用者
[[email protected] conf]# grep ‘#user‘ nginx.conf.default
#user nobody;
(1)為Nginx服務建立新使用者
[[email protected] conf]# useradd nginx -s /sbin/nologin -M
useradd:使用者“nginx”已存在
(2)使用剛剛建立的nginx使用者
第一種方法:直接更改設定檔參數
User nginx nginx;
第二種方法:在編譯軟體時指定使用者名稱和使用者組
(3)檢查更改使用者的效果
[[email protected] conf]# ps -ef|grep nginx|grep -v grep
root 1574 1 0 23:07 ? 00:00:00 nginx: master process /application/nginx/sbin/nginx -c /application/nginx/conf/nginx.conf
nginx 1674 1673 0 23:13 ? 00:00:00 php-fpm: pool www
nginx 1675 1673 0 23:13 ? 00:00:00 php-fpm: pool www
nginx 1832 1574 0 23:36 ? 00:00:00 nginx: worker process
根據參數最佳化Nginx服務效能
最佳化Nginx服務的worker進程個數
建議,根據cpu的個數來配置進程個數。
Worker_processes 1; #預設是1個進程數
調整Nginx單個進程允許的用戶端最大串連數
events {
worker_connections 1024; #預設是1024
}
Worker_connections的值要根據具體伺服器效能和程式的記憶體使用量量來指定。
#vi nginx
user nginx nginx;
worker_processes 1;
events
{
use epoll; #事件處理模型最佳化,沒有進行任何設定,nginx會自動選擇最優方法
worker_connections 2000;
}
http
{
include mime.types;
default_type application/octet-stream;
sendfile on; #用開啟檔案的高效傳輸模式
keepalive_timeout 25; #連線逾時的參數設定
server_tokens off;
tcp_nopush on; #減少網路報文段的數量
tcp_nodelay on; #提高I/O效能
client_header_timeout 15s; #讀取用戶端要求標頭資料的逾時時間
client_body_timeout 60s;
send_timeout 25s; #響應用戶端的逾時時間
client_max_body_size 8M; #調整上傳檔案的大小
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
FastCGI相關參數調優
user nginx nginx;
worker_processes 1;
worker_rlimit_nofile 65535;
events
{
use epoll;
worker_connections 2000;
}
http
{
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server_tokens off;
fastcgi_connect_timeout 240;
fastcgi_send_timeout 240;
fastcgi_read_timeout 240;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
#fastcgi_temp_path data/ngx_fcgi_tmp;
fastcgi_cache_path data/ngx_fcgi_cache levels=2:2 keys_zone=ngx_fcgi_cache:512m inactive=1d max_size=40g;
tcp_nopush on;
tcp_nodelay on;
client_header_timeout 15s;
client_body_timeout 60s;
send_timeout 25s;
client_max_body_size 8M;
include extra/www.conf;
include extra/bbs.conf;
include extra/blog.conf;
}
server {
listen 80;
server_name blog.etiantian.org;
location / {
root html/blog;
index index.php index.html index.htm;
}
location ~.*\.(php|php5)?$ {
root html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_cache ngx_fcgi_cache;
fastcgi_cache_valid 200 302 2h;
fastcgi_cache_valid 301 2d;
fastcgi_cache_valid and 2m;
fastcgi_cache_min_uses 2;
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_cache_key http://$host$request_uri;
}
配置Nginx gzip壓縮實現效能最佳化
[[email protected] conf]# vi mime.types
#檔案末尾添加如下內容:
gzip on;
gzip_mim_length 1k;
gzip_buffers 4 32k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/css text/xml application/javascript;
# gzip_vary on;
本文出自 “sky9890” 部落格,請務必保留此出處http://sky9896.blog.51cto.com/2330653/1881323
Nginx Web服務最佳化