1.背景:
虚拟主机:一台web主机配置多个站点,每个站点,希望用不同的域名和站点目录,或者是不同的端口,或者是不同的IP。
假设网站的域名为:25linux.com,网站下面设有
;;
;;
;
三个站点,这样我们可以在一台nginx主机上配置虚拟主机来实现。
通常虚拟主机分为3种:
基于域名,基于端口,基于IP,以及它们的混合来实现,我这里以基于域名的方式来创建虚拟主机。
2.创建站点目录:
# 我打算以nginx的默认root目录来创建www、blog、bbs三个网站的目录,# 切换到nginx的root目录,默认为Root "/usr/local/nginx/html"[root@web ~]# cd /usr/local/nginx/html/# 创建www,bbs,blog三个目录:[root@web html]# mkdir {www,bbs,blog}# 给三个站点分别创建index.html页面:[root@nginx html]# touch {www,blog,bbs}/index.html# for name in www bbs blog;do echo "http://$name.25linux.com" >/usr/local/nginx/html/$name/index.html;done [root@webhtdocs]# cat bbs/index.htmlhttp://bbs.25linux.com[root@webhtdocs]# cat www/index.htmlhttp://www.25linux.com[root@webhtdocs]# cat blog/index.htmlhttp://blog.25linux.com
3.配置nginx.conf文件
我们把nginx.conf主配置文件中的server段的配置给提取出来,放到单独的文件中,就像apache的虚拟主机配置一样,我这里要建3个站点所以使用3个配置文件www_server.conf;include bbs_server.conf;include bbs_server.conf
[root@nginx nginx]# tail -84 nginx.conf >www_server.conf[root@nginx nginx]# tail -84 nginx.conf >bbs_server.conf[root@nginx nginx]# tail -84 nginx.conf >blog_server.conf
注意:需要删除最后一行的“}”号。然后在主配置文件中删除所有的server段,
在http段中加入include www_server.conf;include bbs_server.conf;include bbs_server.conf三个配置文件,
[root@nginx nginx]# vim nginx.conf#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;include www_server.conf;include bbs_server.conf;include bbs_server.conf; }
4.配置虚拟主机
我们分别在www_server.conf;include bbs_server.conf;include bbs_server.conf配置文件中配置三个虚拟主机
先检查一下语法[root@nginx nginx]# /usr/local/nginx/sbin/nginx-tnginx: the configuration file/etc/nginx/nginx.conf syntax is oknginx: configuration file/etc/nginx/nginx.conf test is successful
4.1. 编辑www站点server.conf配置文件
[root@nginx nginx]# vim www_server.conf server { listen 80; server_name www.25linux.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html/www; index index.phpindex.html index.htm; } #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; } # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location~ \.php$ { fastcgi_pass 127.0.0.1:9000; root html/www; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
4.2. 编辑bbs站点server.conf配置文件
[root@nginx nginx]# vim bbs_server.confserver { listen 80; server_name bbs.25linux.com; #charset koi8-r; #access_log logs/host.access.log main; location / { root html/bbs; index index.php index.htmlindex.htm; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; root html/bbs; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
4.3. 编辑blog站点server.conf配置文件
server { listen 80; server_name blog.25linux.com; root html/blog; #charset koi8-r; #access_log logs/host.access.log main; location / { index index.php index.htmlindex.htm; } #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; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; root html/blog; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$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; #} }
修改后,再次检查语法:[root@nginx nginx]# /usr/local/nginx//sbin/nginx-tnginx: the configuration file/etc/nginx/nginx.conf syntax is oknginx:configuration file /etc/nginx/nginx.conf test is successful最后重新加载nginx:[root@nginx nginx]# service nginx reloadnginx: the configuration file /etc/nginx/nginx.confsyntax is oknginx: configuration file/etc/nginx/nginx.conf test is successfulReloadingnginx: [ OK ]
5.编辑本地hosts文件,添加dns记录
由于用的是测试域名,没有经过dns服务器解析,所以需要在windows的机器上的hosts文件添加dns记录,电脑才能通过域名访问:
6.然后浏览器访问域名:
nginx虚拟主机配置成功,下面我们来搭建3个虚拟主机的站点。