user www www;
# ginx要開啓的進程數 通常等於cpu的總核數,不必開那麼多,1個nginx內存消耗10兆左右
worker_processes 4;
# 爲每一個進程分配cpu,上例中將4 個進程分配到4個cpu,固然能夠寫多個,或者將一 個進程分配到多個cpu。
worker_cpu_affinity 00000001 00000010 00000100 00001000;
# 每一個nginx進程打開文件描述符最大數目 配置要和系統的單進程打開文件數一
# 致,linux 2.6內核下開啓文件打開數爲65535,worker_rlimit_nofile就相應,應該填寫65535
# nginx調度時分配請求到進程並非那麼的均衡,假如超過會返回502錯誤。我這裏寫的大一點
worker_rlimit_nofile 100000;
# 開啓nginx錯誤日誌
error_log logs/error.log;
# 告訴nginx只能記錄嚴重的錯誤
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
# 每一個工做進程容許最大的同時鏈接數(Maxclient = work_processes * worker_connections)
# 默認1024
worker_connections 65535;
# 告訴nginx收到一個新鏈接通知後接受盡量多的鏈接。
multi_accept on;
# 設置用於複用客戶端線程的輪詢方法。若是你使用Linux 2.6+,你應該使用epoll。
# 若是你使用*BSD,你應該使用kqueue。
# 值得注意的是若是你不知道Nginx該使用哪一種輪詢方法的話,它會選擇一個最適合你操做系統的
use epoll;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
limit_req_zone $binary_remote_addr zone=allips:10m rate=20r/s;
#
#access_log logs/access.log main;
# 這個將爲打開文件指定緩存,默認是沒有啓用的,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執行的速度更快,但它能夠關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的
server_tokens off;
# 磁盤和TCP socket之間互相拷貝數據(或任意兩個文件描述符)。
# Pre-sendfile是傳送數據以前在用戶空間申請數據緩衝區
sendfile on;
# 告訴nginx在一個數據包裏發送全部頭文件,而不一個接一個的發送
#tcp_nopush on;
# 告訴nginx不要緩存數據,而是一段一段的發送,
# 當須要及時發送數據時,就應該給應用設置這個屬性,這樣發送一小塊數據信息時就不能當即獲得返回值。
#tcp_nodelay on;
upstream phpServer{
server 172.20.17.210:9000 weight=1 max_fails=2 fail_timeout=3;
server 172.20.17.211:9000 weight=1 max_fails=2 fail_timeout=3;
}
# keepalive超時時間
keepalive_timeout 65;
client_max_body_size 2m;
# 不許許IP直接訪問, 直接訪問報500錯誤
server {
listen 80 default_server;
server_name _;
return 500;
}
# 配置虛擬主機,過個server就複製多個
server {
listen 80;
# 開啓gzip壓縮
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 text/javascript application/x-httpd-php;
gzip_vary off;
gzip_disable "MSIE [1-6]\.";
# 配置域名
server_name www.xxxxx.com xxxxx.com;
# 配置網站目錄
root /usr/local/nginx/html/xxxxx.com;
# 只容許咱們的域名的訪問
if ($host !~ ^(xxxxx.com|www.xxxxx.com|images.xxxxx.com)$ ) {
return 444;
}
# 配置域名重定向
#if ($host != 'www.xxxxx.com' ) {
# rewrite ^/(.*)$ http://www.xxxxx.com/$1 permanent;
#}
# 限制可用的請求方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 444;
}
# 如何拒絕一些User-Agents
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403;
}
# 如何防止圖片盜鏈
location /images/ {
valid_referers none blocked www.xxxxx.com xxxxx.com;
if ($invalid_referer) {
return 403;
}
}
location / {
# 配置rewrite
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
# include /usr/local/nginx/html/yphp/.htaccess;
# rewrite ^/(.+)/(.+)[/]?$ /index.php?m=$1&a=$2 last;
# 配置默認訪問文件
index index.php index.html index.htm;
}
# 包含虛擬主機公用配置文件
include server.conf;
}
}
php-fpm.conf 文件配置優化
[global]
pid = run/php-fpm.pid
process_control_timeout=5
[www]
listen.allowed_clients = 127.0.0.1
user=www
group=www
pm=dynamic
# 增長 PHP-FPM 打開文件描述符的限制
rlimit_files = 51200
# 這個配置決定了php-fpm的總進程數,內存小的少設點
pm.max_children=20
# 併發數越大,此請求數應越大
pm.max_requests=10000
# 初始php-fpm進程數
pm.start_servers =10
# 動態方式下的起始php-fpm進程數量(設置太大可能會報錯,根據服務器配置設置)
pm.min_spare_servers = 5
# 動態方式下服務器空閒時最小php-fpm進程數量
pm.max_spare_servers = 10
# 表示在emergency_restart_interval所設值內出現SIGSEGV或者SIGBUS錯誤的php-cgi
# 進程數若是超過 emergency_restart_threshold個,php-fpm就會優雅重啓。這兩個選項通常保持默認值。
emergency_restart_threshold = 60
emergency_restart_interval = 60s
iptables 防火牆限制用戶訪問平率
# 下面的例子會阻止來自一個IP的60秒鐘內超過15個鏈接端口80的鏈接數
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –set
/sbin/iptables -A INPUT -p tcp –dport 80 -i eth0 -m state –state NEW -m recent –update –seconds 60 –hitcount 15 -j DROP
service iptables save