当前位置:   article > 正文

Nginx虚拟主机配置

Nginx虚拟主机配置

目录

1.什么是虚拟主机?

2.准备工作(本机IP:10.12.153.222)

3.基于端口配置

4.基于IP配置

5.基于域名配置


1.什么是虚拟主机?

虚拟主机是一种特殊的软硬件技术,它可以将网络上的每一台计算机分成多个虚拟主机,每个虚拟主机可以独立对外提供www服务,这样就可以实现一台主机对外提供多个web服务,每个虚拟主机之间是独立的,互不影响。

nginx可以实现虚拟主机的配置,nginx支持三种类型的虚拟主机配置。

1、基于域名的虚拟主机 (server_name来区分虚拟主机——应用:外部网站)

2、基于ip的虚拟主机, (一台主机绑定多个ip地址)

3、基于端口的虚拟主机 (端口来区分虚拟主机——应用:公司内部网站,外部网站的管理后台)

2.准备工作(本机IP:10.12.153.222)

1.查看nginx是否启动

2.在/usr/share/nginx/html/下创建aaa,bbb,ccc三个目录并在aaa,bbb,ccc下分别创建index.html文件并写入不同的内容用来测试虚拟主机是否部署成功

3.基于端口配置

vim /etc/nginx/conf.d/nginx.conf(若没有自己创建)

  1. server {
  2. listen 80; #端口
  3. server_name localhost; #域名
  4. location /
  5. {
  6. root /usr/share/nginx/html/aaa;
  7. index index.html index.htm;
  8. }
  9. }
  10. server {
  11. listen 90;
  12. server_name localhost;
  13. location /
  14. {
  15. root /usr/share/nginx/html/bbb;
  16. index index.html index.htm;
  17. }
  18. }
  19. server {
  20. listen 100;
  21. server_name localhost;
  22. location /
  23. {
  24. root /usr/share/nginx/html/ccc;
  25. index index.html index.htm;
  26. }
  27. }

浏览器访问IP:80  IP:90  IP:100 

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

4.基于IP配置

1.为服务器添加两个IP

ip a a 10.12.153.220/24 dev ens33

ip a a 10.12.153.221/24 dev ens33

  1. server {
  2. listen 10.12.153.220:80; #端口
  3. server_name localhost; #域名
  4. location /
  5. {
  6. root /usr/share/nginx/html/aaa;
  7. index index.html index.htm;
  8. }
  9. }
  10. server {
  11. listen 10.12.153.221:80;
  12. server_name localhost;
  13. location /
  14. {
  15. root /usr/share/nginx/html/bbb;
  16. index index.html index.htm;
  17. }
  18. }
  19. server {
  20. listen 10.12.153.222:80;
  21. server_name localhost;
  22. location /
  23. {
  24. root /usr/share/nginx/html/ccc;
  25. index index.html index.htm;
  26. }
  27. }

浏览器访问10.12.153.220,10.12.153.221,10.12.153.222

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

5.基于域名配置

  1. server {
  2. listen 80; #端口
  3. server_name www.test.com; #域名
  4. location /
  5. {
  6. root /usr/share/nginx/html/aaa;
  7. index index.html index.htm;
  8. }
  9. }
  10. server {
  11. listen 80;
  12. server_name www.test1.com;
  13. location /
  14. {
  15. root /usr/share/nginx/html/bbb;
  16. index index.html index.htm;
  17. }
  18. }
  19. server {
  20. listen 80;
  21. server_name www.test2.com;
  22. location /
  23. {
  24. root /usr/share/nginx/html/ccc;
  25. index index.html index.htm;
  26. }
  27. }

#注意 使用域名时需本地解析

Windows解析路径

C:\Windows\System32\drivers\etc\hosts

10.12.153.222  www.test.com  

10.12.153.222  www.test1.com 

10.12.153.222  www.test2.com

浏览器分别访问www.test.com,www.test1.com,www.test2.com

若分别出现aaa,bbb,ccc下index.html的内容为部署成功

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号