nginx作用及其配置
阿新 • • 發佈:2020-11-24
nginx
作用
-
代理
-
負載均衡
-
web快取
配置
-
全域性塊
-
events: nginx伺服器或與使用者的網路連線
-
http: 巢狀多個server,配置代理,快取,日誌定義等絕大多數功能和第三方模組的配置
-
server: 配置虛擬主機的相關引數,一個http中可以有多個server
-
location: 配置請求的路由,以及各種頁面的處理情況
#定義Nginx執行的使用者和使用者組 user nginx; #nginx程序數,建議設定為等於CPU總核心數。 worker_processes 1; #全域性錯誤日誌定義型別,[ debug | info | notice | warn | error | crit ] error_log /var/log/nginx/error.log warn; #程序pid檔案 pid /var/run/nginx.pid; events { #單個程序最大連線數(最大連線數=連線數*程序數) worker_connections 1024; } http { #副檔名與檔案型別對映表 include /etc/nginx/mime.types; #預設檔案型別 default_type application/octet-stream; #日誌格式設定 #$remote_addr與$http_x_forwarded_for用以記錄客戶端的ip地址; #$remote_user:用來記錄客戶端使用者名稱稱; #$time_local: 用來記錄訪問時間與時區; #$request: 用來記錄請求的url與http協議; #$status: 用來記錄請求狀態;成功是200, #$body_bytes_sent :記錄傳送給客戶端檔案主體內容大小; #$http_referer:用來記錄從那個頁面連結訪問過來的; #$http_user_agent:記錄客戶瀏覽器的相關資訊; #通常web伺服器放在反向代理的後面,這樣就不能獲取到客戶的IP地址了,通過$remote_add拿到的IP地址是反向代理伺服器的iP地址。反向代理伺服器在轉發請求的http頭資訊中,可以增加x_forwarded_for資訊,用以記錄原有客戶端的IP地址和原來客戶端的請求的伺服器地址。 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #定義本虛擬主機的訪問日誌 access_log /var/log/nginx/access.log main; #開啟高效檔案傳輸模式,sendfile指令指定nginx是否呼叫sendfile函式來輸出檔案,對於普通應用設為 on, #如果用來進行下載等應用磁碟IO重負載應用,可設定為off,以平衡磁碟與網路I/O處理速度,降低系統的負載。注意:如果圖片顯示不正常把這個改成off。 sendfile on; #此選項允許或禁止使用socke的TCP_CORK的選項,此選項僅在使用sendfile的時候使用 #tcp_nopush on; #長連線超時時間,單位是秒 keepalive_timeout 65; #開啟gzip壓縮輸出 #gzip on; include /etc/nginx/conf.d/*.conf; } server { #監聽埠 listen 80; listen [::]:80; #域名可以有多個,用空格隔開 server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; # ~ 匹配正則表示式 區分大小寫 # ~* 匹配正則表示式 不區分大小寫 # == 嚴格匹配 成功則不往下搜尋 # ~~ 不含正則表示式 按匹配度最高處理 location / { root /usr/share/nginx/html; index index.html index.htm; } # 錯誤 配置 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} }
負載均衡配置
1、熱備:如果你有2臺伺服器,當一臺伺服器發生事故時,才啟用第二臺伺服器給提供服務。伺服器處理請求的順序:AAAAAA突然A掛啦,BBBBBBBBBBBBBB.....
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #熱備
}
2、輪詢:nginx預設就是輪詢其權重都預設為1,伺服器處理請求的順序:ABABABABAB....
upstream mysvr { server 127.0.0.1:7878; server 192.168.10.121:3333; }
3、加權輪詢:跟據配置的權重的大小而分發給不同伺服器不同數量的請求。如果不設定,則預設為1。下面伺服器的請求順序為:ABBABBABBABBABB....
upstream mysvr {
server 127.0.0.1:7878 weight=1;
server 192.168.10.121:3333 weight=2;
}
4、ip_hash:nginx會讓相同的客戶端ip請求相同的伺服器。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333;
ip_hash;
}
代理配置
include mime.types; #副檔名與檔案型別對映表
default_type application/octet-stream; #預設檔案型別,預設為text/plain
#access_log off; #取消服務日誌
log_format myFormat ' $remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; #自定義格式
access_log log/access.log myFormat; #combined為日誌格式的預設值
sendfile on; #允許sendfile方式傳輸檔案,預設為off,可以在http塊,server塊,location塊。
sendfile_max_chunk 100k; #每個程序每次呼叫傳輸數量不能大於設定的值,預設為0,即不設上限。
keepalive_timeout 65; #連線超時時間,預設為75s,可以在http,server,location塊。
proxy_connect_timeout 1; #nginx伺服器與被代理的伺服器建立連線的超時時間,預設60秒
proxy_read_timeout 1; #nginx伺服器想被代理伺服器組發出read請求後,等待響應的超時間,預設為60秒。
proxy_send_timeout 1; #nginx伺服器想被代理伺服器組發出write請求後,等待響應的超時間,預設為60秒。
proxy_http_version 1.0 ; #Nginx伺服器提供代理服務的http協議版本1.0,1.1,預設設定為1.0版本。
#proxy_method get; #支援客戶端的請求方法。post/get;
proxy_ignore_client_abort on; #客戶端斷網時,nginx伺服器是否終端對被代理伺服器的請求。預設為off。
proxy_ignore_headers "Expires" "Set-Cookie"; #Nginx伺服器不處理設定的http相應投中的頭域,這裡空格隔開可以設定多個。
proxy_intercept_errors on; #如果被代理伺服器返回的狀態碼為400或者大於400,設定的error_page配置起作用。預設為off。
proxy_headers_hash_max_size 1024; #存放http報文頭的雜湊表容量上限,預設為512個字元。
proxy_headers_hash_bucket_size 128; #nginx伺服器申請存放http報文頭的雜湊表容量大小。預設為64個字元。
proxy_next_upstream timeout; #反向代理upstream中設定的伺服器組,出現故障時,被代理伺服器返回的狀態值。error|timeout|invalid_header|http_500|http_502|http_503|http_504|http_404|off
#proxy_ssl_session_reuse on; 預設為on,如果我們在錯誤日誌中發現“SSL3_GET_FINSHED:digest check failed”的情況時,可以將該指令設定為off