系统相关
首页 > 系统相关> > nginx虚拟主机的搭建

nginx虚拟主机的搭建

作者:互联网

nginx虚拟主机实战

基于nginx部署网站

虚拟主机指的就是一个独立的站点,具有独立的域名,有完整的www服务,例如网站、FTP、邮件等。

Nginx支持多虚拟主机,在一台机器上可以运行完全独立的多个站点。

单虚拟主机(静态资源网站)

nginx.conf

[root@web-7 ~]#vim /etc/nginx/nginx.conf 
[root@web-7 ~]#cat /etc/nginx/nginx.conf 

user  www;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

www.linux0224.cc

[root@web-7 /etc/nginx/conf.d]#vim www.linux0224.conf 
[root@web-7 /etc/nginx/conf.d]#cat www.linux0224.conf 
server {
    listen       80;
    server_name www.linux0224.cc;
    charset utf-8;
    location / {
        root   /usr/share/nginx/html/;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

[root@web-7 /www]#systemctl restart nginx

放入数据

[root@web-7 /usr/share/nginx/html]#wget https://pic.qqtn.com/up/2016-1/2016012713353349001.jpg

[root@web-7 /usr/share/nginx/html]#echo '我是Linux' > 1.txt

[root@web-7 ~]#cat > /usr/share/nginx/html/index.html <<EOF
<meta charset=utf-8>
我是阿飞
EOF

静态文本数据
若是text类型,nginx能够提供读取;

若是其他类型,nginx提供下载功能;

基于IP多虚拟主机

10.0.0.77 /www/77/index.html
10.0.0.78 /www/78/index.html

1、添加网卡
[root@web-7 ~]#ip addr add 10.0.0.77/24 dev eth0
[root@web-7 ~]#ip addr add 10.0.0.78/24 dev eth0

[root@web-7 ~]#ping 10.0.0.77
PING 10.0.0.77 (10.0.0.77) 56(84) bytes of data.
64 bytes from 10.0.0.77: icmp_seq=1 ttl=64 time=0.012 ms
^C
--- 10.0.0.77 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.012/0.012/0.012/0.000 ms
[root@web-7 ~]#ping 10.0.0.78
PING 10.0.0.78 (10.0.0.78) 56(84) bytes of data.
64 bytes from 10.0.0.78: icmp_seq=1 ttl=64 time=0.010 ms
64 bytes from 10.0.0.78: icmp_seq=2 ttl=64 time=0.021 ms
^C
--- 10.0.0.78 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.010/0.015/0.021/0.006 ms


2、子配置文件
[root@web-7 ~]#vim  /etc/nginx/conf.d/78.conf
[root@web-7 ~]#vim  /etc/nginx/conf.d/77.conf
[root@web-7 ~]#cat   /etc/nginx/conf.d/77.conf
server {

listen 10.0.0.77:80;
server_name _;

location /  {
	root  /www/77/;
	index  index.html;
}

}
[root@web-7 ~]#cat   /etc/nginx/conf.d/78.conf
server {

listen 10.0.0.78:80;
server_name _;

location /  {
	root  /www/78/;
	index  index.html;
}

}


测试

[root@web-7 ~]#mkdir /www
[root@web-7 ~]#cd /www
[root@web-7 /www]#mkdir 77
[root@web-7 /www]#mkdir 78
[root@web-7 /www]#ls
77  78
[root@web-7 /www]#tree /www
/www
├── 77
│   └── index.html
└── 78
    └── index.html

2 directories, 2 files

[root@web-7 /www]#systemctl restart nginx

基于域名的多虚拟主机

blog.linux0224.cc /www/blog/index.html
movie.linux0224.cc /www/movie/index.html

基于多IP的虚拟主机可能会造成IP地址不足的问题,如果没有特殊需求,更常用的是基于多域名的形式。

只需要你单独配置DNS服务器,将主机名对应到正确的IP地址,修改Nginx配置,可以识别到不同的主机即可,这样就可以使得多个虚拟主机用同一个IP,解决了IP不足的隐患。

1、配置子配置文件
[root@web-7 /etc/nginx/conf.d]#cat blog.linux0224.conf 
server {

    listen 80;
    server_name blog.linux0224.cc;
	charset utf-8;
    location /  {
        root  /www/blog/;
        index  index.html;
    }

}
[root@web-7 /etc/nginx/conf.d]#cat movie.linux0224.conf
server {

    listen 80;
    server_name movie.linux0224.cc;
	charset utf-8;
    location /  {
        root  /www/movie/;
        index  index.html;
    }

}

重启
[root@web-7 /www]#systemctl restart nginx
测试
本地域名解析
10.0.0.7 blog.linux0224.cc
10.0.0.7 movie.linux0224.cc

echo "我是盖伦" > /www/blog/index.html
echo 'I am 78 movie' > /www/movie/index.html

基于多端口的虚拟主机

10.0.0.8:81 /www/81/index.html
10.0.0.8:82 /www/82/index.html

[root@web-7 /etc/nginx/conf.d]#cat blod.movie.linux0224.conf 
server {

    listen 10.0.0.7:81;
    server_name _;
	charset utf-8;
    location /  {
        root  /www/81/;
        index  index.html;
    }

}

server {

    listen 10.0.0.7:82;
    server_name _;
	charset utf-8;
    location /  {
        root  /www/82/;
        index  index.html;
    }

}

重启
[root@web-7 ~]#systemctl restart nginx

测试

[root@web-7 /www]#mkdir {81,82}
[root@web-7 /www]#echo "我是81,哈哈哈" > 81/index.html
[root@web-7 /www]#echo "我是82,啦啦啦" > 82/index.html
[root@web-7 /www]#

标签:web,www,虚拟主机,index,nginx,html,root,搭建
来源: https://www.cnblogs.com/Xafei/p/16479242.html