適用於使用Nginx進行部署的Web網站。php
熟悉Nginx配置,可以Nginx進行部署,並能針對站點使用Nginx進行安全加固。html
一、 根據站點開放端口,進程ID,確認站點採用Nginx進行部署;nginx
二、 找到Nginx安裝目錄,針對具體站點對配置文件進行修改;安全
三、 在執行過程當中如有任何疑問或建議,應及時反饋。微信
一、備份nginx.conf 配置文件。併發
修改配置,按以下設置日誌記錄文件、記錄內容、記錄格式,添加標籤爲main的log_format格式測試
(http標籤內,在全部的server標籤內能夠調用): log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"';
二、在server標籤內,定義日誌路徑網站
access_log logs/host.access.log main
三、保存,而後後重啓nginx服務。spa
備份nginx.conf配置文件。日誌
編輯配置文件,HTTP模塊添加以下一行內容:
autoindex off;
保存,而後後重啓nginx服務。
備份nginx.conf配置文件。
編輯配置文件,在server標籤內添加以下內容:
#示例:去掉單個目錄的PHP執行權限 location ~ /attachments/.*\.(php|php5)?$ { deny all; } #示例:去掉多個目錄的PHP執行權限 location ~ /(attachments|upload)/.*\.(php|php5)?$ { deny all; }
保存,而後後重啓nginx服務。
須要注意兩點:
一、以上的配置文件代碼須要放到 location ~ .php{...}上面,若是放到下面是無效的;
二、attachments須要寫相對路徑,不能寫絕對路徑。
備份nginx.conf配置文件。
修改配置,在http{}段加入以下內容
http { ... fastcgi_intercept_errors on; error_page 401 /401.html; error_page 402 /402.html; error_page 403 /403.html; error_page 404 /404.html; error_page 405 /405.html; error_page 500 /500.html; ... } 修改內容: ErrorDocument 400 /custom400.html ErrorDocument 401 /custom401.html ErrorDocument 403 /custom403.html ErrorDocument 404 /custom404.html ErrorDocument 405 /custom405.html ErrorDocument 500 /custom500.html 其中401.html、402.html、403.html、404.html、405.html、500.html 爲要指定的錯誤提示頁面。
保存,重啓 nginx 服務生效
4.5.1 隱藏版本信息
備份nginx.conf配置文件。
編輯配置文件,添加http模塊中以下一行內容:
server_tokens off;
保存,而後後重啓nginx服務。
4.5.2 限制HTTP請求方法
備份nginx.conf配置文件。
編輯配置文件,添加以下內容:
if ($request_method !~ ^(GET|HEAD|POST)$ ) { return 444; }
保存,而後後重啓nginx服務。
備註:只容許經常使用的GET和POST方法,頂多再加一個HEAD方法
4.5.3 限制IP訪問
備份nginx.conf配置文件。
編輯配置文件,在server標籤內添加以下內容:
location / { deny 192.168.1.1; #拒絕IP allow 192.168.1.0/24; #容許IP allow 10.1.1.0/16; #容許IP deny all; #拒絕其餘全部IP }
保存,而後後重啓nginx服務。
4.5.4 限制併發和速度
備份nginx.conf配置文件。
編輯配置文件,在server標籤內添加以下內容:
limit_zone one $binary_remote_addr 10m; server { listen 80; server_name down.test.com; index index.html index.htm index.php; root /usr/local/www; #Zone limit; location / { limit_conn one 1; limit_rate 20k; } ……… }
保存,而後後重啓nginx服務。
4.5.5 控制超時時間
備份nginx.conf配置文件。
編輯配置文件,具體設置以下:
client_body_timeout 10; #設置客戶端請求主體讀取超時時間 client_header_timeout 10; #設置客戶端請求頭讀取超時時間 keepalive_timeout 5 5; #第一個參數指定客戶端鏈接保持活動的超時時間,第二個參數是可選的,它指定了消息頭保持活動的有效時間 send_timeout10; #指定響應客戶端的超時時間
保存,而後後重啓nginx服務。
4.6.1 Nginx降權
備份nginx.conf配置文件。
編輯配置文件,添加以下一行內容:
user nobody;
保存,而後後重啓nginx服務。
4.6.2 防盜鏈
備份nginx.conf配置文件。
編輯配置文件,在server標籤內添加以下內容:
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip)$ { valid_referers none blocked server_names *.nsfocus.com http://localhost baidu.com; if ($invalid_referer) { rewrite ^/ [img]http://www.XXX.com/images/default/logo.gif[/img]; # return 403; } }
保存,而後後重啓nginx服務。
4.6.3 補丁更新
一、軟件信息
查看軟件版本 nginx -v
測試配置文件 nginx –t
二、補丁安裝
手動安裝補丁或安裝最新版本軟件
最後
歡迎關注我的微信公衆號:Bypass--,每週原創一篇技術乾貨。