如果日后需要加入某個(gè)模塊的話,需要重新編譯,具體:
make clean,然后./configure+新的配置選項(xiàng), make, make install。
難點(diǎn)是如何理解nginx服務(wù)器的配置,位于安裝目錄的nginx.conf文件。
總之需要知道的是:具體如何配置決定于準(zhǔn)備使用nginx實(shí)現(xiàn)什么功能,例如最簡單的單單是個(gè)反向代理服務(wù)器,或者作為一個(gè)http server來使用。各種具體應(yīng)用和配置可以參看一下:
"Typical Configurations Overview For Nginx HTTP(S) Reverse Proxy/Web Server":
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/ 更多詳情參看關(guān)于如何配置的教程:
http://wiki.nginx.org/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類似都是通過module方式對(duì)各種功能進(jìn)行擴(kuò)展,關(guān)于nginx更多信息可以參看如上的鏈接。
3) Windows下的二進(jìn)制安裝非常簡單: 直接下載,解壓縮到C:/根目錄下即可.
具體參看:
http://wiki.nginx.org/NginxInstall nginx -s [ stop | quit | reopen | reload ]
3. nginx的配置: 上面介紹了,首先nginx和Apache類似通過各種模塊module可以對(duì)服務(wù)器的功能進(jìn)行豐富的擴(kuò)展。同樣也是類似通過nginx.conf文件可以對(duì)進(jìn)行核心配置或者針對(duì)各種模塊的配置。
http://wiki.nginx.org/NginxModules 具體如何配置通常還是取決于,準(zhǔn)備使用nginx來做什么,幾個(gè)很好的關(guān)于配置的參考資料:
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/ http://wiki.nginx.org/NginxConfiguration 0) 首先我們展示一下默認(rèn)的nginx.conf文件中各個(gè)部分: nginx.conf配置文件中,針對(duì)不同的模塊使用大括號(hào)包括起來,很方便地進(jìn)行配置;
一個(gè)非常好的教程可以參看:
http://wiki.nginx.org/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; #開啟自動(dòng)列出目錄文件功能,如果找不到index頁面的話。
index index.html index.htm; #設(shè)置默認(rèn)的首頁
}
#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)偵聽的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è)說明了nginx本身不包裹FastCGI處理器,而是僅僅通過如下的方式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ī)情況支持多種類型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;
# }
#}
}
下面我們來簡單介紹各種使用nginx的情況: 可以參考
http://blog.kovyrin.net/2006/04/17/typical-nginx-configurations/ 1)默認(rèn)安裝的nginx配置文件中l(wèi)isten 80; 表示偵聽80端口的HTTP請求,如果預(yù)先安裝了Apache的話,就會(huì)占用該端口,從而使得Apache無法使用。
默認(rèn)的配置提供對(duì)各種靜態(tài)文件的訪問;
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腳本的請求,都proxy_pass到后臺(tái)偵聽的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://sameerparwani.com/posts/nginx-as-a-front-end-to-apache/ 常見的情況是,使用nginx直接處理靜態(tài)的請求響應(yīng);對(duì)于動(dòng)態(tài)的才pass給服務(wù)器,而且對(duì)服務(wù)器的響應(yīng)結(jié)果進(jìn)行緩存。
3)nginx做為多臺(tái)server的負(fù)載均衡作用: http://sameerparwani.com/posts/load-balancing-with-nginx/ 4)另外一個(gè)常見的應(yīng)用就是直接使用nginx代替了Apache,使用Linux+Nginx+MyQL+PHP的stack。 這個(gè)時(shí)候首先需要以FastCGI的方式安裝PHP(需要一個(gè)FastCGI的process server),然后配置nginx支持PHP。
具體參考相關(guān)的專題介紹。
5)更多的配置介紹參看 http://wiki.nginx.org/NginxModules 各個(gè)模塊的配置和configure教程。