3.1 nginx的完整配置示例
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),新城企業(yè)網(wǎng)站建設(shè),新城品牌網(wǎng)站建設(shè),網(wǎng)站定制,新城網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,新城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。nginx的配置文件默認(rèn)在nginx程序安裝目錄的conf二級(jí)目錄下,主配置文件為nginx.conf,假設(shè)您的nginx安裝在/usr/local/webserver/nginx/目錄下,那么默認(rèn)的主配置文件則為/usr/local/webserver/nginx/nginx.conf,以下是我當(dāng)時(shí)為某保險(xiǎn)公司搭建的nginx的配置示例(部分信息做了漂白)。
user apache apache; worker_processes 4; error_log /usr/local/webserver/nginx/logs/nginx_error.log crit; pid /usr/local/webserver/nginx/nginx.pid; #Specifies the value for maximum file descriptors that can be opened by this process. worker_rlimit_nofile 65535; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 128k; large_client_header_buffers 4 32k; client_max_body_size 300m; sendfile on; tcp_nopush on; keepalive_timeout 1800; tcp_nodelay on; fastcgi_connect_timeout 1800; fastcgi_send_timeout 1800; fastcgi_read_timeout 1800; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; client_body_buffer_size 512k; proxy_connect_timeout 5; proxy_read_timeout 1800; proxy_send_timeout 1800; proxy_buffer_size 128k; proxy_buffers 4 64k; proxy_busy_buffers_size 128k; proxy_temp_file_write_size 512k; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #add_header Content-Encoding gzip; #注:proxy_temp_path和proxy_cache_path指定的路徑必須在同一分區(qū) proxy_temp_path logs/proxy_temp_dir; #設(shè)置Web緩存區(qū)名稱為cache_one,內(nèi)存緩存空間大小為200MB,1天沒(méi)有被訪問(wèn)的內(nèi)容自動(dòng)清除,硬盤緩存空間大小為30GB。 proxy_cache_path logs/proxy_cache_dir levels=1:2 keys_zone=cache_one:10m inactive=1d max_size=10g; upstream xxx1_domain_cluster { ip_hash; server 9.0.1.11:9000; server 9.0.1.12:9000; server 9.0.1.13:9000; server 9.0.1.14:9000; } upstream xxx2_domain_cluster { ip_hash; server 9.0.1.11:9004; server 9.0.1.12:9004; server 9.0.1.13:9004; server 9.0.1.14:9004; } upstream xxx3_domain_cluster { ip_hash; server 9.0.1.11:9001; server 9.0.1.12:9001; server 9.0.1.13:9001; server 9.0.1.14:9001; } upstream xxx4_domain_cluster{ ip_hash; server 9.0.1.11:9002; server 9.0.1.12:9002; server 9.0.1.13:9002; server 9.0.1.14:9002; } upstream xxx5_domain_cluster{ ip_hash; server 9.0.1.11:9003; server 9.0.1.12:9003; server 9.0.1.13:9003; server 9.0.1.14:9003; } upstream xxx6_domain_cluster{ ip_hash; server 9.0.1.11:9005; server 9.0.1.12:9005; server 9.0.1.13:9005; server 9.0.1.14:9005; } upstream xxx7_domain_cluster{ ip_hash; server 9.0.1.11:9006; server 9.0.1.12:9006; server 9.0.1.13:9006; server 9.0.1.14:9006; } upstream xxx8_domain_cluster{ ip_hash; server 9.0.1.11:9099; server 9.0.1.12:9099; server 9.0.1.13:9099; server 9.0.1.14:9099; } upstream xxx9_domain_cluster{ ip_hash; server 9.0.1.11:9007; server 9.0.1.12:9007; server 9.0.1.13:9007; server 9.0.1.14:9007; } upstream xxx10_domain_cluster{ ip_hash; server 9.0.1.11:9008; server 9.0.1.12:9008; server 9.0.1.13:9008; server 9.0.1.14:9008; } upstream xxx11_domain_cluster{ ip_hash; server 9.0.1.11:8008; server 9.0.1.12:8008; server 9.0.1.13:8008; server 9.0.1.14:8008; } server { listen 80; server_name 9.0.1.11; index index.html Login.jsp index.jsp QueryCodeInputOverview.jsp; default_type \'text/html\'; charset UTF-8; root /usr/local/webserver/nginx/html; location /xxx/ { charset gb2312; index broadcast.html; proxy_pass http://guoyuan_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset gb2312; #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://xxx1_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } location /undwrt/ { proxy_pass http://undwrt_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ if ($request_uri ~* ^9.0.1.5(.*)$){ rewrite ^(.*)1.5(.*)/undwrt(.*)$ http://9.0.1.13/undwrt permanent; proxy_pass http://$http_host$request_uri; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://undwrt_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_undwrt_domain_cluster.log; } location /prpall/ { charset utf-8; proxy_pass http://prpall_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset utf-8; #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://prpall_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_prpall_domain_cluster.log; } location /account/ { proxy_pass http://account_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://account_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_account_domain_cluster.log; } location /payment/ { charset utf-8; proxy_pass http://payment_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset utf-8; #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://payment_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_payment_domain_cluster.log; } location /visa/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_visa_domain_cluster.log; } location /reins/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_reins_domain_cluster.log; } location /newclaim/ { if ($host ~* ^www.gyic.(.*?)$) { rewrite ^/(.*)$ http://www.gyic.com:9003/newclaim last; } charset GB2312; proxy_pass http://newclaim_domain_cluster; proxy_set_header Host $host; gzip on; gzip_min_length 1k; gzip_buffers 4 128k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; add_header Content-Encoding gzip; location ~(.jsp)|(.do)|(.htm)|(.html)~{ charset GBK; #if ($host ~* ^9.0.1.(.*?)$) #{ #rewrite ^/(.*)$ http://9.0.1.13:9003/newclaim last; #} if ($host ~* ^www.gyic.(.*?)$) { rewrite ^/(.*)$ http://www.gyic.com:9003/newclaim last; } gzip on; gzip_min_length 1k; gzip_buffers 4 128k; gzip_http_version 1.1; gzip_comp_level 2; gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; add_header Content-Encoding gzip; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://newclaim_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_newclaim_domain_cluster.log; } location /claim/ { if ($host ~* ^www.gyic.(.*?)$) { rewrite ^/(.*)$ http://www.gyic.com:9002/claim last; } charset GB2312; proxy_pass http://claim_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)|(.htm)|(.html)~{ charset GBK; if ($host ~* ^www.gyic.(.*?)$) { rewrite ^/(.*)$ http://www.gyic.com:9002/claim last; } proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://claim_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_claim_domain_cluster.log; } location /reserve/ { charset utf-8; if ($host ~* ^www.xxxx.(.*?)$) { rewrite ^/(.*)$ http://www.xxxx.com:9007/reserve last; } proxy_pass http://reserve_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset utf-8; if ($host ~* ^www.gyic.(.*?)$) { rewrite ^/(.*)$ http://www.xxxx.com:9007/reserve last; } #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://reserve_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_reserve_domain_cluster.log; } location /platform/ { charset utf-8; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset utf-8; #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_patform_domain_cluster.log; } location /newreport/ { charset UTF-8; proxy_pass http://newreport_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)|(.html)~{ default_type \'text/html\'; charset GB2312; index newreporthelp.html welcome.html; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://newreport_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_newreport_domain_cluster.log; } location /anareport/ { charset GB2312; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ charset GB2312; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_anareport_domain_cluster.log; } location /FXQ/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_FXQ_domain_cluster.log; } location /srs/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_srs_domain_cluster.log; } location /sales/ { #if ($host ~* ^www.gyic.(.*?)$) #{ #rewrite ^/(.*)$ http://www.xxxx.com/sales/common/Login.jsp break; #} #index Login.jsp proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #if ($host ~* ^www.gyic.(.*?)$) #{ #rewrite ^/(.*)$ http://www.xxxx.com/sales/common/Login.jsp break; #} #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_sales_domain_cluster.log; } location /webapp/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_webapp_domain_cluster.log; } location /WebRoot/ { proxy_pass http://webroot_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #index index.jsp QueryCodeInputOverview.jsp QueryCodeInputOverviewResult.jsp; proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://webroot_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_webapp_domain_cluster.log; } location /nginxstatus { stub_status on; access_log off; allow all; } location / { #如果后端的服務(wù)器返回502、504、執(zhí)行超時(shí)等錯(cuò)誤,自動(dòng)將請(qǐng)求轉(zhuǎn)發(fā)到upstream負(fù)載均衡池中的另一臺(tái)服務(wù)器,實(shí)現(xiàn)故障轉(zhuǎn)移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; #對(duì)不同的HTTP狀態(tài)碼設(shè)置不同的緩存時(shí)間 proxy_cache_valid 200 304 12h; #以域名、URI、參數(shù)組合成Web緩存的Key值,Nginx根據(jù)Key值哈希,存儲(chǔ)緩存內(nèi)容到二級(jí)緩存目錄內(nèi) proxy_cache_key $host$uri$is_args$args; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; #proxy_pass http://backend_server; proxy_pass http://xxx1_domain_cluster; #proxy_pass http://www.xxx.com; access_log logs/access_backend_server_log; expires 1d; } #用于清除緩存,假設(shè)一個(gè)URL為http://192.168.8.42/test.txt,通過(guò)訪問(wèn)http://192.168.8.42/purge/test.txt就可以清除該URL的緩存。 location ~ /purge(/.*) { #設(shè)置只允許指定的IP或IP段才可以清除URL緩存。 allow 127.0.0.1; allow 9.0.0.0/16; # deny all; proxy_cache_purge cache_one $host$1$is_args$args; } #擴(kuò)展名以.php、.jsp、.cgi結(jié)尾的動(dòng)態(tài)應(yīng)用程序不緩存。 location ~ .*.(php|cgi)?$ { proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; #proxy_pass http://backend_server; } location /get/ { set $hostx ""; set $addrs ""; if ( $uri ~ "^/get/http./+([^/]+)/(.+)$") { set $hostx $1; set $addrs $2; } resolver 127.0.0.1; proxy_pass http://$hostx/$addrs; proxy_set_header referer "http://$hostx"; } access_log logs/access.log; } server { listen 9.0.1.5:9004; location /undwrt/ { #if ($host ~* ^9.0.1.5.(.*?)$){ #rewrite ^/(.*)$ http://9.0.1.13:9004/undwrt redirect; #} proxy_pass http://undwrt_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #if ($host ~* ^9.0.1.5.(.*?)$){ #rewrite ^/(.*)$ http://9.0.1.13:9004/undwrt redirect; #} proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://undwrt_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_undwrt1_domain_cluster.log; } } server { listen 9.0.1.5:9001; location /prpall/ { #if ($host ~* ^9.0.1.5.(.*?)$){ #rewrite ^/(.*)$ http://9.0.1.13:9001/prpall redirect; #} proxy_pass http://prpall_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ #if ($host ~* ^9.0.1.5.(.*?)$){ #rewrite ^/(.*)$ http://9.0.1.13:9001/prpall redirect; #} proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://prpall_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_prpall1_domain_cluster.log; } } #server #{ #listen www.xxx.com:9003; #location /newclaim/ { ##if ($host ~* ^www.gyic.com(.*?)$) ##{ ##rewrite ^/(.*)$ http://9.0.1.14:9003/newclaim last; ##} #proxy_pass http://newclaim_domain_cluster; #proxy_set_header Host $host; #location ~(.jsp)|(.do)~{ #proxy_set_header X-Real-IP $remote_addr; #proxy_set_header Host $host; #proxy_pass http://newclaim_domain_cluster; #proxy_set_header Host $host; #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; #} #access_log logs/access_claim1_domain_cluster.log; #} #} server { listen 9.0.1.5:7001; location /reins/ { proxy_pass http://other_domain_cluster; proxy_set_header Host $host; location ~(.jsp)|(.do)~{ proxy_set_header X-Real-IP $remote_addr; proxy_set_header Host $host; proxy_pass http://other_domain_cluster; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } access_log logs/access_reins1_domain_cluster.log; } } }
從示例中可以看出,nginx.conf的配置文件主要由以下幾部分構(gòu)成:
…… events {……} http{ server{……} server{……} } 3.2 nginx的虛擬主機(jī)配置 3.2.1 什么是虛擬主機(jī)
虛擬主機(jī)使用的是特殊的軟硬件技術(shù),他把一臺(tái)運(yùn)行在因特網(wǎng)上的服務(wù)器主機(jī)分成一臺(tái)臺(tái)“虛擬主機(jī)”,每臺(tái)虛擬主機(jī)都可以是一個(gè)獨(dú)立的網(wǎng)站,可以具有獨(dú)立的域名,具有完整的Internet服務(wù)器功能,同一臺(tái)主機(jī)上的虛擬主機(jī)之間是完全獨(dú)立的。從網(wǎng)站訪問(wèn)者來(lái)看,每一臺(tái)虛擬主機(jī)和一臺(tái)獨(dú)立的主機(jī)完全一樣。
利用虛擬主機(jī),不用為每個(gè)要運(yùn)行的網(wǎng)站提供一臺(tái)單獨(dú)的nginx服務(wù)器或單獨(dú)運(yùn)行一組nginx進(jìn)程。虛擬主機(jī)提供了在同一臺(tái)服務(wù)器、同一組nginx進(jìn)程上運(yùn)行多個(gè)網(wǎng)站的功能。
Nginx可以配置多種類型的虛擬主機(jī):一是基于IP的虛擬主機(jī),二是基于域名的虛擬主機(jī),三是基于端口的虛擬主機(jī)。這點(diǎn)與Apache一致。
下面分別講解這三種虛擬主機(jī)的配置方法。
3.2.2 配置基于IP的虛擬主機(jī)UNIX、Linux、FreeBSD操作系統(tǒng)都允許添加IP別名。IP別名的概念很簡(jiǎn)單:可以在一塊物理網(wǎng)卡上綁定多個(gè)IP地址。這樣就能夠在使用單一網(wǎng)卡的同一個(gè)服務(wù)器上運(yùn)行多個(gè)基于IP的虛擬主機(jī)。設(shè)置IP別名也非常容易,只需配置系統(tǒng)上的網(wǎng)絡(luò)接口,讓他監(jiān)聽(tīng)額外的IP地址。在Linux/UNIX系統(tǒng)上,可以使用標(biāo)準(zhǔn)的網(wǎng)絡(luò)配置工具(比如ifconfig和route命令)添加IP別名。
以下是添加IP別名的示例:
先用ifconfig查看該服務(wù)器的IP地址,下面這臺(tái)服務(wù)器有一塊物理網(wǎng)卡設(shè)備eth0和本地回環(huán)設(shè)備lo,eth0的IP地址為192.168.8.42,本地回環(huán)lo的IP地址為127.0.0.1。本地回環(huán)代表設(shè)備的本地虛擬接口,所以默認(rèn)被看做是永遠(yuǎn)不會(huì)宕掉的接口,他的主要作用有兩個(gè):一是測(cè)試本機(jī)的網(wǎng)絡(luò)配置,能ping通127.0.0.1說(shuō)明本機(jī)的網(wǎng)卡和IP協(xié)議安裝都沒(méi)有問(wèn)題;另一個(gè)作用是某些Server/Client的應(yīng)用程序在運(yùn)行時(shí)須調(diào)用服務(wù)器上的資源,一般要指定SERVER的IP地址,但當(dāng)該程序要在同一臺(tái)機(jī)器上運(yùn)行且沒(méi)有別的Server時(shí),就可以把SERVER的資源裝在本機(jī)上,SERVER的IP地址設(shè)為127.0.0.1,也可以同樣運(yùn)行。
如果要在eth0網(wǎng)卡設(shè)備上添加兩個(gè)IP別名192.168.8.43和192.168.8.44,可以通過(guò)以下的ifconfig和route命令來(lái)進(jìn)行:
/sbin/ifconfig eth0:1 192.168.8.43 broadcast 192.168.8.255 netmask 255.255.255.0 up /sbin/route add –host 192.168.8.43 dev eth0:1 /sbin/ifconfig eth0:2 192.168.8.44 broadcast 192.168.8.255 netmask 255.255.255.0 up /sbin/route add –host 192.168.8.44 dev eth0:2
這時(shí),在執(zhí)行ifconfig命令,就可以看到eth0網(wǎng)卡設(shè)備上綁定了兩個(gè)IP別名。
這時(shí)候,從另外一臺(tái)服務(wù)器ping 192.168.8.43 和 192.168.8.44兩個(gè)IP,如果能夠PING通,則證明配置無(wú)誤。但是,通過(guò)ifconfig和route配置的IP別名在服務(wù)器重啟后會(huì)自動(dòng)消失,不過(guò)可以將這兩條ifconfig和route命令添加到/etc/rc.local文件中,讓系統(tǒng)開(kāi)機(jī)時(shí)自動(dòng)運(yùn)行,以下是相關(guān)命令。
在文件末尾增加以下內(nèi)容,然后保存即可:
/sbin/ifconfig eth0:1 192.168.8.43 broadcast 192.168.8.255 netmask 255.255.255.0 up /sbin/route add –host 192.168.8.43 dev eth0:1 /sbin/ifconfig eth0:2 192.168.8.44 broadcast 192.168.8.255 netmask 255.255.255.0 up /sbin/route add –host 192.168.8.44 dev eth0:2
下面開(kāi)始配置基于IP的虛擬主機(jī)。無(wú)論是通過(guò)IP別名在一臺(tái)服務(wù)器上配置多個(gè)IP地址,還是通過(guò)多塊網(wǎng)卡在服務(wù)器上配置多個(gè)IP地址,在NGINX中都能將其配置成為基于IP的虛擬主機(jī)。
接下來(lái)在NGINX配置文件(nginx.conf)中,分別對(duì)192.168.8.43、192.168.8.44、192.168.8.45三個(gè)IP配置三個(gè)純靜態(tài)HTML支持的虛擬主機(jī),如代碼:
http{ server{ listen 192.168.8.43:80; server_name 192.168.8.43; access_log logs/server1.access.log combined; location /{ #默認(rèn)首頁(yè)文件,順序從左到右,如果找不到index.html文件,則查找index.htm文件作為首頁(yè)文件 index index.html index.htm; root /data0/htdocs/server1; } …… }
從上面的配置文件可以看出,一段server{……}就是一個(gè)虛擬主機(jī),如果要配置多個(gè)虛擬主機(jī),建立多段server{}配置即可,非常方便。監(jiān)聽(tīng)I(yíng)P和端口也可以不寫IP地址,只寫端口,把它配置成“listen 80”,則表示監(jiān)聽(tīng)該服務(wù)器上所有IP的80端口,可通過(guò)server_name區(qū)分不同的虛擬主機(jī)。
3.2.3 配置基于域名的虛擬主機(jī)基于域名的虛擬主機(jī)是最常見(jiàn)的一種虛擬主機(jī)。只需配置你的DNS服務(wù)器,將每個(gè)主機(jī)名映射到正確的IP地址,然后配置NGINX服務(wù)器,令其識(shí)別不同的主機(jī)名就可以了。這種虛擬主機(jī)技術(shù),使很多虛擬主機(jī)可以共享同一個(gè)IP地址,有效解決了IP地址不足的問(wèn)題。所以,如果沒(méi)有特殊要求使你必須配置一個(gè)基于IP的虛擬主機(jī),最好還是使用基于域名的虛擬主機(jī)。
接下來(lái)配置基于域名的虛擬主機(jī)。在以下的示例中,配置了三個(gè)虛擬主機(jī),第一個(gè)虛擬主機(jī)表示對(duì)域名aaa.domain.com的訪問(wèn)都由它來(lái)處理,第二個(gè)虛擬主機(jī)表示所有對(duì)域名bbb.otherdomain.com的訪問(wèn)都由它來(lái)處理,第三個(gè)虛擬主機(jī)表示對(duì)域名www.domain.com、domain.com,以及除了aaa.domain.com之外的所有*.domain.com二級(jí)域名的訪問(wèn)都由它來(lái)處理。每個(gè)虛擬主機(jī)的網(wǎng)頁(yè)文件分別存放了不同的目錄中,每個(gè)虛擬主機(jī)使用了不同的日志文件來(lái)記錄訪問(wèn)日志,如代碼:
http { server { listen 80; server_name aaa.domain.com; access_log logs/aaa.domain.com.access.log combined; location / { index index.html index.htm; root /data0/htdocs/aaa.domain.com; } } server 80; server_name bbb.otherdomain.com; access_log logs/bbb.otherdomain.com.access.log combined; location / { index index.html index.htm; root /data0/htdocs/bbb.otherdomain.com; } } …… } 3.3 nginx 的日志文件配置與切割
在上面的nginx虛擬主機(jī)配置中,已經(jīng)使用了access_log進(jìn)行了日志記錄,這一節(jié)中,我們將詳細(xì)介紹Nginx訪問(wèn)日志文件得配置。
與nginx日志相關(guān)的指令主要有兩條,一條是log_format,用來(lái)設(shè)置日志的格式,另外一條是access_log,用來(lái)指定日志文件得存放路徑、格式和緩存大小。兩條指令在nginx配置文件中的位置可以在http{……}之間,也可以在虛擬主機(jī)之間,即server{……}兩個(gè)大括號(hào)之間。
3.3.1 用log_format指令設(shè)置日志格式log_format指令用來(lái)設(shè)置日志的記錄格式,它的語(yǔ)法如下:log_format name format [format …]
其中name 表示定義的格式名稱,format表示定義的格式樣式。log_format有一個(gè)默認(rèn)的、無(wú)須設(shè)置的combined日志格式設(shè)置,相當(dāng)于Apache的combined日志格式,其具體參數(shù)如下:
log_format combined ‘$remote_addr - $remote_user [$time_local]’ ‘”$request” $status $body_bytes_sent ‘ ‘”$http_referer” “$http_user_agent”’;
注意:log_format指令設(shè)置的name名稱在nginx配置文件中是不能重復(fù)的。
假設(shè)將nginx服務(wù)器作為web服務(wù)器,位于負(fù)載均衡設(shè)備、Squid、Nginx反向代理之后,就不能獲取到客戶端的真實(shí)IP地址了。原因是經(jīng)過(guò)反向代理之后,由于在客戶端和Web服務(wù)器之間增加了中間層,因此Web服務(wù)器無(wú)法直接拿到客戶端的IP,通過(guò)$remote_addr變量拿到的將是反向代理服務(wù)器的IP地址。但是,反向代理服務(wù)器在轉(zhuǎn)發(fā)請(qǐng)求的HTTP頭信息中,可以增加X(jué)-Formated-For信息,用以記錄原有的客戶端IP地址和原來(lái)客戶端請(qǐng)求的服務(wù)器地址。
這時(shí)候,就要用log_format指令來(lái)設(shè)置日志格式,讓日志記錄X-Forwarded-For信息中的IP地址,即客戶的真實(shí)IP。例如,創(chuàng)建一個(gè)名為mylogformat的日志格式,再用$http_x_forwarded_for變量記錄用戶的X-Forwarded-For IP地址:
log_format mylogformat ‘$http_x_forwarded_for - $remote_user [$time_local]’ ‘”$request” $status $bodybytes_sent ‘ ‘”$http_referer”
在日志變量中,變量$remote_addr和$http_x_forwarded_for用于記錄IP地址;$remote_user用于記錄遠(yuǎn)程客戶端用戶名稱;$time_local用于記錄訪問(wèn)時(shí)間與時(shí)區(qū);$request用于記錄請(qǐng)求URL與HTTP協(xié)議;$status 用于記錄請(qǐng)求狀態(tài),例如成功時(shí)狀態(tài)為200,頁(yè)面找不到時(shí)狀態(tài)為404;$body_bytes_sent用于記錄發(fā)送給客戶端的主體內(nèi)容大??;$http_referer用于記錄是從哪個(gè)頁(yè)面鏈接訪問(wèn)過(guò)來(lái)的;$http_user_agent用于記錄客戶端瀏覽器的相關(guān)信息。
以下為日志范例:
124.42.4.194 - -[12/Mar/2012:02:18:23 +0800] “GET / HTTP/1.1” 200 36179 “-” Mozilla/4.0 (compatible;MSIE 7.0;Windows NT 5.1 ;Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1);CIBA;.NET CLR 2.0.50727)” 3.3.2 Nginx日志文件得切割
生產(chǎn)環(huán)境中的服務(wù)器,用于訪問(wèn)日志文件增長(zhǎng)速度非??欤罩咎髸?huì)嚴(yán)重影響服務(wù)器效率。同時(shí),為了方便對(duì)日志進(jìn)行分析計(jì)算,須要對(duì)日志文件進(jìn)行定時(shí)切割。定時(shí)切割的方式有按月、按天、按小時(shí)切割等。最常用的是按天切割。
Nginx不支持像Apache一樣使用cronolog來(lái)輪轉(zhuǎn)日志,但是可以采用以下方式來(lái)實(shí)現(xiàn)日志文件得切割:
mv /data1/logs/access.log /data1/logs/20120418.log
kill -USR1 Nginx主進(jìn)程號(hào)
首先通過(guò)mv 命令將日志文件重命名為/data1/logs/20120418.log,然后發(fā)送kill –USR1信號(hào)給NGINX的主進(jìn)程號(hào),讓Nginx重新生成一個(gè)新的日志文件/data1/logs/access.log。如果nginx.conf配置文件中使用了”pid /usr/local/webserver/nginx/nginx.pid;”指令,指定了pid文件得存放路徑,我們可以通過(guò)cat 這個(gè)pid文件獲得nginx的主進(jìn)程號(hào),命令如下:
kill –USR1 ‘cat /usr/local/webserver/nginx/nginx.pid’
如果想每天定時(shí)切割日志,還需要借助crontab,我們可以寫一個(gè)按天切割的日志,按年、月份目錄存放日志的shell腳本:
vi /usr/local/webserver/nginx/sbin/cut_nginx_log.sh,輸入以下內(nèi)容并保存:
#!/sbin/bash #這個(gè)腳本須在每天的00:00運(yùn)行 #nginx 日志文件得存放路徑 logs_path = “data1/logs/” mkdir –p ${logs_path}$(date –d “yesterday” + “%Y”)/$(date –d “yesterday” + “%m”)/ mv ${logs_path}access.log ${logs_path}$(date –d “yesterday” + “%Y”)/$(date –d “yesterday” +”%m”)/access_$(date –d “yesterday” +”%Y%m%d”).log kill –USR1 ‘cat /usr/local/webserver/nginx/nginx.pid’
配置Crontab每天凌晨00:00定時(shí)執(zhí)行這個(gè)腳本:crontab –e
輸入以下內(nèi)容:
00 00 * * * /sbin/bash /usr/local/webserver/nginx/sbin/cut_nginx_log.sh 3.4 nginx的壓縮輸出配置
gzip(GNU-ZIP)是一種壓縮技術(shù)。經(jīng)過(guò)gzip壓縮后的頁(yè)面大小可以變?yōu)樵瓉?lái)的30%甚至更小。這樣,用戶瀏覽頁(yè)面的時(shí)候會(huì)快得多。gzip的壓縮頁(yè)面需要瀏覽器和服務(wù)器雙方都支持,實(shí)際上就是服務(wù)器端壓縮,傳到瀏覽器后瀏覽器解壓并解析。瀏覽器那里我們不用擔(dān)心,因?yàn)镮E、Firefox、Opera、谷歌Chrome等絕大多數(shù)瀏覽器都支持解析gzip過(guò)的頁(yè)面。
nginx的壓縮輸出由一組gzip壓縮指令來(lái)實(shí)現(xiàn)。我們從示例入手,來(lái)學(xué)習(xí)gzip壓縮輸出。gzip壓縮輸出的相關(guān)指令位于http{……}兩個(gè)大括號(hào)之間:
gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_com_level 2; gzip_types text/plain application/x-javascript text/css /application/xml; gzip_vary on; 3.5 nginx的瀏覽器本地緩存設(shè)置
瀏覽器緩存是為了加速瀏覽,瀏覽器在用戶磁盤上對(duì)最近請(qǐng)求過(guò)的文檔進(jìn)行存儲(chǔ),當(dāng)訪問(wèn)者再次請(qǐng)求這個(gè)頁(yè)面時(shí),瀏覽器就可以從本地磁盤顯示文檔,這樣就可以加速頁(yè)面的閱覽。緩存的方式節(jié)約了網(wǎng)絡(luò)的資源,提高了網(wǎng)絡(luò)的效率。
location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*.(js|css)?$ { expires 1h; }