1、什么是虚拟主机
简单的理解就是如果我们没有虚拟主机,我们每有一个新的站点之后,我们就需要再起一个web服务,但是有了虚拟主机后,我们可以通过比如域名来区分访问的页面,这样可以很简单的新建站点,在apache和nginx两个服务中都有相应的配置方法,但是apache可能会稍微麻烦点,nginx就比较简单了,而且nginx的高并发的能力加持,现在个人感觉nginx比apache火。
2、配置nginx虚拟主机
Nginx虚拟主机个人总结有一个要点就是一个server标签一个站点,或者一个服务(反向代理后端服务)
修改配置之前我们需要先备份主配置文件:
cp nginx.conf nginx.conf.bak
由于nginx配置文件本身有很多配置被注释的,而且我们这里可能也用不到,所以这里去掉注释内容和空行
egrep -v "#|^$" nginx.conf.bak > nginx.confegrep :是grep的扩展,如果想使用grep调用扩展正则表达式可以使用-e参数,-v是去除、排除的意思
默认配置:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/nginx.conf.d/*.conf;
}
# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/nginx.conf.d/*.conf;
#blog======================================
server {
listen 80;
server_name blog.example.com;
location / {
root /opt/html/blog;
index index.html index.htm;
}
}
#bbs=======================================
server {
listen 80;
server_name bbs.example.com;
location / {
root /opt/html/bbs;
index index.html index.htm;
}
}
}
上面的配置说明:
listen:监听的端口
server_name:监听的域名
root:表示站点的目录
index:表示解析的首页文件,比如网页除了html 还有php、asp等。
创建站点文件:
# cd /opt/
# mkdir html/{blog,bbs} -p
# ll html/
总用量 8
drwxr-xr-x. 2 root root 4096 12月 23 08:46 bbs
drwxr-xr-x. 2 root root 4096 12月 23 08:46 blog
# echo "bbs" > html/bbs/index.html
# echo "blog" > html/blog/index.html
# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx restart
停止 nginx: [确定]
正在启动 nginx: [确定]
如果是编译安装检查配置文件是否正确的方法是-t
参考:
/usr/local/nginx/sbin/nginx -t
注意:大括号必须是成对的。
测试:
提示:由于这里测试涉及到域名解析问题,最简单的方法就是利用本地hosts,解析优先级:缓存> hosts>dns
3、优化
一般生产中,是绝对不会把虚拟主机都放在一个配置文件中的,因为这样极易引起混乱,管理很困难。所以管理配置文件一般都会分类管理:分项目、前后端、网站类型、服务等方法。
# pwd
/etc/nginx
# mkdir nginx.conf.d/{blog,bbs} -p
#sed -n "23,30p" nginx.conf > nginx.conf.d/blog/blog.conf
# sed -n "23,30p" nginx.conf > nginx.conf.d/blog/blog.conf
# sed -i "22,30d" nginx.conf
# sed -n "23,31p" nginx.conf > nginx.conf.d/bbs/bbs.conf
# sed -i "23,31d" nginx.conf
修改主配置文件为如下结果:
# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
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;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/nginx.conf.d/blog/*.conf;
include /etc/nginx/nginx.conf.d/bbs/*.conf;
}最后的配置文件所在路径用include包含进来即可。
重启服务:
# /etc/init.d/nginx configtest
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx restart
停止 nginx: [确定]
正在启动 nginx: [确定]
最后测试成功即可。
716

被折叠的 条评论
为什么被折叠?



