日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

nginx的配置優(yōu)化及經(jīng)常使用的超時配置說明

作者:多放香菜少加蔥 更新時間: 2022-07-21 編程語言

1.開啟gzip

可以從服務器端解決靜態(tài)資源包傳輸過大問題,但是壓縮會占用服務器端cpu計算資源

配置說明
在這里插入圖片描述
配置事例
在這里插入圖片描述

配置nginx多核

在這里插入圖片描述
相關安全配置
在這里插入圖片描述
二. Nginx的優(yōu)化模板

user root;
worker_processes 4;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;


events {
worker_connections 10240;
}


http {

gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.1;
gzip_comp_level 9;
gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php application/javascript application/json;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
include mime.types;
default_type application/octet-stream;

sendfile on;
keepalive_timeout 65;
client_header_timeout 150;
client_body_timeout 1800;
send_timeout 150;
proxy_read_timeout 1800;   
proxy_hide_header X-Powered-By;
proxy_hide_header Server;

server {

listen 8080;
server_name localhost;
server_tokens off;
client_max_body_size 1024m;

# 工作流前端
location /wffront/ {
proxy_pass http://127.0.0.1:8081/wffront/;
}

location / {
root /hr;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}

#location ~* \.(?:jpg|jpeg|gif|png|ico|woff2)$ {
# expires 1M;
# add_header Cache-Control "public";
#}
location /prod-api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:8081/;
}
}
}

參數(shù)詳解

sendfile on;
設置為on表示啟動高效傳輸文件的模式。sendfile可以讓Nginx在傳輸文件時直接在磁盤和tcp socket之間傳輸數(shù)據(jù)。如果這個參數(shù)不開啟,會先在用戶空間(Nginx進程空間)申請一個buffer,用read函數(shù)把數(shù)據(jù)從磁盤讀到cache,再從cache讀取到用戶空間的buffer,再用write函數(shù)把數(shù)據(jù)從用戶空間的buffer寫入到內(nèi)核的buffer,最后到tcp socket。開啟這個參數(shù)后可以讓數(shù)據(jù)不用經(jīng)過用戶buffer。

keepalive_timeout
keepalive_timeout參數(shù)是一個請求完成之后還要保持連接多久,不是請求時間多久,目的是保持長連接,減少創(chuàng)建連接過程給系統(tǒng)帶來的性能損耗,類似于線程池,數(shù)據(jù)庫連接池。

client_header_timeout
客戶端向服務端發(fā)送一個完整的 request header 的超時時間。如果客戶端在指定時間內(nèi)沒有發(fā)送一個完整的 request header,Nginx 返回 HTTP 408(Request Timed Out)

client_body_timeout
指定客戶端與服務端建立連接后發(fā)送 request body 的超時時間。如果客戶端在指定時間內(nèi)沒有發(fā)送任何內(nèi)容,Nginx 返回 HTTP 408(Request Timed Out)

send_timeout
服務端向客戶端傳輸數(shù)據(jù)的超時時間

proxy_read_timeout
nginx 接收 upstream server 數(shù)據(jù)超時, 默認 60s, 如果連續(xù)的 60s 內(nèi)沒有收到 1 個字節(jié), 連接關閉;根據(jù)應用不同可配置 uwsgi_send_timeout/fascgi_send_timeout/proxy_send_timeout

proxy_hide_header X-Powered-By;
規(guī)則描述:
x-powered-By表示網(wǎng)站是用什么技術(shù)開發(fā)的,它會泄漏開發(fā)語言、版本號和框架等信息,有安全隱患,需要隱藏掉。
根據(jù):
審計描述:
檢查nginx.conf文件,是否存在以下配置:
proxy_hide_header X-Powered-By;
修改建議:
在nginx.conf文件中使用指令proxy_hide_header隱藏它

proxy_hide_header X-Powered-By;
proxy_hide_header Server;
可以隱藏主機頭信息,業(yè)務使用的nginx反向代理在顯示響應頭時將后端機器的PHP版本顯示了出來,雖說在用戶體驗和安全上并無大的影響,但是將PHP版本信息暴漏出來總是不妥的。。
在這里插入圖片描述

