Nginx配置简单基于域名的虚拟主机

首先修改hosts文件,让浏览器在看到a.com或是www.a.com的网址时知道上哪里去找:

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost
127.0.0.1 a.com
127.0.0.1 www.a.com

打开nginx.conf,写入下面两段:

    server {
        listen       7777;
        server_name  a.com;

        location / {
            root   html/7777;
            index  index.html index.htm;
        }
    }
    
    server {
        listen       9999;
        server_name  a.com;

        location / {
            root   html/9999;
            index  index.html index.htm;
        }
    }

然后,在C: ginx-1.16.1html创建目录7777,9999,把index.html 拷贝进去,然后修改得能区分开来:

<!DOCTYPE html>
<html>
<head>
<title>Welcome to 7777</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to 7777</h1>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Welcome to 9999</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to 9999</h1>
</html>

之后,平滑重启nginx

C: ginx-1.16.1>nginx.exe -s reload

之后在浏览器器里就能看效果了:

--END-- 2019年12月14日08:22:02

原文地址:https://www.cnblogs.com/heyang78/p/12038057.html