概要
- 防盜鏈
- 根據文件類型設置過時時間
- 靜態資源訪問
- 日誌配置
- 日誌字段說明
- access_log 訪問日誌
- error_log 日誌
- 日誌切割
- 反向代理
- 禁止指定user_agent
- nginx訪問控制
- 負載均衡
防盜鏈
location ~* \.(gif|jpg|png)$ {
valid_referers none blocked 192.168.0.1;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}複製代碼
根據文件類型設置過時時間
location ~.*\.css$ {
expires 1d;
break;
}
location ~.*\.js$ {
expires 1d;
break;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 15d;
break;
}
複製代碼
靜態資源訪問
http {
open_file_cache max=204800 inactive=20s;
open_file_cache_min_uses 1;
open_file_cache_valid 30s;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
server {
listen 80;
server_name www.test.com;
charset utf-8;
root /data/www.test.com;
index index.html index.htm;
}
}複製代碼
日誌配置
日誌字段說明
字段 |
說明 |
|
remote_addr 和 http_x_forwarded_for |
客戶端 IP 地址 |
remote_user |
客戶端用戶名稱 |
request |
請求的 URI 和 HTTP 協議 |
status |
請求狀態 |
body_bytes_sent |
返回給客戶端的字節數,不包括響應頭的大小 |
bytes_sent |
返回給客戶端總字節數 |
connection |
鏈接的序列號 |
connection_requests |
當前同一個 TCP 鏈接的的請求數量 |
msec |
日誌寫入時間。單位爲秒,精度是毫秒 |
pipe |
若是請求是經過HTTP流水線(pipelined)發送,pipe值爲「p」,不然爲「.」 |
http_referer |
記錄從哪一個頁面連接訪問過來的 |
http_user_agent |
記錄客戶端瀏覽器相關信息 |
request_length |
請求的長度(包括請求行,請求頭和請求正文) |
time_iso8601 |
ISO8601標準格式下的本地時間 |
time_local |
記錄訪問時間與時區 |
access_log 訪問日誌
http {
log_format access '$remote_addr - $remote_user [$time_local] $host "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$clientip"';
access_log /srv/log/nginx/talk-fun.access.log access;
}複製代碼
error_log 日誌
error_log /srv/log/nginx/nginx_error.log error;
http {
}複製代碼
日誌切割
# 和apache不一樣的是,nginx沒有apache同樣的工具作切割,須要編寫腳本實現。
#!/bin/bash
dd=$(date -d '-1 day' +%F)[ -d /tmp/nginx_log ] || mkdir /tmp/nginx_log
mv /tmp/nginx_access.log /tmp/nginx_log/$dd.log
/etc/init.d/nginx reload > /dev/null
複製代碼
反向代理
http {
include mime.types;
server_tokens off;
server {
listen 8080;
location / {
proxy_pass https://github.com;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /README.md {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass https://github.com/zibinli/blog/blob/master/README.md;
}
}
}複製代碼
禁止指定user_agent
if ($http_user_agent ~* 'baidu|360|sohu')
{
return 403;
}
location / 和 location ~ / 優先級是不同的。
結合這個文章研究一下吧 http://blog.itpub.net/27181165/viewspace-777202/
curl -A "baidu" -x127.0.0.1:80 www.test.com/forum.php -I 該命令指定百度爲user_agent,返回403
複製代碼
nginx訪問控制
deny 127.0.0.1;
location ~ .*admin\.php$ {
allow 127.0.0.1; 只容許127.0.0.1的訪問,其餘均拒絕
deny all;
include fastcgi_params;
fastcgi_pass unix:/tmp/www.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;
}
複製代碼
負載均衡
http {
upstream test.net {
ip_hash;
server 192.168.10.13:80;
server 192.168.10.14:80 down;
server 192.168.10.15:8009 max_fails=3 fail_timeout=20s;
server 192.168.10.16:8080;
}
server {
location / {
proxy_pass http://test.net;
}
}
}複製代碼
若是以爲個人文章對你有用,請點贊鼓勵