Nginx 是一個(gè)輕量級(jí)高性能的 Web 服務(wù)器, 并發(fā)處理能力強(qiáng), 消耗資源小, 無(wú)論是靜態(tài)服務(wù)器還是網(wǎng)站, Nginx 表現(xiàn)更加出色, 作為 Apache 的補(bǔ)充和替代使用率越來(lái)越高,目前很多大型網(wǎng)站都在使用Nginx做為 Web 服務(wù)器,例如:人人網(wǎng)。另外淘寶研發(fā)大軍針對(duì)大訪問(wèn)量網(wǎng)站的需求,對(duì)Nginx做了專門的定制,添加了很多高級(jí)功能和特性(Tengine),Tengine的性能和穩(wěn)定性已經(jīng)在大型的網(wǎng)站如淘寶網(wǎng),天貓商城等得到了很好的檢驗(yàn)。 本文將講解如何在Ubuntu Linux上使用 Nginx Web服務(wù)器來(lái)實(shí)現(xiàn)一臺(tái)電腦一個(gè)端口(80)搭建多個(gè)網(wǎng)站。 絕大多數(shù)的 Nginx 運(yùn)行在 Linux 機(jī)器上, 雖然有 Windows 移植版,但在Windows下的測(cè)試發(fā)現(xiàn)Nginx發(fā)揮不是很好. 所以本文將以 Linux 為例講解, 而 Mac OS 或其他 Unix like 機(jī)器上的操作應(yīng)該是一樣的. Nginx版本lg@lg-PC:~$ nginx -v nginx version: nginx/1.3.10 nginx配置文件默認(rèn)目錄結(jié)構(gòu)/etc/nginx/ ├── conf.d │ ├── default.conf │ ├── example_ssl.conf ├── fastcgi.conf ├── fastcgi_params ├── koi-utf ├── koi-win ├── mime.types ├── nginx.conf ├── scgi_params ├── uwsgi_params └── win-utf 1 directory, 11 files 增加 Nginx 虛擬主機(jī)配置 Virtual host 步驟如下: 1.檢查/etc/nginx/nginx.conf配置文件,確保文件中有:include /etc/nginx/conf.d/*.conf; 例如: user lg; worker_processes 2; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; debug_connection 127.0.0.1; debug_connection 192.168.1.0/24; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; client_max_body_size 13m; #gzip on; include /etc/nginx/conf.d/*.conf; } 2.關(guān)鍵步驟,在目錄/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意寫,自己看明白就OK,后綴名需要與步驟1配置的一致,這里為.conf。site1代表我們的第一個(gè)站點(diǎn),site2代表我們的第二個(gè)站點(diǎn),下面我們看看兩個(gè)文件都需要寫點(diǎn)什么: site1.conf:server { listen 80; server_name ~^\d+\.\d+\.\d+\.\d+$; #charset koi8-r; error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 503 504 /50x.html; error_log /var/log/nginx/debug.log debug; index index.html index.htm; root /home/lg/www/; location /svn { root /home/lg/www/; index index.html; } location = /favicon.ico { try_files $uri $uri/favicon.ico /home/lg/www/favicon.ico =404; } location /share { root /home/lg/Downloads; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac). location ~ /\. { deny all; } location ^~ /packages { root /home/lg/Downloads/1software; autoindex on; autoindex_exact_size on; autoindex_localtime on; allow all; } location ^~ /Music { root /home/lg/; autoindex on; autoindex_exact_size on; autoindex_localtime on; allow all; } location ^~ /Videos { root /home/lg/; autoindex on; autoindex_exact_size on; autoindex_localtime on; allow all; } location ^~ /html5 { root /home/lg/workspace/nodejs/; index index.html index.htm; } location ^~ /NginxStatus { stub_status on; access_log on; #auth_basic 'NginxStatus'; #auth_basic_user_file conf.d/htpasswd } location = /50x.html { root /usr/share/nginx/html; } location = /404.html { root /usr/share/nginx/html; } }
site2.conf:server { listen 80; server_name ~^$; root /home/lg/workspace/phpworkspace/wp; index index.php index.html index.htm; location = /favicon.ico { try_files /home/lg/www/favicon.ico =404; #log_not_found off; #access_log off; } # Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store(Mac). location ~ /\. { deny all; } location ~* /(?:uploads|files)/.*\.php$ { deny all; } location / { try_files $uri $uri/ /index.php?$args; } location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { access_log off; log_not_found off; expires max; } location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ { expires 24h; log_not_found off; } error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; } } 3.測(cè)試配置文件,沒(méi)問(wèn)題就加載新配置文件 lg@lg-PC:~$ sudo nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful lg@lg-PC:~$ sudo kill -HUP `cat /var/run/nginx.pid` lg@lg-PC:~$ 4.打開文件/etc/hosts,添加 127.0.0.1 5.打開瀏覽器分別請(qǐng)求下面的地址進(jìn)行測(cè)試,如果相應(yīng)內(nèi)容不一樣,那么咱們就大功告成了。 http://127.0.0.1 http:// 到這里,大家也許已經(jīng)明白怎么回事了,我再羅嗦兩句,我這里的site1.conf相當(dāng)與一個(gè)靜態(tài)文件服務(wù)器,site2.conf是一個(gè)php的網(wǎng)站,大家可以看到配置文件中加粗顯示的兩行:listen和server_name分別代表監(jiān)聽端口和虛擬主機(jī)名稱。80端口沒(méi)得說(shuō),重點(diǎn)解釋下server_name,server_name用的是正則表達(dá)式,~^\d+\.\d+\.\d+\.\d+$匹配所有的ip地址,~^$匹配,這里也可以直接寫成server_name 127.0.0.1;或者server_name ; 當(dāng)我們請(qǐng)求http://127.0.0.1/時(shí),nginx會(huì)使用兩個(gè)server_name配置的正則表達(dá)式分別去測(cè)試請(qǐng)求地址中的127.0.0.1來(lái)決定使用那個(gè)虛擬主機(jī),這里當(dāng)然就是使用site1.conf中配置的server了。當(dāng)我們請(qǐng)求http:///時(shí),nginx就會(huì)選擇使用site2.conf中配置的server了。 |
|