NGINX的配置:
1.全局變量:
#Nginx的worker進(jìn)程運(yùn)行用戶以及用戶組
user? nobody nobody;
#Nginx開(kāi)啟的進(jìn)程數(shù)韵洋,建議為CPU的核數(shù)
worker_processes? 1;
#worker_processes auto;
#以下參數(shù)指定了哪個(gè)cpu分配給哪個(gè)進(jìn)程僚楞,一般來(lái)說(shuō)不用特殊指定巫俺。如果一定要設(shè)的話,用0和1指定分配方式.
#這樣設(shè)就是給1-4個(gè)進(jìn)程分配單獨(dú)的核來(lái)運(yùn)行盆佣,出現(xiàn)第5個(gè)進(jìn)程是就是隨機(jī)分配了痕慢。eg:
worker_processes 4? ? #4核CPU
worker_cpu_affinity 0001 0010 0100 1000
#定義全局錯(cuò)誤日志定義類型瓷产,[debug|info|notice|warn|crit]
error_log? logs/error.log? info;
#指定進(jìn)程ID存儲(chǔ)文件位置
#pid? ? ? ? logs/nginx.pid;
#一個(gè)nginx進(jìn)程打開(kāi)的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開(kāi)文件數(shù)(ulimit -n)與nginx進(jìn)程數(shù)相除按摘,但是nginx分配請(qǐng)求并不是那么均勻包券,所以最好與ulimit -n的值保持一致。
#vim /etc/security/limits.conf
#? *? ? ? ? ? ? ? ? soft? ? nproc? ? ? ? ? 65535
#? *? ? ? ? ? ? ? ? hard? ? nproc? ? ? ? ? 65535
#? *? ? ? ? ? ? ? ? soft? ? nofile? ? ? ? 65535
#? *? ? ? ? ? ? ? ? hard? ? nofile? ? ? ? 65535
worker_rlimit_nofile 65535;
2.事件配置:
events {
? ? #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型炫贤,如果跑在FreeBSD上面溅固,就用kqueue模型。
? ? use epoll;
? ? #每個(gè)進(jìn)程可以處理的最大連接數(shù)兰珍,理論上每臺(tái)nginx服務(wù)器的最大連接數(shù)為worker_processes*worker_connections侍郭。理論值:worker_rlimit_nofile/worker_processes
? ? #注意:最大客戶數(shù)也由系統(tǒng)的可用socket連接數(shù)限制(~ 64K),所以設(shè)置不切實(shí)際的高沒(méi)什么好處
? ? worker_connections? 65535;? ?
? ? #worker工作方式:串行(一定程度降低負(fù)載俩垃,但服務(wù)器吞吐量大時(shí)励幼,關(guān)閉使用并行方式)
? ? #multi_accept on;
}
3.Http參數(shù):
#文件擴(kuò)展名與文件類型映射表
? ? include mime.types;
? ? #默認(rèn)文件類型
? ? default_type application/octet-stream;
#日志相關(guān)定義
? ? log_format? main? '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
? ? #定義日志的格式。后面定義要輸出的內(nèi)容口柳。
? ? #1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址苹粟;
? ? #2.$remote_user :用來(lái)記錄客戶端用戶名稱;
? ? #3.$time_local :用來(lái)記錄訪問(wèn)時(shí)間與時(shí)區(qū)跃闹;
? ? #4.$request? :用來(lái)記錄請(qǐng)求的url與http協(xié)議嵌削;
? ? #5.$status :用來(lái)記錄請(qǐng)求狀態(tài);
? ? #6.$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大型铡苛秕;
? ? #7.$http_referer :用來(lái)記錄從那個(gè)頁(yè)面鏈接訪問(wèn)過(guò)來(lái)的;
? ? #8.$http_user_agent :記錄客戶端瀏覽器的相關(guān)信息
? ? #連接日志的路徑找默,指定的日志格式放在最后艇劫。
? ? #access_log? logs/access.log? main;
? ? #只記錄更為嚴(yán)重的錯(cuò)誤日志,減少I(mǎi)O壓力
? ? error_log logs/error.log crit;
? ? #關(guān)閉日志
? ? #access_log? off;
? ? #默認(rèn)編碼
? ? #charset utf-8;
? ? #服務(wù)器名字的hash表大小
? ? server_names_hash_bucket_size 128;
? ? #客戶端請(qǐng)求單個(gè)文件的最大字節(jié)數(shù)
? ? client_max_body_size 8m;
? ? #指定來(lái)自客戶端請(qǐng)求頭的hearerbuffer大小
? ? client_header_buffer_size 32k;
? ? #指定客戶端請(qǐng)求中較大的消息頭的緩存最大數(shù)量和大小惩激。
? ? large_client_header_buffers 4 64k;
? ? #開(kāi)啟高效傳輸模式店煞。
? ? sendfile? ? ? ? on;
? ? #防止網(wǎng)絡(luò)阻塞
? ? tcp_nopush on;
? ? tcp_nodelay on;? ?
? ? #客戶端連接超時(shí)時(shí)間蟹演,單位是秒
? ? keepalive_timeout 60;
? ? #客戶端請(qǐng)求頭讀取超時(shí)時(shí)間
? ? client_header_timeout 10;
? ? #設(shè)置客戶端請(qǐng)求主體讀取超時(shí)時(shí)間
? ? client_body_timeout 10;
? ? #響應(yīng)客戶端超時(shí)時(shí)間
? ? send_timeout 10;
#FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問(wèn)速度顷蟀。
? ? fastcgi_connect_timeout 300;
? ? fastcgi_send_timeout 300;
? ? fastcgi_read_timeout 300;
? ? fastcgi_buffer_size 64k;
? ? fastcgi_buffers 4 64k;
? ? fastcgi_busy_buffers_size 128k;
? ? fastcgi_temp_file_write_size 128k;
#gzip模塊設(shè)置
? ? #開(kāi)啟gzip壓縮輸出
? ? gzip on;
? ? #最小壓縮文件大小
? ? gzip_min_length 1k;
? ? #壓縮緩沖區(qū)
? ? gzip_buffers 4 16k;
? ? #壓縮版本(默認(rèn)1.1酒请,前端如果是squid2.5請(qǐng)使用1.0)
? ? gzip_http_version 1.0;
? ? #壓縮等級(jí) 1-9 等級(jí)越高,壓縮效果越好鸣个,節(jié)約寬帶羞反,但CPU消耗大
? ? gzip_comp_level 2;
? ? #壓縮類型,默認(rèn)就已經(jīng)包含text/html囤萤,所以下面就不用再寫(xiě)了昼窗,寫(xiě)上去也不會(huì)有問(wèn)題,但是會(huì)有一個(gè)warn涛舍。
? ? gzip_types text/plain application/x-javascript text/css application/xml;
? ? #前端緩存服務(wù)器緩存經(jīng)過(guò)壓縮的頁(yè)面
? ? gzip_vary on;
4.虛擬主機(jī)基本配置
#虛擬主機(jī)定義
? ? server {
? ? ? ? #監(jiān)聽(tīng)端口
? ? ? ? listen? ? ? 80;
? ? ? ? #訪問(wèn)域名
? ? ? ? server_name? localhost;
? ? ? ? #編碼格式膏秫,若網(wǎng)頁(yè)格式與此不同,將被自動(dòng)轉(zhuǎn)碼
? ? ? ? #charset koi8-r;
? ? ? ? #虛擬主機(jī)訪問(wèn)日志定義
? ? ? ? #access_log? logs/host.access.log? main;
? ? ? ? #對(duì)URL進(jìn)行匹配
? ? ? ? location / {
? ? ? ? ? ? #訪問(wèn)路徑做盅,可相對(duì)也可絕對(duì)路徑
? ? ? ? ? ? root? html;
? ? ? ? ? ? #首頁(yè)文件缤削。以下按順序匹配
? ? ? ? ? ? index? index.html index.htm;
? ? ? ? }
#錯(cuò)誤信息返回頁(yè)面
? ? ? ? #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? html;
? ? ? ? }
#訪問(wèn)URL以.php結(jié)尾則自動(dòng)轉(zhuǎn)交給127.0.0.1
? ? ? ? # proxy the PHP scripts to Apache listening on 127.0.0.1:80
? ? ? ? #
? ? ? ? #location ~ \.php$ {
? ? ? ? #? ? proxy_pass? http://127.0.0.1;
? ? ? ? #}
#php腳本請(qǐng)求全部轉(zhuǎn)發(fā)給FastCGI處理
? ? ? ? # 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;
? ? ? ? #}
#禁止訪問(wèn).ht頁(yè)面 (需ngx_http_access_module模塊)
? ? ? ? # deny access to .htaccess files, if Apache's document root
? ? ? ? # concurs with nginx's one
? ? ? ? #
? ? ? ? #location ~ /\.ht {
? ? ? ? #? ? deny? all;
? ? ? ? #}
? ? }
#HTTPS虛擬主機(jī)定義
? ? # HTTPS server
? ? #
? ? #server {
? ? #? ? listen? ? ? 443 ssl;
? ? #? ? server_name? localhost;
? ? #? ? ssl_certificate? ? ? cert.pem;
? ? #? ? ssl_certificate_key? cert.key;
? ? #? ? ssl_session_cache? ? shared:SSL:1m;
? ? #? ? ssl_session_timeout? 5m;
? ? #? ? ssl_ciphers? HIGH:!aNULL:!MD5;
? ? #? ? ssl_prefer_server_ciphers? on;
? ? #? ? location / {
? ? #? ? ? ? root? html;
? ? #? ? ? ? index? index.html index.htm;
? ? #? ? }
? ? #}
5.NGINX狀態(tài)監(jiān)控
#Nginx運(yùn)行狀態(tài),StubStatus模塊獲取Nginx自啟動(dòng)的工作狀態(tài)(編譯時(shí)要開(kāi)啟對(duì)應(yīng)功能)
? ? ? ? #location /NginxStatus {
? ? ? ? #? ? #啟用StubStatus的工作訪問(wèn)狀態(tài)? ?
? ? ? ? #? ? stub_status? ? on;
? ? ? ? #? ? #指定StubStaus模塊的訪問(wèn)日志文件
? ? ? ? #? ? access_log? ? logs/Nginxstatus.log;
? ? ? ? #? ? #Nginx認(rèn)證機(jī)制(需Apache的htpasswd命令生成)
? ? ? ? #? ? #auth_basic? ? "NginxStatus";
? ? ? ? #? ? #用來(lái)認(rèn)證的密碼文件
? ? ? ? #? ? #auth_basic_user_file? ? ../htpasswd;? ?
? ? ? ? #}
訪問(wèn):http://IP/NginxStatus(測(cè)試就不加密碼驗(yàn)證相關(guān))
6.反向代理
#以下配置追加在HTTP的全局變量中
#nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_connect_timeout? ? ? 5;
#后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí))
proxy_send_timeout? ? ? ? 5;
#連接成功后吹榴,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))
proxy_read_timeout? ? ? ? 60;
#設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffer_size? ? ? ? ? 16k;
#proxy_buffers緩沖區(qū)亭敢,網(wǎng)頁(yè)平均在32k以下的話,這樣設(shè)置
proxy_buffers? ? ? ? ? ? ? 4 32k;
#高負(fù)荷下緩沖大型汲铩(proxy_buffers*2)
proxy_busy_buffers_size? ? 64k;
#設(shè)定緩存文件夾大小帅刀,大于這個(gè)值,將從upstream服務(wù)器傳
proxy_temp_file_write_size 64k;
#反向代理緩存目錄
proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
#levels=1:2 設(shè)置目錄深度远剩,第一層目錄是1個(gè)字符扣溺,第2層是2個(gè)字符
#keys_zone:設(shè)置web緩存名稱和內(nèi)存緩存空間大小
#inactive:自動(dòng)清除緩存文件時(shí)間。
#max_size:硬盤(pán)空間最大可使用值瓜晤。
#指定臨時(shí)緩存文件的存儲(chǔ)路徑(路徑需和上面路徑在同一分區(qū))
proxy_temp_path /data/proxy/temp
#服務(wù)配置
server {
? ? #偵聽(tīng)的80端口
? ? listen? ? ? 80;
? ? server_name? localhost;
? ? location / {
? ? ? ? #反向代理緩存設(shè)置命令(proxy_cache zone|off,默認(rèn)關(guān)閉所以要設(shè)置)
? ? ? ? proxy_cache cache_one;
? ? ? ? #對(duì)不同的狀態(tài)碼緩存不同時(shí)間
? ? ? ? proxy_cache_valid 200 304 12h;
? ? ? ? #設(shè)置以什么樣參數(shù)獲取緩存文件名
? ? ? ? proxy_cache_key $host$uri$is_args$args;
? ? ? ? #后7端的Web服務(wù)器可以通過(guò)X-Forwarded-For獲取用戶真實(shí)IP
? ? ? ? proxy_set_header Host $host;
? ? ? ? proxy_set_header X-Real-IP $remote_addr;
? ? ? ? proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;?
? ? ? ? #代理設(shè)置
? ? ? ? proxy_pass? http://IP;
? ? ? ? #文件過(guò)期時(shí)間控制
? ? ? ? expires? ? 1d;
? ? }
? ? #配置手動(dòng)清楚緩存(實(shí)現(xiàn)此功能需第三方模塊 ngx_cache_purge)
? ? #http://www.123.com/2017/0316/17.html訪問(wèn)
? ? #http://www.123.com/purge/2017/0316/17.html清楚URL緩存
? ? location ~ /purge(/.*) {
? ? ? ? allow? ? 127.0.0.1;
? ? ? ? deny? ? all;
? ? ? ? proxy_cache_purge? ? cache_one? ? $host$1$is_args$args;
? ? }
? ? #設(shè)置擴(kuò)展名以.jsp锥余、.php、.jspx結(jié)尾的動(dòng)態(tài)應(yīng)用程序不做緩存
? ? location ~.*\.(jsp|php|jspx)?$ {
? ? ? ? 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_pass http://http://IP;
? ? }
7.負(fù)載均衡
#負(fù)載均衡服務(wù)器池
upstream my_server_pool {
? ? #調(diào)度算法
? ? #1.輪循(默認(rèn))(weight輪循權(quán)值)
? ? #2.ip_hash:根據(jù)每個(gè)請(qǐng)求訪問(wèn)IP的hash結(jié)果分配痢掠。(會(huì)話保持)
? ? #3.fair:根據(jù)后端服務(wù)器響應(yīng)時(shí)間最短請(qǐng)求驱犹。(upstream_fair模塊)
? ? #4.url_hash:根據(jù)訪問(wèn)的url的hash結(jié)果分配。(需hash軟件包)
? ? #參數(shù):
? ? #down:表示不參與負(fù)載均衡
? ? #backup:備份服務(wù)器
? ? #max_fails:允許最大請(qǐng)求錯(cuò)誤次數(shù)
? ? #fail_timeout:請(qǐng)求失敗后暫停服務(wù)時(shí)間足画。
? ? server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
? ? server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
}
#負(fù)載均衡調(diào)用
server {
? ? ...
? ? location / {
? ? proxy_pass http://my_server_pool;
? ? }
}
8.URL重寫(xiě)
#根據(jù)不同的瀏覽器URL重寫(xiě)
? if($http_user_agent ~ Firefox){
? rewrite ^(.*)$? /firefox/$1 break;
? }
? if($http_user_agent ~ MSIE){
? rewrite ^(.*)$? /msie/$1 break;
? }
? #實(shí)現(xiàn)域名跳轉(zhuǎn)
? location / {
? rewrite ^/(.*)$ https://web8.example.com$1 permanent;
? }
9.IP限制
#限制IP訪問(wèn)
location / {
? ? deny 192.168.0.2雄驹;
? ? allow 192.168.0.0/24;
? ? allow 192.168.1.1;
? ? deny all;
}