一.nginx的配置文件
user work work; //運行nginx的用戶(組)
worker_processes auto; //work進程數1個主進程
pid /opt/soft/nginx/run/nginx.pid; //pid存放位置
error_log /opt/log/nginx/localhost/error.log error; //全局nginx運行時 error info,notice,warn,error,crit,alert,emerg//所有指定的文件,運行nginx進程的用戶必須有寫權限,否則報錯
worker_rlimit_nofile 65535;#一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,但是nginx分配請求並不均勻,所以建議與ulimit -n的值保持一致。
include /opt/soft/nginx/conf.d/*.conf; //引入配置文件
events { //負責網絡連接配置 ,比較影響性能
accept_mutex on; //防止多個進程對連接的爭搶 默認開啟,(影響性能)
multi_accept on; //配置是否允許一個worker_process一次同時接收多個網絡連接 默認off 不允許
use epoll; //事件驅動模型 select,poll,kqueue,epoll,rtsig,有些模塊需手動安裝
worker_connections 1024; //最大連接數允許每個worker_process同時開啟的最大連接數(包括所有可能的連接數),該值不能大於操作系統支持打開的最大文件句柄數
}
http {
server_tokens off;//隱藏nginx的版本號,並不會讓nginx執行的速度更快,但它可以關閉在錯誤頁面中的nginx版本數字,這樣對於安全性是有好處的。
server_tag off;//隱藏nginx的信息
include mime.types; //前端請求的資源類型
default_type application/octet-stream; //處理前端請求默認的類型
charset utf-8;
# nginx log format.
log_format main '$http_host $remote_addr - $remote_user [$time_local] '
'$scheme "$request" $status $bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$gzip_ratio" $request_time '
'$upstream_addr $upstream_status $upstream_response_time'; //服務日志的格式
access_log /opt/log/nginx/localhost/access.log main; //服務日志路徑,格式選擇
error_log /opt/log/nginx/localhost/error.log error;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
autoindex on; #開啟目錄列表訪問,合適下載服務器,默認關閉。
sendfile on; //開啟sendfile高效傳輸文件的模式 默認關閉
sendfile_max_chunk 512k; //設置每個worker_process傳輸文件的最大值 默認0 無限制
tcp_nopush on;//tcp_nopush需要和sendfile一起使用,作用是:讓HTTP響應頭和sendfile()發送的數據混合為一個數據包發送出去。
tcp_nodelay on;//用於覆蓋Nagle's算法.必須開啟keepalive才能使用這個指令否則不能使用
keepalive_timeout 15s; //配置客戶端連接超時時間 默認75s
keepalive_requests 60; //建立某連接允許發送的請求次數 默認100
server_names_hash_max_size 512;//服務器名字的hash表大小
server_names_hash_bucket_size 512;#服務器名字的hash表大小和上個值保持一致
client_header_buffer_size 4k;#上傳文件大小限制
large_client_header_buffers 4 256k; #設定請求緩
client_header_timeout 10;//請求頭的超時時間
// allow 192.33.33.3; 設置允許訪問nginx的Ip 設置多個Ip使用多次allow all 代表所有 可配置在http ,server,location
//deny 192.33.33.3; 禁止訪問的IP
//auth_basic string|off ; 開啟密碼訪問nginx
//auth_basic_user_file file; 設置密碼訪問的密碼文件的絕對路徑
gzip on;//開啟壓縮功能,默認off
gzip_min_length 1k;//大於1k才壓縮
gzip_buffers 16 8k;//壓縮申請的空間個數和大小
gzip_comp_level 4;//壓縮等級 1-9 9代表壓縮程度最高
gzip_http_version 1.0;//1 以上版本使用壓縮,默認1.1
gzip_types text/plain application/x-javascript text/css application/xml text/javascript image/gif image/png;//根據類型壓縮
gzip_vary on;//告知本不支持gzip壓縮的瀏覽器,數據壓縮了
map_hash_max_size 102400;
map_hash_bucket_size 128;
fastcgi_intercept_errors on;//開啟錯誤頁面跳轉,默認關閉
server {
listen 80; //配置網絡監聽 可監聽IP,端口
#listen 443 ssl ;//配置https
server_name uums.58qf.com; 虛擬主機名稱,可多個 空格隔開 支持正則
#add_header Strict-Transport-Security "max-age=31536000; includeSubDomains"; #HSTS強制使用HTTPS
error_page 500 404 http://m.58qf.com/error.html; //設置錯誤頁面 /404.html
//error_page 404 /404.html;// 指安裝路徑下的/html/404.html
//下面的配置指根路徑下的/usr/html/404.html;
//location /404.html
//{
// root /usr/html/
//}
//
access_log /opt/log/nginx/uums.58qf.com/uums.**.com_access.log main;
error_log /opt/log/nginx/uums.58qf.com/uums.**.com_error.log error;
location / { //支持正則
proxy_next_upstream http_502 error timeout invalid_header;
proxy_set_header Host $host;//如果客戶端發過來的請求的header中有’HOST’這個字段時,$http_host和$host都是原始的’HOST’字段 ,沒有使用$host
poxry_set_header X-Forwarded-For $proxy_add_x_forwarded_for; //獲取HTTP的請求端真實的IP
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://test;
}
upstream test{
ip_hash;
server 172.16.11.120:9014 weight=1 max_fails=1 fail_timeout=10s;
server 172.16.11.120:9014 weight=2 max_fails=1 fail_timeout=10s;
}
