将域名绑定到ip上,并实现访问不同二级子域名对应不同目录

一、将域名绑定到ip上

1、环境介绍:阿里云服务器ESC(美国硅谷)

2、购买域名

3、备案

注:由于我买的是美国地区服务器,所以不用备案,如果买的国内服务器,这里需要添加一个备案操作。

4、域名实名认证

5、将域名绑定到云服务器公网ip

阿里云官网域名解析地址:https://dc.console.aliyun.com/next/index#/domain/list/all-domain

5.1、对购买的域名进行解析(点击解析按钮)


5.2、进入解析域名页面后,点击添加记录按钮


看上图,可以清晰的看出来,我绑定了5个子域名,现在访问子域名,就直接访问我的ip所在的云服务器了。

到此,将域名绑定到ip地址的操作就完成了,剩下的就需要在我们云服务器上进行相应操作了。



二、实现浏览器访问不同子域名,服务器进入不同子目录

环境介绍:centos7.4、LNMP

注:上面将域名解析后,如果我们不在本地nginx进行配置,访问的就是nginx的默认目录,为了实现不同子域名对应不同目录,我们就需要对nginx进行相应SERVER的配置。

1、使用的是yum安装的nginx,这里直接配置nginx子配置文件就可以了。( /etc/nginx/conf.d 文件夹下)


注:yum安装nginx  https://blog.csdn.net/m_nanle_xiaobudiu/article/details/80640293


在这里,我将www.a.xiaobudiu.conf 和 www.b.xiaobudiu.conf 代码展示出来。

www.a.xiaobudiu.top.conf 代码:

server {
    listen       80;
    server_name  www.a.xiaobudiu.top;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /data/main;
        index  index.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   /usr/share/nginx/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$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #}
}

www.b.xiaobudiu.top.conf 代码:

server {
    listen       80;
    server_name  www.b.xiaobudiu.top;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /data/b;
        index  index.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   /usr/share/nginx/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$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$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;
    #}
}

注:nginx.conf 我没进行改动,yum安装之后是什么样就是什么样。为了测试方便,我这里只对conf.d下的子配置文件进行了更改。


2、到这里,其实配置已经结束了,systemctl restart nginx 重启一下nginx,清除一下浏览器缓存,现在就可以在浏览器中看到效果了。

注意:想要实现浏览器访问不同子域名,对应不同文件夹的效果,域名解析和nginx配置文件一定要两者都有,浏览器访问时,才会访问到对应目录。(按照上面的步骤来操作,就没问题了)



三、效果演示


1、访问www.a.xiaobudiu.top



2、访问www.b.xiaobudiu.top


猜你喜欢

转载自blog.csdn.net/m_nanle_xiaobudiu/article/details/80726900