
【Nginx 安裝與配置教程】
Nginx 安裝教程:
1.、下載nginx安裝包。下載后進(jìn)行解壓,將解壓后的文件放到自己心儀的目錄下,我的解壓文件放在了d盤根目錄下,如下圖所示:
2、進(jìn)入window的cmd窗口,輸入如下圖所示的命令,進(jìn)入到nginx目錄,使用“start nginx.exe ”進(jìn)行nginx的安裝,如下圖所示:
3、安裝成功后,在“任務(wù)管理器”中會(huì)看到“nginx.exe”進(jìn)程,如下圖所示:
4、在瀏覽器地址欄輸入:127.0.0.1,會(huì)看到如下圖所示的nginx歡迎界面
相應(yīng)的命令:
nginx.exe -s stop //停止nginx
nginx.exe -s reload //重新加載nginx
nginx.exe -s quit //退出nginx
注:以上的命令中,.exe可以去掉
相應(yīng)命令的操作如下圖所示:
如果nginx還是啟動(dòng)失敗需要查看nginx日志文件logs/error.log,如果該文件不存在需要查看Windows事件日志中。
同時(shí)需要注意的是
1.windows下nginx配置文件中的目錄請使用“/”,而不是“”做目錄分隔
2.windows下的nginx只有一個(gè)有效的工作進(jìn)程
3.windows vista以后系統(tǒng)的不支持nginx的cache模塊和需要共享內(nèi)存支持的模塊。
4.widows下的nginx支持最大1024個(gè)并發(fā)連接
Nginx 配置教程:
大多數(shù)的Nginx安裝指南告訴你如下基礎(chǔ)知識(shí)——通過apt-get安裝,修改這里或那里的幾行配置,好了,你已經(jīng)有了一個(gè)Web服務(wù)器了!而且,在大多數(shù)情況下,一個(gè)常規(guī)安裝的nginx對你的網(wǎng)站來說已經(jīng)能很好地工作了。然而,如果你真的想擠壓出nginx的性能,你必須更深入一些。在本指南中,我將解釋Nginx的那些設(shè)置可以微調(diào),以優(yōu)化處理大量客戶端時(shí)的性能。需要注意一點(diǎn),這不是一個(gè)全面的微調(diào)指南。這是一個(gè)簡單的預(yù)覽——那些可以通過微調(diào)來提高性能設(shè)置的概述。你的情況可能不同。
基本的 (優(yōu)化過的)配置
我們將修改的唯一文件是nginx.conf,其中包含Nginx不同模塊的所有設(shè)置。你應(yīng)該能夠在服務(wù)器的/etc/nginx目錄中找到nginx.conf。首先,我們將談?wù)撘恍┤衷O(shè)置,然后按文件中的模塊挨個(gè)來,談一下哪些設(shè)置能夠讓你在大量客戶端訪問時(shí)擁有良好的性能,為什么它們會(huì)提高性能。本文的結(jié)尾有一個(gè)完
整的配置文件。
高層的配置
nginx.conf文件中,Nginx中有少數(shù)的幾個(gè)高級(jí)配置在模塊部分之上。
user www-data;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;
user和pid應(yīng)該按默認(rèn)設(shè)置 – 我們不會(huì)更改這些內(nèi)容,因?yàn)楦呐c否沒有什么不同。
worker_processes 定義了nginx對外提供web服務(wù)時(shí)的worder進(jìn)程數(shù)。最優(yōu)值取決于許多因素,包括(但不限于)CPU核的數(shù)量、存儲(chǔ)數(shù)據(jù)的硬盤數(shù)量及負(fù)載模式。不能確定的時(shí)候,將其設(shè)置為可用的CPU內(nèi)核數(shù)將是一個(gè)好的開始(設(shè)置為“auto”將嘗試自動(dòng)檢測它)。
worker_rlimit_nofile 更改worker進(jìn)程的最大打開文件數(shù)限制。如果沒設(shè)置的話,這個(gè)值為操作系統(tǒng)的限制。設(shè)置后你的操作系統(tǒng)和Nginx可以處理比“ulimit -a”更多的文件,所以把這個(gè)值設(shè)高,這樣nginx就不會(huì)有“too many open files”問題了。
Events模塊
events模塊中包含nginx中所有處理連接的設(shè)置。
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
worker_connections設(shè)置可由一個(gè)worker進(jìn)程同時(shí)打開的最大連接數(shù)。如果設(shè)置了上面提到的worker_rlimit_nofile,我們可以將這個(gè)值設(shè)得很高。
記住,最大客戶數(shù)也由系統(tǒng)的可用socket連接數(shù)限制(~ 64K),所以設(shè)置不切實(shí)際的高沒什么好處。
multi_accept 告訴nginx收到一個(gè)新連接通知后接受盡可能多的連接。
use 設(shè)置用于復(fù)用客戶端線程的輪詢方法。如果你使用Linux 2.6+,你應(yīng)該使用epoll。如果你使用*BSD,你應(yīng)該使用kqueue。想知道更多有關(guān)事件輪詢?看下維基百科吧(注意,想了解一切的話可能需要neckbeard和操作系統(tǒng)的課程基礎(chǔ))
(值得注意的是如果你不知道Nginx該使用哪種輪詢方法的話,它會(huì)選擇一個(gè)最適合你操作系統(tǒng)的)。
HTTP 模塊
HTTP模塊控制著nginx http處理的所有核心特性。因?yàn)檫@里只有很少的配置,所以我們只節(jié)選配置的一小部分。所有這些設(shè)置都應(yīng)該在http模塊中,甚至你不會(huì)特別的注意到這段設(shè)置。
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
}
server_tokens 并不會(huì)讓nginx執(zhí)行的速度更快,但它可以關(guān)閉在錯(cuò)誤頁面中的nginx版本數(shù)字,這樣對于安全性是有好處的。
sendfile可以讓sendfile()發(fā)揮作用。sendfile()可以在磁盤和TCP socket之間互相拷貝數(shù)據(jù)(或任意兩個(gè)文件描述符)。Pre-sendfile是傳送數(shù)據(jù)之前在用戶空間申請數(shù)據(jù)緩沖區(qū)。之后用read()將數(shù)據(jù)從文件拷貝到這個(gè)緩沖區(qū),write()將緩沖區(qū)數(shù)據(jù)寫入網(wǎng)絡(luò)。sendfile()是立即將數(shù)據(jù)從磁盤讀到OS緩存。因?yàn)檫@種拷貝是在內(nèi)核完成的,sendfile()要比組合read()和write()以及打開關(guān)閉丟棄緩沖更加有效(更多有關(guān)于sendfile)
tcp_nopush 告訴nginx在一個(gè)數(shù)據(jù)包里發(fā)送所有頭文件,而不一個(gè)接一個(gè)的發(fā)送
tcp_nodelay 告訴nginx不要緩存數(shù)據(jù),而是一段一段的發(fā)送–當(dāng)需要及時(shí)發(fā)送數(shù)據(jù)時(shí),就應(yīng)該給應(yīng)用設(shè)置這個(gè)屬性,這樣發(fā)送一小塊數(shù)據(jù)信息時(shí)就不能立即得到返回值。
access_log off;
error_log /var/log/nginx/error.log crit;
access_log設(shè)置nginx是否將存儲(chǔ)訪問日志。關(guān)閉這個(gè)選項(xiàng)可以讓讀取磁盤IO操作更快(aka,YOLO)。
error_log 告訴nginx只能記錄嚴(yán)重的錯(cuò)誤。
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
keepalive_timeout 給客戶端分配keep-alive鏈接超時(shí)時(shí)間。服務(wù)器將在這個(gè)超時(shí)時(shí)間過后關(guān)閉鏈接。我們將它設(shè)置低些可以讓ngnix持續(xù)工作的時(shí)間更長。
client_header_timeout 和client_body_timeout 設(shè)置請求頭和請求體(各自)的超時(shí)時(shí)間。我們也可以把這個(gè)設(shè)置低些。
reset_timeout_connection告訴nginx關(guān)閉不響應(yīng)的客戶端連接。這將會(huì)釋放那個(gè)客戶端所占有的內(nèi)存空間。
send_timeout 指定客戶端的響應(yīng)超時(shí)時(shí)間。這個(gè)設(shè)置不會(huì)用于整個(gè)轉(zhuǎn)發(fā)器,而是在兩次客戶端讀取操作之間。如果在這段時(shí)間內(nèi),客戶端沒有讀取任何數(shù)據(jù),nginx就會(huì)關(guān)閉連接。
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
limit_conn為給定的key設(shè)置最大連接數(shù)。這里key是addr,我們設(shè)置的值是100,也就是說我們允許每一個(gè)IP地址最多同時(shí)打開有100個(gè)連接。
limit_conn_zone設(shè)置用于保存各種key(比如當(dāng)前連接數(shù))的共享內(nèi)存的參數(shù)。5m就是5兆字節(jié),這個(gè)值應(yīng)該被設(shè)置的足夠大以存儲(chǔ)(32K*5)32byte狀態(tài)或者(16K*5)64byte狀態(tài)。
include /etc/nginx/mime.types;
default_type text/html;
charset UTF-8;
include只是一個(gè)在當(dāng)前文件中包含另一個(gè)文件內(nèi)容的指令。這里我們使用它來加載稍后會(huì)用到的一系列的MIME類型。
default_type設(shè)置文件使用的默認(rèn)的MIME-type。
charset設(shè)置我們的頭文件中的默認(rèn)的字符集。
以下兩點(diǎn)對于性能的提升在偉大的WebMasters StackExchange中有解釋。
gzip_disable "msie6";
# gzip_static on;
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip是告訴nginx采用gzip壓縮的形式發(fā)送數(shù)據(jù)。這將會(huì)減少我們發(fā)送的數(shù)據(jù)量。
gzip_disable為指定的客戶端禁用gzip功能。我們設(shè)置成IE6或者更低版本以使我們的方案能夠廣泛兼容。
gzip_static告訴nginx在壓縮資源之前,先查找是否有預(yù)先gzip處理過的資源。這要求你預(yù)先壓縮你的文件(在這個(gè)例子中被注釋掉了),從而允許你使用最高壓縮比,這樣nginx就不用再壓縮這些文件了(想要更詳盡的gzip_static的信息,請點(diǎn)擊這里)。
gzip_proxied允許或者禁止壓縮基于請求和響應(yīng)的響應(yīng)流。我們設(shè)置為any,意味著將會(huì)壓縮所有的請求。
gzip_min_length設(shè)置對數(shù)據(jù)啟用壓縮的最少字節(jié)數(shù)。如果一個(gè)請求小于1000字節(jié),我們最好不要壓縮它,因?yàn)閴嚎s這些小的數(shù)據(jù)會(huì)降低處理此請求的所有進(jìn)程的速度。
gzip_comp_level設(shè)置數(shù)據(jù)的壓縮等級(jí)。這個(gè)等級(jí)可以是1-9之間的任意數(shù)值,9是最慢但是壓縮比最大的。我們設(shè)置為4,這是一個(gè)比較折中的設(shè)置。
gzip_type設(shè)置需要壓縮的數(shù)據(jù)格式。上面例子中已經(jīng)有一些了,你也可以再添加更多的格式。
# cache informations about file descriptors, frequently accessed files
# can boost performance, but you need to test those values
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
##
# Virtual Host Configs
# aka our settings for specific servers
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
open_file_cache打開緩存的同時(shí)也指定了緩存最大數(shù)目,以及緩存的時(shí)間。我們可以設(shè)置一個(gè)相對高的最大時(shí)間,這樣我們可以在它們不活動(dòng)超過20秒后清除掉。
open_file_cache_valid 在open_file_cache中指定檢測正確信息的間隔時(shí)間。
open_file_cache_min_uses 定義了open_file_cache中指令參數(shù)不活動(dòng)時(shí)間期間里最小的文件數(shù)。
open_file_cache_errors指定了當(dāng)搜索一個(gè)文件時(shí)是否緩存錯(cuò)誤信息,也包括再次給配置中添加文件。我們也包括了服務(wù)器模塊,這些是在不同文件中定義的。如果你的服務(wù)器模塊不在這些位置,你就得修改這一行來指定正確的位置。
一個(gè)完整的配置:
user www-data;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 100000;
events {
worker_connections 2048;
multi_accept on;
use epoll;
}
http {
server_tokens off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
access_log off;
error_log /var/log/nginx/error.log crit;
keepalive_timeout 10;
client_header_timeout 10;
client_body_timeout 10;
reset_timedout_connection on;
send_timeout 10;
limit_conn_zone $binary_remote_addr zone=addr:5m;
limit_conn addr 100;
include /etc/nginx/mime.types;
default_type text/html;
charset UTF-8;
gzip on;
gzip_disable "msie6";
gzip_proxied any;
gzip_min_length 1000;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
open_file_cache max=100000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
編輯完配置后,確認(rèn)重啟nginx使設(shè)置生效。
sudo service nginx restart
就這樣!你的Web服務(wù)器現(xiàn)在已經(jīng)就緒,之前困擾你的眾多訪問者的問題來吧。這并不是加速網(wǎng)站的唯一途徑,很快我會(huì)寫更多介紹其他加速網(wǎng)站方法的文章的。
【說明】
#定義Nginx運(yùn)行的用戶和用戶組
user www www;
#nginx進(jìn)程數(shù),建議設(shè)置為等于CPU總核心數(shù)。
worker_processes 8;
#全局錯(cuò)誤日志定義類型,[ debug | info | notice | warn | error | crit ]
error_log /var/log/nginx/error.log info;
#進(jìn)程文件
pid /var/run/nginx.pid;
#一個(gè)nginx進(jìn)程打開的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開文件數(shù)(系統(tǒng)的值ulimit -n)與nginx進(jìn)程數(shù)相除,但是nginx分配請求并不均勻,所以建議與ulimit -n的值保持一致。
worker_rlimit_nofile 65535;
#工作模式與連接數(shù)上限
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ù)(最大連接數(shù)=連接數(shù)*進(jìn)程數(shù))
worker_connections 65535;
}
#設(shè)定http服務(wù)器
http
{
include mime.types; #文件擴(kuò)展名與文件類型映射表
default_type application/octet-stream; #默認(rèn)文件類型
#charset utf-8; #默認(rèn)編碼
server_names_hash_bucket_size 128; #服務(wù)器名字的hash表大小
client_header_buffer_size 32k; #上傳文件大小限制
large_client_header_buffers 4 64k; #設(shè)定請求緩
client_max_body_size 8m; #設(shè)定請求緩
sendfile on; #開啟高效文件傳輸模式,sendfile指令指定nginx是否調(diào)用sendfile函數(shù)來輸出文件,對于普通應(yīng)用設(shè)為 on,如果用來進(jìn)行下載等應(yīng)用磁盤IO重負(fù)載應(yīng)用,可設(shè)置為off,以平衡磁盤與網(wǎng)絡(luò)I/O處理速度,降低系統(tǒng)的負(fù)載。注意:如果圖片顯示不正常把這個(gè)改成off。
autoindex on; #開啟目錄列表訪問,合適下載服務(wù)器,默認(rèn)關(guān)閉。
tcp_nopush on; #防止網(wǎng)絡(luò)阻塞
tcp_nodelay on; #防止網(wǎng)絡(luò)阻塞
keepalive_timeout 120; #長連接超時(shí)時(shí)間,單位是秒
#FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問速度。下面參數(shù)看字面意思都能理解。
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è)置
gzip on; #開啟gzip壓縮輸出
gzip_min_length 1k; #最小壓縮文件大小
gzip_buffers 4 16k; #壓縮緩沖區(qū)
gzip_http_version 1.0; #壓縮版本(默認(rèn)1.1,前端如果是squid2.5請使用1.0)
gzip_comp_level 2; #壓縮等級(jí)
gzip_types text/plain application/x-javascript text/css application/xml;
#壓縮類型,默認(rèn)就已經(jīng)包含text/html,所以下面就不用再寫了,寫上去也不會(huì)有問題,但是會(huì)有一個(gè)warn。
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m; #開啟限制IP連接數(shù)的時(shí)候需要使用
upstream blog.ha97.com {
#upstream的負(fù)載均衡,weight是權(quán)重,可以根據(jù)機(jī)器配置定義權(quán)重。weigth參數(shù)表示權(quán)值,權(quán)值越高被分配到的幾率越大。
server 192.168.80.121:80 weight=3;
server 192.168.80.122:80 weight=2;
server 192.168.80.123:80 weight=3;
}
#虛擬主機(jī)的配置
server
{
#監(jiān)聽端口
listen 80;
#域名可以有多個(gè),用空格隔開
server_name www.ha97.com ha97.com;
index index.html index.htm index.php;
root /data/www/ha97;
location ~ .*\.(php|php5)?$
{
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
#圖片緩存時(shí)間設(shè)置
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 10d;
}
#JS和CSS緩存時(shí)間設(shè)置
location ~ .*\.(js|css)?$
{
expires 1h;
}
#日志格式設(shè)定
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#定義本虛擬主機(jī)的訪問日志
access_log /var/log/nginx/ha97access.log access;
#對 "/" 啟用反向代理
location / {
proxy_pass http://127.0.0.1:88;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
#后端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實(shí)IP
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#以下是一些反向代理的配置,可選。
proxy_set_header Host $host;
client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù)
client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù),
proxy_connect_timeout 90; #nginx跟后端服務(wù)器連接超時(shí)時(shí)間(代理連接超時(shí))
proxy_send_timeout 90; #后端服務(wù)器數(shù)據(jù)回傳時(shí)間(代理發(fā)送超時(shí))
proxy_read_timeout 90; #連接成功后,后端服務(wù)器響應(yīng)時(shí)間(代理接收超時(shí))
proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的設(shè)置
proxy_busy_buffers_size 64k; #高負(fù)荷下緩沖大小(proxy_buffers*2)
proxy_temp_file_write_size 64k;
#設(shè)定緩存文件夾大小,大于這個(gè)值,將從upstream服務(wù)器傳
}
#設(shè)定查看Nginx狀態(tài)的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
auth_basic_user_file conf/htpasswd;
#htpasswd文件的內(nèi)容可以用apache提供的htpasswd工具來產(chǎn)生。
}
#本地動(dòng)靜分離反向代理配置
#所有jsp的頁面均交由tomcat或resin處理
location ~ .(jsp|jspx|do)?$ {
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://127.0.0.1:8080;
}
#所有靜態(tài)文件由nginx直接讀取不經(jīng)過tomcat或resin
location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$
{ expires 15d; }
location ~ .*.(js|css)?$
{ expires 1h; }
}
}
Nginx 配置其他說明:
一般我們都需要先裝pcre, zlib,前者為了重寫rewrite,后者為了gzip壓縮。
1.選定源碼目錄
選定目錄 /usr/local/
cd /usr/local/
2.安裝PCRE庫
cd /usr/local/
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.21.tar.gz
tar -zxvf pcre-8.21.tar.gz
cd pcre-8.21
./configure
make
make install
3.安裝zlib庫
cd /usr/local/
wget http://zlib.net/zlib-1.2.8.tar.gz
tar -zxvf zlib-1.2.8.tar.gz cd zlib-1.2.8
./configure
make
make install
4.安裝ssl
cd /usr/local/
wget http://www.openssl.org/source/openssl-1.0.1c.tar.gz
tar -zxvf openssl-1.0.1c.tar.gz
./config
make
make install
5.安裝nginx
Nginx 一般有兩個(gè)版本,分別是穩(wěn)定版和開發(fā)版,您可以根據(jù)您的目的來選擇這兩個(gè)版本的其中一個(gè),下面是把 Nginx 安裝到 /usr/local/nginx 目錄下的詳細(xì)步驟:
cd /usr/local/
wget http://nginx.org/download/nginx-1.2.8.tar.gz
tar -zxvf nginx-1.2.8.tar.gz
cd nginx-1.2.8
./configure --prefix=/usr/local/nginx
make
make install
--with-pcre=/usr/src/pcre-8.21 指的是pcre-8.21 的源碼路徑。
--with-zlib=/usr/src/zlib-1.2.7 指的是zlib-1.2.7 的源碼路徑。
6.啟動(dòng)
確保系統(tǒng)的 80 端口沒被其他程序占用,
/usr/local/nginx/sbin/nginx
檢查是否啟動(dòng)成功:
netstat -ano|grep 80 有結(jié)果輸入說明啟動(dòng)成功
打開瀏覽器訪問此機(jī)器的 IP,如果瀏覽器出現(xiàn) Welcome to nginx! 則表示 Nginx 已經(jīng)安裝并運(yùn)行成功。
7.重啟
/usr/local/nginx/sbin/nginx –s reload
8.修改配置文件
cd /usr/local/nginx/conf
vi nginx.conf
9.常用配置
#nginx運(yùn)行用戶和組
user www www;
#啟動(dòng)進(jìn)程,通常設(shè)置成和cpu的數(shù)量相等
worker_processes 4;
#全局錯(cuò)誤日志及PID文件
pid /var/run/nginx.pid;
error_log /var/log/nginx/error.log;
events {
#epoll是多路復(fù)用IO(I/O Multiplexing)中的一種方式,但是僅用于linux2.6以上內(nèi)核,可以大大提高nginx的性能
use epoll;
#單個(gè)后臺(tái)worker process進(jìn)程的最大并發(fā)鏈接數(shù)
worker_connections 10240;
}
#設(shè)定http服務(wù)器,利用它的反向代理功能提供負(fù)載均衡支持
http {
include mime.types;
default_type application/octet-stream;
error_page 400 403 500 502 503 504 /50x.html;
index index.html index.shtml
autoindex off;
fastcgi_intercept_errors on;
sendfile on;
# These are good default values.
tcp_nopush on;
tcp_nodelay off;
# output compression saves bandwidth
gzip off;
#gzip_static on;
#gzip_min_length 1k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_buffers 4 16k;
gzip_proxied any;
gzip_disable "MSIE [1-6]\.";
gzip_types text/plain text/html text/css application/x-javascript application/xml application/xml+rss text/javascript;
#gzip_vary on;
server_name_in_redirect off;
#設(shè)定負(fù)載均衡的服務(wù)器列表
upstream portals {
server 172.16.68.134:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.135:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.136:8082 max_fails=2 fail_timeout=30s;
server 172.16.68.137:8082 max_fails=2 fail_timeout=30s;
}
#upstream overflow {
# server 10.248.6.34:8090 max_fails=2 fail_timeout=30s;
# server 10.248.6.45:8080 max_fails=2 fail_timeout=30s;
#}
server {
#偵聽8080端口
listen 8080;
server_name 127.0.0.1;
#403、404頁面重定向地址
error_page 403 = http://www.e100.cn/ebiz/other/217/403.html;
error_page 404 = http://www.e100.cn/ebiz/other/218/404.html;
proxy_connect_timeout 90;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_buffer_size 64k;
proxy_buffers 4 128k;
proxy_busy_buffers_size 128k;
client_header_buffer_size 16k;
large_client_header_buffers 4 64k;
#proxy_send_timeout 3m;
#proxy_read_timeout 3m;
#proxy_buffer_size 4k;
#proxy_buffers 4 32k;
proxy_set_header Host $http_host;
proxy_max_temp_file_size 0;
#proxy_hide_header Set-Cookie;
# if ($host != 'www.e100.cn' ) {
# rewrite ^/(.*)$ http://www.e100.cn/$1 permanent;
# }
location / {
deny all;
}
location ~ ^/resource/res/img/blue/space.gif {
proxy_pass http://tecopera;
}
location = / {
rewrite ^(.*)$ /ebiz/event/517.html last;
}
location = /ebiz/event/517.html {
add_header Vary Accept-Encoding;
root /data/web/html;
expires 10m;
}
location = /check.html {
root /usr/local/nginx/html/;
access_log off;
}
location = /50x.html {
root /usr/local/nginx/html/;
expires 1m;
access_log off;
}
location = /index.html {
add_header Vary Accept-Encoding;
#定義服務(wù)器的默認(rèn)網(wǎng)站根目錄位置
root /data/web/html/ebiz;
expires 10m;
}
#定義反向代理訪問名稱
location ~ ^/ecps-portal/* {
# expires 10m;
#重定向集群名稱
proxy_pass http://portals;
#proxy_pass http://172.16.68.134:8082;
}
location ~ ^/fetionLogin/* {
# expires 10m;
proxy_pass http://portals;
#proxy_pass http://172.16.68.134:8082;
}
#location ~ ^/business/* {
# # expires 10m;
# proxy_pass http://172.16.68.132:8088;
# #proxy_pass http://172.16.68.134:8082;
#}
location ~ ^/rsmanager/* {
expires 10m;
root /data/web/;
#proxy_pass http://rsm;
}
#定義nginx處理的頁面后綴
location ~* (.*)\.(jpg|gif|htm|html|png|js|css)$ {
root /data/web/html/;
#頁面緩存時(shí)間為10分鐘
expires 10m;
}
#設(shè)定查看Nginx狀態(tài)的地址
location ~* ^/NginxStatus/ {
stub_status on;
access_log off;
allow 10.1.252.126;
allow 10.248.6.49;
allow 127.0.0.1;
deny all;
}
# error_page 405 =200 @405;
# location @405
# {
# proxy_pass http://10.248.6.45:8080;
# }
access_log /data/logs/nginx/access.log combined;
error_log /data/logs/nginx/error.log;
}
server {
listen 8082;
server_name _;
location = /check.html {
root /usr/local/nginx/html/;
access_log off;
}
}
server {
listen 8088;
server_name _;
location ~ ^/* {
root /data/web/b2bhtml/;
access_log off;
}
}
server {
listen 9082;
server_name _;
# location ~ ^/resource/* {
# expires 10m;
# root /data/web/html/;
# }
location / {
root /data/web/html/sysMaintain/;
if (!-f $request_filename) {
rewrite ^/(.*)$ /sysMaintain.html last;
}
}
}
}


































