一、將域名綁定到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配置文件一定要兩者都有,瀏覽器訪問時,才會訪問到對應目錄。(按照上面的步驟來操作,就沒問題了)