nginx 配置文件
#主配置文件
[root@nginx_1 conf]# more nginx.conf
user root;
worker_processes 4;
error_log logs/error.log;
pid sbin/nginx.pid;
events {
use epoll;
worker_connections 65535;
multi_accept on;
}
stream {
upstream cmp {
server 192.168.27.38:8080 max_fails=1 fail_timeout=10s;
server 192.168.27.39:8080 max_fails=1 fail_timeout=10s backup;
}
server {
listen 22222;
proxy_pass cmp;
}
}
http {
include mime.types;
default_type application/octet-stream;
types {
application/pdf;
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_addr"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
fastcgi_buffers 8 128k;
proxy_connect_timeout 1000;
proxy_send_timeout 1000;
proxy_read_timeout 1000;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
client_max_body_size 200m;
server_tokens off;
include vhosts/local_upstream.conf;
include vhosts/server.conf;
proxy_buffering on;
proxy_buffer_size 256k;
proxy_buffers 64 256K;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
proxy_temp_path /tmp/temp_buffer;
########
# 開(kāi)啟壓縮機(jī)制
gzip on;
# 指定會(huì)被壓縮的文件類型(也可自己配置其他類型)
gzip_types text/plain application/javascript text/css application/xml text/javascript image/jpeg image/gif image/png;
# 設(shè)置壓縮級(jí)別鳖目,越高資源消耗越大绪爸,但壓縮效果越好
gzip_comp_level 5;
# 在頭部中添加Vary: Accept-Encoding(建議開(kāi)啟)
gzip_vary on;
# 處理壓縮請(qǐng)求的緩沖區(qū)數(shù)量和大小
gzip_buffers 16 8k;
# 對(duì)于不支持壓縮功能的客戶端請(qǐng)求不開(kāi)啟壓縮機(jī)制
gzip_disable "MSIE [1-6]\."; # 低版本的IE瀏覽器不支持壓縮
# 設(shè)置壓縮響應(yīng)所支持的HTTP最低版本
gzip_http_version 1.1;
# 設(shè)置觸發(fā)壓縮的最小閾值
gzip_min_length 10k;
# 關(guān)閉對(duì)后端服務(wù)器的響應(yīng)結(jié)果進(jìn)行壓縮
gzip_proxied off;
}
#server配置文件
[root@nginx_1 conf]# more server.conf
server{
listen 8099;
server_name mirrors.aliyun.com;
location ~ ^/*{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_buffering off;
chunked_transfer_encoding off;
proxy_pass http://mirrors.aliyun.com;
client_max_body_size 512m;
}
}
server {
listen 443 ssl http2;
ssl_certificate /home/nginx/conf/crt/9371738__test.com.pem;
ssl_certificate_key /home/nginx/conf/crt/9371738__test.com.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
ssl_prefer_server_ciphers on;
proxy_read_timeout 3600s;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
server_name cloud.test.com;
include vhosts/local.conf;
#if ($host !~* ^(cloud.test.com|[a-zA-Z0-9].test.com|120.test.com)$) {
if ($host !~* "^[a-zA-Z0-9_]{3,10}\.test.com$") {
return 444;
}
error_page 404 /error.html;
error_page 500 502 503 504 /error.html;
location = /error.html {
root html;
}
server {
listen 8010 default_server;
server_name test.com;
include vhosts/local.conf;
error_page 404 /error.html;
error_page 500 502 503 504 /error.html;
location = /error.html {
root html;
}
}
#local配置文件
[root@nginx_1 vhosts]# more local.conf
location /ws/ {
proxy_pass http://ws;
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;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location /test_api/ {
proxy_pass http://test_api;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /test_api2{
proxy_pass http://test_api2;
proxy_redirect http:// https://;
proxy_set_header Host $host:1443;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 64k;
#add_header 'Access-Control-Allow-Origin' '*.ccod.com' always;
add_header Access-Control-Allow-Origin $http_origin;
add_header X-Content-Type-Options "nosniff";
add_header X-XSS-Protection "1";
add_header Set-Cookie "HttpOnly";
add_header Set-Cookie "Secure";
add_header X-Frame-Options SAMEORIGIN;
add_header Content-Security-Policy "default-src *;style-src 'self' 'unsafe-inline';script-src 'self' 'unsafe-inline' 'unsafe-eval';img-src * data:;worker-src * blob:;font-src 'self' data:;";
add_header 'Access-Control-Allow-Methods' "POST, GET";
}
#local_upstream配置文件
[root@nginx_1 vhosts]# more local_upstream.conf
upstream gls{
sticky;
server 192.168.1.2:8080;
server 192.168.1.3:8080;
check interval=3000 rise=3 fall=5 timeout=1000 type=tcp;
}
nginx配置文件優(yōu)化
- worker_processes nginx進(jìn)程工作數(shù)
#cat /proc/cpuinfo |grep processor|wc -l 庸推,或者是設(shè)置auto自動(dòng)根據(jù)CPU 核心數(shù)來(lái)確定 worker 進(jìn)程的數(shù)量都行狼忱。
worker_processes auto;
worker_processes 2;
events {
#采用epoll事件模型面哼,處理效率高蹬蚁。
use epoll;
#worker_connections 是單個(gè)worker進(jìn)程允許客戶端最大連接數(shù),根據(jù)服務(wù)器性能和內(nèi)存來(lái)指定致盟,需要服務(wù)器系統(tǒng)open files數(shù)對(duì)應(yīng)大于worker_connections即可腥泥。
worker_connections 65535;
#開(kāi)啟多連接處理,Nginx 會(huì)使用多線程或多進(jìn)程的方式來(lái)處理多個(gè)客戶端連接請(qǐng)求匾南,從而提高服務(wù)器的性能和響應(yīng)速度。
multi_accept on;
}
sendfile on;
#啟用 Nagle 算法蛔外,禁用延遲發(fā)送,可以有效地減少 TCP 連接的延遲蛆楞,提高 Nginx 的性能和響應(yīng)速度
tcp_nodelay on;
#客戶端保持會(huì)話超時(shí)時(shí)間,超過(guò)該時(shí)間夹厌,會(huì)斷開(kāi)連接
keepalive_timeout 65;
# 開(kāi)啟文件緩存豹爹,最大緩存文件數(shù)為 204800,不活躍緩存時(shí)間為 20 秒
open_file_cache max=204800 inactive=20s;
# 設(shè)置文件緩存的有效時(shí)間為 30 秒
open_file_cache_valid 30s;
# 設(shè)置文件緩存使用次數(shù)的最小值為 1
open_file_cache_min_uses 1;
#客戶端請(qǐng)求頭部緩沖區(qū)大小矛纹,根據(jù)系統(tǒng)分頁(yè)大小來(lái)進(jìn)行設(shè)置臂聋;系統(tǒng)命令查看getconf PAGESIZE
client_header_buffer_size 4k;
#被設(shè)置為 on 時(shí),Nginx 會(huì)在客戶端與服務(wù)器建立連接后或南,將連接的超時(shí)時(shí)間重置為默認(rèn)值孩等。
#啟用連接超時(shí)重置功能可能會(huì)導(dǎo)致一些性能問(wèn)題,因?yàn)樗鼤?huì)增加 Nginx 的負(fù)載采够。因此瞎访,在使用該指令時(shí)需要謹(jǐn)慎考慮是否真的需要啟用該功能。
#reset_timeout_connection on;
#設(shè)置FastCGI緩沖區(qū)的大小為8個(gè)緩沖區(qū)吁恍,每個(gè)緩沖區(qū)大小為128KB。
fastcgi_buffers 8 128k;
#設(shè)置代理連接超時(shí)時(shí)間為600秒播演。
proxy_connect_timeout 600s;
#設(shè)置代理發(fā)送超時(shí)時(shí)間為600秒冀瓦。
proxy_send_timeout 600s;
設(shè)置代理讀取超時(shí)時(shí)間為600秒。
proxy_read_timeout 600s;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
#設(shè)置客戶端請(qǐng)求體的最大大小為200MB写烤。
client_max_body_size 200m;
#關(guān)閉服務(wù)器令牌功能,隱藏版本信息翼闽。
server_tokens off;
include vhosts/local_upstream.conf;
include vhosts/server.conf;
#開(kāi)啟代理緩沖功能。
proxy_buffering on;
#設(shè)置代理緩沖區(qū)大小為256KB洲炊。
proxy_buffer_size 256k;
#設(shè)置代理緩沖區(qū)的數(shù)量為64個(gè)感局,每個(gè)緩沖區(qū)大小為256KB。
proxy_buffers 64 256K;
#設(shè)置代理繁忙緩沖區(qū)的大小為512KB暂衡。
proxy_busy_buffers_size 512k;
#設(shè)置代理臨時(shí)文件寫(xiě)入大小為512KB询微。
proxy_temp_file_write_size 512k;
#設(shè)置代理臨時(shí)文件路徑為/tmp/temp_buffer。
proxy_temp_path /tmp/temp_buffer;
#ginx還會(huì)設(shè)置響應(yīng)頭中的“Expires”和“Cache-Control”頭標(biāo)狂巢,使瀏覽器在30天內(nèi)緩存該頁(yè)面的內(nèi)容撑毛。如果用戶再次訪問(wèn)該頁(yè)面,Nginx會(huì)直接從緩存中讀取內(nèi)容唧领,而不會(huì)向后端服務(wù)器發(fā)送新的請(qǐng)求
location ~* \.(jpg|jpeg|png|gif|ico|swf|flv|bmp)$
expires 30d;
access_log off;
location ~* \.(js|css)$
expires 7d;
access_log off;
location ~* \.(jpg|jpeg|png|gif|ico|swf|flv|bmp|zip|mp3|mp4|)$ {
valid_referers none blocked www.test.com *.test123.com;
if ($invalid_referer) {
return 403;
}
}
nginx服務(wù)器內(nèi)核優(yōu)化
#表示進(jìn)程(比如一個(gè)worker進(jìn)程)可以同時(shí)打開(kāi)的最大句柄數(shù)藻雌,這 個(gè)參數(shù)直接限制最大并發(fā)連接數(shù)雌续,需根據(jù)實(shí)際情況配置
fs.file-max = 999999
#設(shè)置為1,表示允許將TIME-WAIT狀態(tài)的socket重新用于新的 TCP連接胯杭,這對(duì)于服務(wù)器來(lái)說(shuō)很有意義驯杜,因?yàn)榉?wù)器上總會(huì)有大量TIME-WAIT狀態(tài)的連接。
net.ipv4.tcp_tw_reuse = 1
#表示當(dāng)keepalive啟用時(shí)做个,TCP發(fā)送keepalive消息的頻度鸽心。 默認(rèn)是2小時(shí),若將其設(shè)置得小一些叁温,可以更快地清理無(wú)效的連接再悼。
net.ipv4.tcp_keepalive_time = 15
#表示當(dāng)服務(wù)器主動(dòng)關(guān)閉連接時(shí),socket保持在FIN-WAIT-2狀態(tài)的最大時(shí)間膝但。
net.ipv4.tcp_fin_timeout = 15
#操作系統(tǒng)允許TIME_WAIT套接字?jǐn)?shù)量的最大值冲九, 如果超過(guò)這個(gè)數(shù)字,TIME_WAIT套接字將立刻被清除并打印警告信息跟束。該參數(shù)默認(rèn)為 180000莺奸,過(guò)多的TIME_WAIT套接字會(huì)使Web服務(wù)器變慢。
net.ipv4.tcp_max_tw_buckets = 5000
#定義了在UDP和TCP連接中本地(不包括連接的遠(yuǎn)端) 端口的取值范圍冀宴。
net.ipv4.ip_local_port_range = 1024 65000
#定義了TCP接收緩存(用于TCP接收滑動(dòng)窗口)的最小 值灭贷、默認(rèn)值、最大值略贮。
net.ipv4.tcp_rmem = 4096 32768 262144
#定義了TCP發(fā)送緩存(用于TCP發(fā)送滑動(dòng)窗口)的最小 值甚疟、默認(rèn)值、最大值逃延。
net.ipv4.tcp_wmem = 4096 32768 262144
#選項(xiàng)用于記錄那些尚未收到客戶端確認(rèn)信息的連接請(qǐng)求的最大值览妖。對(duì)于有128MB內(nèi)存的系統(tǒng)而言,此參數(shù)的默認(rèn)值是1024揽祥,對(duì)小內(nèi)存的系統(tǒng)則是128讽膏。
net.ipv4.tcp_max_orphans = 262144
#當(dāng)網(wǎng)卡接收數(shù)據(jù)包的速度大于內(nèi)核處理的速度時(shí),會(huì)有一個(gè)隊(duì)列 保存這些數(shù)據(jù)包拄丰。這個(gè)參數(shù)表示該隊(duì)列的最大值府树。
net.core.netdev_max_backlog = 262144
#表示內(nèi)核套接字接收緩存區(qū)默認(rèn)的大小。
net.core.rmem_default = 262144
#表示內(nèi)核套接字發(fā)送緩存區(qū)默認(rèn)的大小料按。
net.core.wmem_default = 262144
#表示內(nèi)核套接字接收緩存區(qū)的最大大小奄侠。
net.core.rmem_max = 2097152
#表示內(nèi)核套接字發(fā)送緩存區(qū)的最大大小。
net.core.wmem_max = 2097152
#選項(xiàng)表示當(dāng)每個(gè)網(wǎng)絡(luò)接口接收數(shù)據(jù)包的速率比內(nèi)核處理這些包的速率快時(shí)站绪,允許發(fā)送到隊(duì)列的數(shù)據(jù)包的最大數(shù)目遭铺。
net.core.somaxconn = 262144
#設(shè)置為 1。該參數(shù)與性能無(wú)關(guān),用于解決TCP的SYN攻擊魂挂。
net.ipv4.tcp_syncookies = 1
#表示TCP三次握手建立階段接收SYN請(qǐng)求隊(duì)列的最大 長(zhǎng)度甫题,默認(rèn)為1024,將其設(shè)置得大一些可以使出現(xiàn)Nginx繁忙來(lái)不及accept新連接的情況時(shí)涂召, Linux不至于丟失客戶端發(fā)起的連接請(qǐng)求坠非。
net.ipv4.tcp_max_syn_backlog=262144