Nginx 是一个轻量级高性能的 Web 服务器, 并发处理能力强, 消耗资源小, 无论是静态服务器还是网站, Nginx 表现更加出色, 作为 Apache 的补充和替代使用率越来越高,目前很多大型网站都在使用Nginx做为 Web 服务器,例如:人人网。另外淘宝研发大军针对大访问量网站的需求,对Nginx做了专门的定制,添加了很多高级功能和特性(Tengine),Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验。
本文将讲解如何在Ubuntu Linux上使用 Nginx Web服务器来实现一台电脑一个端口(80)搭建多个网站。
绝大多数的 Nginx 运行在 Linux 机器上, 虽然有 Windows 移植版,但在Windows下的测试发现Nginx发挥不是很好. 所以本文将以 Linux 为例讲解, 而 Mac OS 或其他 Unix like 机器上的操作应该是一样的.
Nginx版本
lg@lg-PC:~$ nginx -v
nginx version: nginx/1.3.10
nginx配置文件默认目录结构
/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 虚拟主机
配置 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.关键步骤,在目录/etc/nginx/conf.d/下面新建文件site1.conf,site2.conf,文件名任意写,自己看明白就OK,后缀名需要与步骤1配置的一致,这里为.conf。site1代表我们的第一个站点,site2代表我们的第二个站点,下面我们看看两个文件都需要写点什么:
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 ~^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 -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 openlg.net
5.打开浏览器分别请求下面的地址进行测试,如果相应内容不一样,那么咱们就大功告成了。
http://127.0.0.1 http://openlg.net
到这里,大家也许已经明白怎么回事了,我再罗嗦两句,我这里的site1.conf相当与一个静态文件服务器,site2.conf是一个php的网站,大家可以看到配置文件中加粗显示的两行:listen和server_name分别代表监听端口和虚拟主机名称。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了。