nginx經(jīng)常使用的超時配置說明
client_header_timeout

語法 client_header_timeout time
默認值 60s
上下文 http server
說明 指定等待client發(fā)送一個請求頭的超時時間(例如:GET / HTTP/1.1).僅當在一次read中,沒有收到請求頭,才會算成超時。若是在超時時間內(nèi),client沒發(fā)送任何東西,nginx返回HTTP狀態(tài)碼408(“Request timed out”)

client_body_timeout

語法 client_body_timeout time
默認值 60s
上下文 http server location
說明 該指令設置請求體(request body)的讀超時時間。僅當在一次readstep中,沒有獲得請求體,就會設為超時。超時后,nginx返回HTTP狀態(tài)碼408(“Request timed out”)

keepalive_timeout

語法 keepalive_timeout timeout [ header_timeout ]
默認值 75s
上下文 http server location
說明 第一個參數(shù)指定了與client的keep-alive鏈接超時時間。服務器將會在這個時間后關閉鏈接。可選的第二個參數(shù)指定了在響應頭Keep-Alive: timeout=time中的time值。這個頭可以讓一些瀏覽器主動關閉鏈接,這樣服務器就沒必要要去關閉鏈接了。沒有這個參數(shù),nginx不會發(fā)送Keep-Alive響應頭(盡管并非由這個頭來決定鏈接是否“keep-alive”)
兩個參數(shù)的值可并不相同

注意不一樣瀏覽器怎么處理“keep-alive”頭
MSIE和Opera忽略掉"Keep-Alive: timeout=" header.
MSIE保持鏈接大約60-65秒,而后發(fā)送TCP RST
Opera永久保持長鏈接
Mozilla keeps the connection alive for N plus about 1-10 seconds.
Konqueror保持長鏈接N秒

lingering_timeout

語法 lingering_timeout time
默認值 5s
上下文 http server location
說明 lingering_close生效后,在關閉鏈接前,會檢測是否有用戶發(fā)送的數(shù)據(jù)到達服務器,若是超過lingering_timeout時間后尚未數(shù)據(jù)可讀,就直接關閉鏈接;不然,必須在讀取完鏈接緩沖區(qū)上的數(shù)據(jù)并丟棄掉后才會關閉鏈接。

resolver_timeout

語法 resolver_timeout time
默認值 30s
上下文 http server location
說明 該指令設置DNS解析超時時間

proxy_connect_timeout

語法 proxy_connect_timeout time
默認值 60s
上下文 http server location
說明 該指令設置與upstream server的鏈接超時時間,有必要記住,這個超時不能超過75秒。
這個不是等待后端返回頁面的時間,那是由proxy_read_timeout聲明的。若是你的upstream服務器起來了,可是hanging住了(例如,沒有足夠的線程處理請求,因此把你的請求放到請求池里稍后處理),那么這個聲明是沒有用的,因為與upstream服務器的鏈接已經(jīng)創(chuàng)建了。

proxy_read_timeout

語法 proxy_read_timeout time
默認值 60s
上下文 http server location
說明 該指令設置與代理服務器的讀超時時間。它決定了nginx會等待多長時間來得到請求的響應。這個時間不是得到整個response的時間,而是兩次reading操做的時間。

proxy_send_timeout

語法 proxy_send_timeout time
默認值 60s
上下文 http server location
說明 這個指定設置了發(fā)送請求給upstream服務器的超時時間。超時設置不是為了整個發(fā)送期間,而是在兩次write操做期間。若是超時后,upstream沒有收到新的數(shù)據(jù),nginx會關閉鏈接

proxy_upstream_fail_timeout(fail_timeout)

語法 server address [fail_timeout=30s]默認值 10s上下文 upstream說明 Upstream模塊下 server指令的參數(shù),設置了某一個upstream后端失敗了指定次數(shù)(max_fails)后,該后端不可操做的時間,默認為10秒

原文鏈接:https://blog.csdn.net/qq_41512902/article/details/125606689

欄目分類
最近更新