nginx
轉(zhuǎn)載請(qǐng)注明原文鏈接:http://blog.csdn.net/omohe/archive/2009/07/09/4335763.aspx 版本:v1.0 作者:OMO 最后修改時(shí)間:2009.07.09
0. nginx在實(shí)際中使用:
如果日后需要加入某個(gè)模塊的話,需要重新編譯,具體:
make clean,然后./configure+新的配置選項(xiàng), make, make install。 難點(diǎn)是如何理解nginx服務(wù)器的配置,位于安裝目錄的nginx.conf文件。 總之需要知道的是:具體如何配置決定于準(zhǔn)備使用nginx實(shí)現(xiàn)什么功能,例如最簡(jiǎn)單的單單是個(gè)反向代理服務(wù)器,或者作為一個(gè)http server來(lái)使用。各種具體應(yīng)用和配置可以參看一下: "Typical Configurations Overview For Nginx HTTP(S) Reverse Proxy/Web Server": http://blog./2006/04/17/typical-nginx-configurations/ 更多詳情參看關(guān)于如何配置的教程: http://wiki./NginxConfiguration 默認(rèn)安裝到/usr/local/nginx,具體啟動(dòng)的時(shí)候需要到bin目錄下執(zhí)行sudo ./nginx; 要終止nginx可以查看位于logs目錄下的pid,然后kill pid即可; 2) 如果是二進(jìn)制包安裝的話: 首先搜搜看apt-cache search nginx 然后安裝sudo apt-get install nginx Ubuntu下查看默認(rèn)安裝的位置,可以使用whereis nginx nginx和Apache類(lèi)似都是通過(guò)module方式對(duì)各種功能進(jìn)行擴(kuò)展,關(guān)于nginx更多信息可以參看如上的鏈接。 3) Windows下的二進(jìn)制安裝非常簡(jiǎn)單: 直接下載,解壓縮到C:/根目錄下即可. 具體參看:http://wiki./NginxInstall nginx -s [ stop | quit | reopen | reload ] 3. nginx的配置: 上面介紹了,首先nginx和Apache類(lèi)似通過(guò)各種模塊module可以對(duì)服務(wù)器的功能進(jìn)行豐富的擴(kuò)展。同樣也是類(lèi)似通過(guò)nginx.conf文件可以對(duì)進(jìn)行核心配置或者針對(duì)各種模塊的配置。 http://wiki./NginxModules 具體如何配置通常還是取決于,準(zhǔn)備使用nginx來(lái)做什么,幾個(gè)很好的關(guān)于配置的參考資料: http://blog./2006/04/17/typical-nginx-configurations/ http://wiki./NginxConfiguration 0) 首先我們展示一下默認(rèn)的nginx.conf文件中各個(gè)部分: nginx.conf配置文件中,針對(duì)不同的模塊使用大括號(hào)包括起來(lái),很方便地進(jìn)行配置; 一個(gè)非常好的教程可以參看: http://wiki./NginxModules #user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #這里表示設(shè)置該server的root根目錄是nginx安裝目錄的html目錄,當(dāng)然可以設(shè)置絕對(duì)路徑 location / { root html; autoindex on; #開(kāi)啟自動(dòng)列出目錄文件功能,如果找不到index頁(yè)面的話。 index index.html index.htm; #設(shè)置默認(rèn)的首頁(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; } #這里是一個(gè)很好的nginx作為反向代理服務(wù)的設(shè)置,表示遇到php結(jié)尾的,會(huì)自動(dòng)pass給后臺(tái)偵聽(tīng)的apache或者別的服務(wù)器; #proxy the PHP scripts to Apache listening on 127.0.0.1:80 location ~ \.php$ { proxy_pass http://127.0.0.1 ; } #如果安裝了FastCGI,則將其pass給FastCGI,這個(gè)說(shuō)明了nginx本身不包裹FastCGI處理器,而是僅僅通過(guò)如下的方式pass給預(yù)先安裝的FastCGI server,具體PHP的FastCGI如何安裝,可以參看相關(guān)教程。 # 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; #} } #nginx的虛擬主機(jī)情況支持多種類(lèi)型name-based(多個(gè)網(wǎng)站ip相同,域名不同),ip-based(多個(gè)網(wǎng)站ip不同) # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} #提供SSL認(rèn)證的server配置 # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 下面我們來(lái)簡(jiǎn)單介紹各種使用nginx的情況: 可以參考http://blog./2006/04/17/typical-nginx-configurations/ 1)默認(rèn)安裝的nginx配置文件中l(wèi)isten 80; 表示偵聽(tīng)80端口的HTTP請(qǐng)求,如果預(yù)先安裝了Apache的話,就會(huì)占用該端口,從而使得Apache無(wú)法使用。 默認(rèn)的配置提供對(duì)各種靜態(tài)文件的訪問(wèn); 2)nginx作為反向代理服務(wù)器使用 如上面的配置, #proxy the PHP scripts to Apache listening on 127.0.0.1:80 location ~ \.php$ { proxy_pass http://127.0.0.1 ; } 我們可以設(shè)置所有對(duì)php script腳本的請(qǐng)求,都proxy_pass到后臺(tái)偵聽(tīng)的web server服務(wù)器(例如apache)等。 nginx還提供了一些模塊例如Memcached,或者結(jié)合Squid等可以實(shí)現(xiàn)pass到后臺(tái)web server相應(yīng)之后,對(duì)返回的數(shù)據(jù)進(jìn)行cache,從而提供性能。 這個(gè)相當(dāng)于使用nginx作為apache的前端,可以看看這個(gè)教程: http:///posts/nginx-as-a-front-end-to-apache/ 常見(jiàn)的情況是,使用nginx直接處理靜態(tài)的請(qǐng)求響應(yīng);對(duì)于動(dòng)態(tài)的才pass給服務(wù)器,而且對(duì)服務(wù)器的響應(yīng)結(jié)果進(jìn)行緩存。 3)nginx做為多臺(tái)server的負(fù)載均衡作用: http:///posts/load-balancing-with-nginx/ 4)另外一個(gè)常見(jiàn)的應(yīng)用就是直接使用nginx代替了Apache,使用Linux+Nginx+MyQL+PHP的stack。 這個(gè)時(shí)候首先需要以FastCGI的方式安裝PHP(需要一個(gè)FastCGI的process server),然后配置nginx支持PHP。 具體參考相關(guān)的專(zhuān)題介紹。 5)更多的配置介紹參看 http://wiki./NginxModules 各個(gè)模塊的配置和configure教程。 |
|