後端必備 Nginx 配置


後端必備 Nginx 配置


後端必備 Nginx 配置

概要

  • 防盜鏈
  • 根據文件類型設置過期時間
  • 靜態資源訪問
  • 日誌配置
    • 日誌字段說明
    • access_log 訪問日誌
    • error_log 日誌
    • 日誌切割
  • 反向代理
  • 禁止指定user_agent
  • nginx訪問控制
  • 負載均衡

防盜鏈


<code>location ~* \\.(gif|jpg|png)$ {
# 只允許 192.168.0.1 請求資源

valid_referers none blocked 192.168.0.1;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}複製代碼/<code>


根據文件類型設置過期時間


<code>location ~.*\\.css$ {
expires 1d;
break;
}
location ~.*\\.js$ {
expires 1d;
break;
}

location ~ .*\\.(gif|jpg|jpeg|png|bmp|swf)$ {
access_log off;
expires 15d; #保存15天
break;
}

# curl -x127.0.0.1:80 http://www.test.com/static/image/common/logo.png -I #測試圖片的max-age
複製代碼/<code>


靜態資源訪問

<code>http {
# 這個將為打開文件指定緩存,默認是沒有啟用的,max 指定緩存數量,
# 建議和打開文件數一致,inactive 是指經過多長時間文件沒被請求後刪除緩存。
open_file_cache max=204800 inactive=20s;


# open_file_cache 指令中的inactive 參數時間內文件的最少使用次數,
# 如果超過這個數字,文件描述符一直是在緩存中打開的,如上例,如果有一個
# 文件在inactive 時間內一次沒被使用,它將被移除。
open_file_cache_min_uses 1;

# 這個是指多長時間檢查一次緩存的有效信息
open_file_cache_valid 30s;

# 默認情況下,Nginx的gzip壓縮是關閉的, gzip壓縮功能就是可以讓你節省不
# 少帶寬,但是會增加服務器CPU的開銷哦,Nginx默認只對text/html進行壓縮 ,
# 如果要對html之外的內容進行壓縮傳輸,我們需要手動來設置。
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;
}
}複製代碼/<code>

日誌配置

日誌字段說明

字段說明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_iso8601ISO8601標準格式下的本地時間time_local記錄訪問時間與時區

access_log 訪問日誌

<code>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;
}複製代碼/<code>

error_log 日誌

<code>error_log  /srv/log/nginx/nginx_error.log  error;
# error_log /dev/null; # 真正的關閉錯誤日誌
http {
# ...
}複製代碼/<code>

日誌切割

<code># 和apache不同的是,nginx沒有apache一樣的工具做切割,需要編寫腳本實現。# 在/usr/local/sbin下寫腳本


#!/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
複製代碼/<code>

反向代理

<code>http {
include mime.types;
server_tokens off;

## 配置反向代理的參數
server {
listen 8080;

## 1. 用戶訪問 http://ip:port,則反向代理到 https://github.com
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;
}

## 2.用戶訪問 http://ip:port/README.md,則反向代理到
## https://github.com/zibinli/blog/blob/master/README.md
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;
}
}
}複製代碼/<code>

禁止指定user_agent


<code>#虛擬主機的配置文件里加入:

if ($http_user_agent ~* 'baidu|360|sohu') #禁止useragent為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
複製代碼/<code>

nginx訪問控制

<code># 可以設置一些配置禁止一些ip的訪問

deny 127.0.0.1; #全局定義限制,location裡的是局部定義的。如果兩者衝突,以location這種精確地優先,

location ~ .*admin\\.php$ {
#auth_basic "cct auth";
#auth_basic_user_file /usr/local/nginx/conf/.htpasswd;

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;
}
複製代碼/<code>

負載均衡

<code>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;
}
}
}複製代碼/<code>


如果覺得我的文章對你有用,請轉發、點贊鼓勵


分享到:


相關文章: