Nginx配置之基于域名的虚拟主机
2016-05-11 08:53
676 查看
1、配置好DNS解析
[root@server ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.2 (Santiago)
[root@server ~]# uname -r
2.6.32-220.el6.i686
[root@server ~]# yum install bind* -y
[root@server ~]# vim /etc/named.conf
[root@server ~]# cat /etc/named.conf
[plain] view plain copy
print?
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { any; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "sxkeji.com.cn" IN {
type master;
file "sxkeji.com.cn.zone";
};
[root@server ~]# cp /var/named/named.localhost /var/named/sxkeji.com.cn.zone
[root@server ~]# vim /var/named/sxkeji.com.cn.zone
[root@server ~]# cat /var/named/sxkeji.com.cn.zone
[plain] view plain copy
print?
$TTL 1D
@ IN SOA sxkeji.com.cn rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
@ A 192.168.10.199
www A 192.168.10.199
mail A 192.168.10.199
[root@server ~]# service named restart
测试DNS解析是否成功
[root@server ~]# host www.sxkeji.com.cn
www.sxkeji.com.cn has address 192.168.10.199
[root@server ~]# host mail.sxkeji.com.cn
mail.sxkeji.com.cn has address 192.168.10.199
[root@server ~]#
2、配置Nginx虚拟主机
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server ~]# grep -vE "#|^$" /usr/local/nginx/conf/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;
}
}
server {
listen 80;
server_name mail.sxkeji.com.cn;
location / {
root /usr/local/nginx/html/mail.sxkeji.com;
index index.html index.htm;
}
}
include /usr/local/nginx/conf/vhosts/sxkeji.conf;
#include这里是模块化的,把虚拟主机独立成一个配置文件
#上面斜体server部分是在主配置文件中直接实现虚拟主机
}
[root@server ~]# cat /usr/local/nginx/conf/vhosts/sxkeji.conf #vhosts目录需要自己建立,主页目录也是需要自己建立的
[plain] view plain copy
print?
server {
listen 80;
server_name www.sxkeji.com.cn;
access_log logs/sxkeji.com.log;
location / {
index index.html;
root /usr/local/nginx/html/sxkeji.com;
}
}
[root@server ~]#
3、测试是否成功
[root@server ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` #重启nginx
[root@server ~]# vim /usr/local/nginx/html/sxkeji.com/index.html
[root@server ~]# vim /usr/local/nginx/html/mail.sxkeji.com/index.html
[root@server ~]# cat /usr/local/nginx/html/mail.sxkeji.com/index.html
mail.sxkeji.com.cn
[root@server ~]# cat /usr/local/nginx/html/sxkeji.com/index.html
www.sxkeji.com.cn
[root@server ~]# elinks --dump 127.0.0.1
hello!
[root@server ~]# elinks --dump mail.sxkeji.com.cn
mail.sxkeji.com.cn
[root@server ~]# elinks --dump www.sxkeji.com.cn
www.sxkeji.com.cn
[root@server ~]#
########如此便可以收工了,下次继续基于端口的虚拟主机配置########
Nginx相关
Nginx安装配置:点击打开链接
Nginx虚拟主机配置:点击打开链接
[root@server ~]# cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.2 (Santiago)
[root@server ~]# uname -r
2.6.32-220.el6.i686
[root@server ~]# yum install bind* -y
[root@server ~]# vim /etc/named.conf
[root@server ~]# cat /etc/named.conf
[plain] view plain copy
print?
options {
listen-on port 53 { any; };
listen-on-v6 port 53 { any; };
directory "/var/named";
dump-file "/var/named/data/cache_dump.db";
statistics-file "/var/named/data/named_stats.txt";
memstatistics-file "/var/named/data/named_mem_stats.txt";
allow-query { any; };
recursion yes;
dnssec-enable yes;
dnssec-validation yes;
dnssec-lookaside auto;
/* Path to ISC DLV key */
bindkeys-file "/etc/named.iscdlv.key";
};
logging {
channel default_debug {
file "data/named.run";
severity dynamic;
};
};
zone "." IN {
type hint;
file "named.ca";
};
zone "sxkeji.com.cn" IN {
type master;
file "sxkeji.com.cn.zone";
};
[root@server ~]# cp /var/named/named.localhost /var/named/sxkeji.com.cn.zone
[root@server ~]# vim /var/named/sxkeji.com.cn.zone
[root@server ~]# cat /var/named/sxkeji.com.cn.zone
[plain] view plain copy
print?
$TTL 1D
@ IN SOA sxkeji.com.cn rname.invalid. (
0 ; serial
1D ; refresh
1H ; retry
1W ; expire
3H ) ; minimum
NS @
@ A 192.168.10.199
www A 192.168.10.199
mail A 192.168.10.199
[root@server ~]# service named restart
测试DNS解析是否成功
[root@server ~]# host www.sxkeji.com.cn
www.sxkeji.com.cn has address 192.168.10.199
[root@server ~]# host mail.sxkeji.com.cn
mail.sxkeji.com.cn has address 192.168.10.199
[root@server ~]#
2、配置Nginx虚拟主机
[root@server ~]# vim /usr/local/nginx/conf/nginx.conf
[root@server ~]# grep -vE "#|^$" /usr/local/nginx/conf/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;
}
}
server {
listen 80;
server_name mail.sxkeji.com.cn;
location / {
root /usr/local/nginx/html/mail.sxkeji.com;
index index.html index.htm;
}
}
include /usr/local/nginx/conf/vhosts/sxkeji.conf;
#include这里是模块化的,把虚拟主机独立成一个配置文件
#上面斜体server部分是在主配置文件中直接实现虚拟主机
}
[root@server ~]# cat /usr/local/nginx/conf/vhosts/sxkeji.conf #vhosts目录需要自己建立,主页目录也是需要自己建立的
[plain] view plain copy
print?
server {
listen 80;
server_name www.sxkeji.com.cn;
access_log logs/sxkeji.com.log;
location / {
index index.html;
root /usr/local/nginx/html/sxkeji.com;
}
}
[root@server ~]#
3、测试是否成功
[root@server ~]# kill -HUP `cat /usr/local/nginx/logs/nginx.pid` #重启nginx
[root@server ~]# vim /usr/local/nginx/html/sxkeji.com/index.html
[root@server ~]# vim /usr/local/nginx/html/mail.sxkeji.com/index.html
[root@server ~]# cat /usr/local/nginx/html/mail.sxkeji.com/index.html
mail.sxkeji.com.cn
[root@server ~]# cat /usr/local/nginx/html/sxkeji.com/index.html
www.sxkeji.com.cn
[root@server ~]# elinks --dump 127.0.0.1
hello!
[root@server ~]# elinks --dump mail.sxkeji.com.cn
mail.sxkeji.com.cn
[root@server ~]# elinks --dump www.sxkeji.com.cn
www.sxkeji.com.cn
[root@server ~]#
########如此便可以收工了,下次继续基于端口的虚拟主机配置########
Nginx相关
Nginx安装配置:点击打开链接
Nginx虚拟主机配置:点击打开链接
相关文章推荐
- 轻松使用Nginx搭建web服务器
- Nginx请求转发,静态文件处理
- Nginx 安装成Windows 服务
- nginx配置简单图片显示
- php修改php.ini重启nginx php.ini设置不生效
- Nginx try_files
- nginx配置alias和root的区别
- nginx php-fpm 小VPS 优化
- Linux下安装nginx
- 脚本控制nginx的启动,关闭,重载等
- nginx的启动 重启 关闭
- nginx location优先级详解
- Nginx部署Thinkphp 404错误PathInfo的解决方案
- 配置Nginx+PHP的正确思路与过程
- Nginx + CGI/FastCGI + C/Cpp
- nginx 并发数问题思考:worker_connections,worker_processes与 max clients
- nginx虚拟主机配置
- 使用nginx支持utf8中文
- nginx安装详解
- Nginx 获取真实 IP 方案