Nginx 多站点 Virtual Hosting 配置指南
当我们有了一个 VPS 主机以后,为了不浪费 VPS 的强大资源(相比共享主机1000多个站点挤在一台机器上),往往有想让 VPS 做点什么的想法,银子不能白花啊:)。放置多个网站或者博客是个不错的想法,可是如何配置 web 服务器才能在一个 VPS 上放置多个网站/博客呢?如何通过一个 IP 访问多个站点/域名呢?这就是大多数 web 服务器支持的 virtual hosting 功能。这里将描述如何一步一步如何用 nginx 配置 virtual hosting。
Nginx 是一款由俄罗斯程序员 Igor Sysoev 开发的小型且高效的 Web 服务器。尽管体积小巧,但其功能并不逊色于其他主流服务器,同样支持 Virtual Hosting 特性。该特性允许一个 IP 对应多个域名,从而支持多站点访问,效果如同每个站点拥有独立 IP 一样,因此被称为“虚拟”。只要硬盘空间充足,你可以在一个 IP 下放置任意数量的站点。
本教程以配置 2 个站点(即 2 个域名)为例进行说明。如果需要配置 n 个站点,只需相应增加和调整配置即可。假设环境如下:
IP地址: 202.55.1.100
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2
配置 Nginx Virtual Hosting 的基本思路与步骤如下:
首先,将两个站点 example1.com 和 example2.com 的网页文件放置在 Nginx 可访问的目录 /www/ 下。其次,为每个站点分别创建独立的 Nginx 配置文件,例如 example1.com.conf 和 example2.com.conf,并将这些文件存放至 /etc/nginx/vhosts/ 目录中。最后,在 /etc/nginx.conf 主配置文件中添加一行 include 指令,使用通配符 * 将所有步骤二中创建的配置文件包含进来。
完成上述配置后,重启 Nginx 服务使更改生效。
以下是具体的操作流程。
具体配置过程详解如下:
第一步,在 /etc/nginx 目录下创建 vhosts 子目录。
mkdir /etc/nginx/vhosts
第二步,在 /etc/nginx/vhosts/ 目录下创建一个名为 example1.com.conf 的文件,并将以下配置内容复制进去:
server {
listen 80;
server_name example1.com www. example1.com;
access_log /www/access_ example1.log main;
location / {
root /www/example1.com; index index.php index.html index.htm;
}
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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example1.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht { deny all; } }
第三步,在 /etc/nginx/vhosts/ 目录下创建一个名为 example2.com.conf 的文件,并将以下配置内容复制进去:
server {
listen 80;
server_name example2.com www. example2.com;
access_log /www/access_ example1.log main;
location /
{
root /www/example2.com; index index.php index.html index.htm;
}
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$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/example2.com/$fastcgi_script_name;
include fastcgi_params;
}
location ~ /.ht { deny all; } }
第四步,打开 /etc/nginx.conf 主配置文件,在适当位置加入 include 指令,以便将上述两个虚拟主机配置文件包含进来。
user nginx; worker_processes 1; # main server error log error_log /var/log/nginx/error.log ; pid /var/run/nginx.pid; events { worker_connections 1024; } # main server config 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"'; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; server { listen 80; server_name _; access_log /var/log/nginx/access.log main; server_name_in_redirect off; location / { root /usr/share/nginx/html; index index.html; } } # 包含所有的虚拟主机的配置文件 include /usr/local/etc/nginx/vhosts/*; }
第五步,重启 Nginx 服务。
执行命令:/etc/init.d/nginx restart