• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    Nginx三种不同类型的虚拟主机的配置(基于域名、IP 和端口)

    发布者: 涵韵 | 发布时间: 2025-6-17 08:17| 查看数: 60| 评论数: 0|帖子模式

    Nginx 是一款高性能的 Web 服务器,支持多种虚拟主机配置方式,能够根据域名、IP 或端口区分不同的站点。这种灵活性让 Nginx 成为搭建多站点服务的首选工具。本文将带你一步步实现三种常见的虚拟主机配置方法:基于域名、基于 IP 和基于端口的虚拟主机。无论你是初学者还是有经验的运维人员,这篇教程都能帮助你快速掌握虚拟主机的配置技巧。
    以下案例演示 是基于源码包安装的nignx (如果你是rpm包 也差不多 只用把路径改为你nginx的路径即可 其他没什么大的变化,如果你是小白请绕道!)

    1. 基于域名的虚拟主机




    步骤 1:准备网站根目录

    为每个域名创建独立的子目录,并添加测试页面:
    1. [root@localhost ~]# mkdir -p /usr/local/nginx/html/site1
    2. [root@localhost ~]# mkdir -p /usr/local/nginx/html/site2

    3. [root@localhost ~]# echo "Welcome to Site 1" > /usr/local/nginx/html/site1/index.html
    4. [root@localhost ~]# echo "Welcome to Site 2" > /usr/local/nginx/html/site2/index.html
    复制代码


    步骤 2:修改 Nginx 配置文件

    打开 Nginx 的配置文件:
    1. [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    复制代码
    1. http
    复制代码
    配置段中添加以下内容:
    注释:如果需要两个虚拟主机 只用将再额外添加一个server即可
    1. # 全局配置
    2. user  nobody;
    3. worker_processes  1;

    4. events {
    5.     worker_connections  1024;
    6. }

    7. http {
    8.     include       mime.types;
    9.     default_type  application/octet-stream;

    10.     sendfile        on;
    11.     keepalive_timeout  65;

    12.     # 基于域名的虚拟主机配置
    13.     server {
    14.         listen       80;
    15.         server_name  www.site1.com;

    16.         # 网站根目录
    17.         root   html/site1;
    18.         index  index.html index.htm;

    19.         # 日志配置
    20.         access_log  logs/site1_access.log;
    21.         error_log   logs/site1_error.log;

    22.         # 主路径配置
    23.         location / {
    24.             try_files $uri $uri/ =404;
    25.         }

    26.         # 状态监控
    27.         location /status {
    28.             stub_status on;
    29.             access_log off;
    30.             allow 192.168.14.112;
    31.             deny all;
    32.         }

    33.         # 错误页面配置
    34.         error_page   404              /404.html;
    35.         error_page   500 502 503 504  /50x.html;
    36.         location = /404.html {
    37.             root   html/site1;
    38.         }
    39.         location = /50x.html {
    40.             root   html;
    41.         }

    42.         # 禁止访问 .ht 文件
    43.         location ~ /\.ht {
    44.             deny all;
    45.         }
    46.     }

    47.     server {
    48.         listen       80;
    49.         server_name  www.site2.com;

    50.         # 网站根目录
    51.         root   html/site2;
    52.         index  index.html index.htm;

    53.         # 日志配置
    54.         access_log  logs/site2_access.log;
    55.         error_log   logs/site2_error.log;

    56.         # 主路径配置
    57.         location / {
    58.             try_files $uri $uri/ =404;
    59.         }

    60.         # 错误页面配置
    61.         error_page   404              /404.html;
    62.         error_page   500 502 503 504  /50x.html;
    63.         location = /404.html {
    64.             root   html/site2;
    65.         }
    66.         location = /50x.html {
    67.             root   html;
    68.         }

    69.         # 禁止访问 .ht 文件
    70.         location ~ /\.ht {
    71.             deny all;
    72.         }
    73.     }
    74. }
    复制代码


    步骤 3:测试配置并重启 Nginx

    测试配置文件语法:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -t
    复制代码
    重启 Nginx 服务:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
    复制代码


    步骤 4:访问测试

    在浏览器中访问:

      1. http://www.site1.com
      复制代码
      ,应显示
      1. Welcome to Site 1
      复制代码

      1. http://www.site2.com
      复制代码
      ,应显示
      1. Welcome to Site 2
      复制代码




    客户端测试

    修改hosts文件(本地dns解析)
    1. [root@localhost ~]# vim /etc/hosts
    复制代码



    2. 基于 IP 的虚拟主机

    步骤 1:准备网站根目录

    为每个 IP 创建独立的子目录,并添加测试页面:
    1. [root@localhost ~]# mkdir -p /usr/local/nginx/html/ip1
    2. [root@localhost ~]# mkdir -p /usr/local/nginx/html/ip2

    3. [root@localhost ~]# echo "Welcome to IP 192.168.14.111" > /usr/local/nginx/html/ip1/index.html
    4. [root@localhost ~]# echo "Welcome to IP 192.168.14.112" > /usr/local/nginx/html/ip2/index.html
    复制代码
    步骤 2:修改 Nginx 配置文件

    打开 Nginx 的配置文件:
    1. [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    复制代码
    1. http
    复制代码
    配置段中添加以下内容:
    1. server {
    2.     listen 192.168.14.111:80;
    3.     server_name 192.168.14.111;
    4.     root html/ip1;  # 使用默认路径的子目录
    5.     index index.html;

    6.     location / {
    7.         try_files $uri $uri/ =404;
    8.     }

    9.     # 错误页面
    10.     error_page 500 502 503 504 /50x.html;
    11.     location = /50x.html {
    12.         root html;  # 默认错误页面路径
    13.     }
    14. }

    15. server {
    16.     listen 192.168.14.112:80;
    17.     server_name 192.168.14.112;
    18.     root html/ip2;  # 使用默认路径的子目录
    19.     index index.html;

    20.     location / {
    21.         try_files $uri $uri/ =404;
    22.     }

    23.     # 错误页面
    24.     error_page 500 502 503 504 /50x.html;
    25.     location = /50x.html {
    26.         root html;  # 默认错误页面路径
    27.     }
    28. }
    复制代码
    步骤 3:测试配置并重启 Nginx

    测试配置文件语法:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -t
    复制代码
    重启 Nginx 服务:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
    复制代码
    步骤 4:访问测试

    在浏览器中访问:

      1. http://192.168.14.111
      复制代码
      ,应显示
      1. Welcome to IP 192.168.14.111
      复制代码

      1. http://192.168.14.112
      复制代码
      ,应显示
      1. Welcome to IP 192.168.14.112
      复制代码

    客户端测试

    因为我在虚拟机测试 只有一个网卡 所以我在虚拟一个网卡 这个你可以忽视 看测试结果即可
    1. ip addr add 192.168.14.110/24 dev ens33
    复制代码


    3. 基于端口的虚拟主机

    步骤 1:准备网站根目录

    为每个端口创建独立的子目录,并添加测试页面:
    1. [root@localhost ~]# mkdir -p /usr/local/nginx/html/port1
    2. [root@localhost ~]# mkdir -p /usr/local/nginx/html/port2

    3. [root@localhost ~]# echo "Welcome to Port 8080" > /usr/local/nginx/html/port1/index.html
    4. [root@localhost ~]# echo "Welcome to Port 9090" > /usr/local/nginx/html/port2/index.html
    复制代码
    步骤 2:修改 Nginx 配置文件

    打开 Nginx 的配置文件:
    1. [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
    复制代码
    1. http
    复制代码
    配置段中添加以下内容:
    1. server {
    2.     listen 8080;
    3.     server_name localhost;
    4.     root html/port1;  # 使用默认路径的子目录
    5.     index index.html;

    6.     location / {
    7.         try_files $uri $uri/ =404;
    8.     }

    9.     # 错误页面
    10.     error_page 500 502 503 504 /50x.html;
    11.     location = /50x.html {
    12.         root html;  # 默认错误页面路径
    13.     }
    14. }

    15. server {
    16.     listen 9090;
    17.     server_name localhost;
    18.     root html/port2;  # 使用默认路径的子目录
    19.     index index.html;

    20.     location / {
    21.         try_files $uri $uri/ =404;
    22.     }

    23.     # 错误页面
    24.     error_page 500 502 503 504 /50x.html;
    25.     location = /50x.html {
    26.         root html;  # 默认错误页面路径
    27.     }
    28. }
    复制代码
    步骤 3:测试配置并重启 Nginx

    测试配置文件语法:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -t
    复制代码
    重启 Nginx 服务:
    1. [root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload
    复制代码
    步骤 4:访问测试

    在浏览器中访问:

      1. http://192.168.14.111:8080
      复制代码
      ,应显示
      1. Welcome to Port 8080
      复制代码

      1. http://192.168.14.111:9090
      复制代码
      ,应显示
      1. Welcome to Port 9090
      复制代码

    客户端测试



    4.总结

    通过本文的详细步骤,我们成功实现了基于域名、IP 和端口的虚拟主机配置。Nginx 的灵活性和高性能使其能够轻松应对多站点服务的需求。这些配置方法不仅适用于日常开发和测试环境,也能在生产环境中提供稳定可靠的服务。
    到此这篇关于Nginx三种不同类型的虚拟主机的配置(基于域名、IP 和端口)的文章就介绍到这了,更多相关Nginx 虚拟主机配置内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    来源:https://www.jb51.net/server/339408okn.htm
    免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表