码迷,mamicode.com
首页 > 其他好文 > 详细

nginx基于域名的虚拟主机配置

时间:2018-01-14 20:21:14      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:mat   主机   gen   pac   测试   add   服务   user   name   

与apache服务器类似,nginx也有基于域名,IP及端口的虚拟主机配置,在实际工作场景中,基于域名的虚拟主机配置较常见。
nginx服务的主要配置文件nginx.conf
[root@lnmp01 conf]# ls -l nginx.conf
-rw-r--r-- 1 root root 2788 Jan 14 17:41 nginx.conf
[root@lnmp01 conf]# pwd
/application/nginx/conf

去掉注释及空行后的配置文件
[root@lnmp01 conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

默认虚拟主机为localhost,
这里配置虚拟,可以配置两个,中间用空格隔开
如 server_name www.tuwei.com tuwei.com
配置完后如下
[root@lnmp01 conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.tuwei.org tuwei.org;
location / {
root html/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

配置站点文件
[root@lnmp01 conf]# echo "hi.Im tuwei,my 51cto‘s blog is http://blog.51cto.com/tuwei">../html/www/index.html
将IP和域名加到本地hosts文件中
echo "192.168.132.20 www.tuwei.org">>/etc/hosts
检查语法
sbin/nginx -t
重启nginx服务
[root@lnmp01 conf]# kill -HUP cat ../logs/nginx.pid
或者用
../sbin/nginx -c /application/nginx/conf/nginx.conf
../sbin/nginx -s reload

本机测试
[root@lnmp01 conf]# curl www.tuwei.org
hi.Im tuwei,my 51cto‘s blog is http://blog.51cto.com/tuwei
如果在电脑上访问该域名,需要在电脑hosts文件中作解析。

基于端口的虚拟主机----次重要

基于IP的虚拟主机------不重要----企业一般用负载均衡配ip

listen IP:80;
server_name IP;

  nginx日志

worker_processes 1;

error_log /application/logs/err.log crit;------->crit为日志级别
events {
use epoll;------------------------------->
worker_connections 1024;
}

日志格式
配置文件中有,去掉注释
log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;

访问日志
location / {
root html/www;
index index.html index.htm;
}
access_log /application/logs/host.access.log main;-------->
可以用命令awk ‘{print $1}‘ host.access.log |sort|uniq -c|sort -rn -k1 通过日志分析访问网站的IP情况

nginx基于域名的虚拟主机配置

标签:mat   主机   gen   pac   测试   add   服务   user   name   

原文地址:http://blog.51cto.com/tuwei/2060825

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!