Nginx 是一個輕量級高性能的 Web 服務(wù)器, 并發(fā)處理能力強, 消耗資源小, 無論是靜態(tài)服務(wù)器還是網(wǎng)站, Nginx 表現(xiàn)更加出色, 作為 Apache 的補充和替代使用率越來越高,目前很多大型網(wǎng)站都在使用Nginx做為 Web 服務(wù)器,例如:人人網(wǎng)。另外淘寶研發(fā)大軍針對大訪問量網(wǎng)站的需求,對Nginx做了專門的定制,添加了很多高級功能和特性(Tengine),Tengine的性能和穩(wěn)定性已經(jīng)在大型的網(wǎng)站如淘寶網(wǎng),天貓商城等得到了很好的檢驗。
本文將講解如何在Ubuntu Linux上使用 Nginx Web服務(wù)器來實現(xiàn)一臺電腦一個端口(80)搭建多個網(wǎng)站。
絕大多數(shù)的 Nginx 運行在 Linux 機器上, 雖然有 Windows 移植版,但在Windows下的測試發(fā)現(xiàn)Nginx發(fā)揮不是很好. 所以本文將以 Linux 為例講解, 而 Mac OS 或其他 Unix like 機器上的操作應該是一樣的.
lg@lg-PC:~$ nginx -vnginx version: nginx/1.3.10
/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-utf1 directory, 11 files
配置 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代表我們的第一個站點,site2代表我們的第二個站點,下面我們看看兩個文件都需要寫點什么:
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; }}
server { listen 80; server_name ~^openlg.net$; 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.測試配置文件,沒問題就加載新配置文件
lg@lg-PC:~$ sudo nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfullg@lg-PC:~$ sudo kill -HUP `cat /var/run/nginx.pid`lg@lg-PC:~$
4.打開文件/etc/hosts,添加
127.0.0.1 openlg.net
5.打開瀏覽器分別請求下面的地址進行測試,如果相應內(nèi)容不一樣,那么咱們就大功告成了。
http://127.0.0.1http://openlg.net
到這里,大家也許已經(jīng)明白怎么回事了,我再羅嗦兩句,我這里的site1.conf相當與一個靜態(tài)文件服務(wù)器,site2.conf是一個php的網(wǎng)站,大家可以看到配置文件中加粗顯示的兩行:listen和server_name分別代表監(jiān)聽端口和虛擬主機名稱。80端口沒得說,重點解釋下server_name,server_name用的是正則表達式,~^\d+\.\d+\.\d+\.\d+$匹配所有的ip地址,~^openlg.net$匹配openlg.net,這里也可以直接寫成server_name 127.0.0.1;或者server_name openlg.net;
當我們請求http://127.0.0.1/時,nginx會使用兩個server_name配置的正則表達式分別去測試請求地址中的127.0.0.1來決定使用那個虛擬主機,這里當然就是使用site1.conf中配置的server了。當我們請求http://openlg.net/時,nginx就會選擇使用site2.conf中配置的server了。
聯(lián)系客服