如何配置Nginx使本地(localhost)LNMP网站环境支持多个域名?

说明:请确保LNMP环境已经搭建成功,且能正常支持网站的创建和浏览

其他说明:

文章用 Debian 11作为演示系统

Nginx在Debian系统中的默认安装地址:/etc/nginx 

Debian系统默认的网站部署根目录:/var/www/html

文章测试用的两个预设测试域名:

siliconcircuit.asia(https://www.chufeng.vip)
jianwangzhan.online

-----------------------------------------------------正文------------------------------------------------------------

1. 在系统中注册两个新的假域名

a.打开hosts文件,

sudo nano /etc/hosts

b.在hosts文件的最后添加下面两行,保存关闭

127.0.0.1        siliconcircuit.asia
127.0.0.1        jianwangzhan.online

2. 在Nginx安装目录中的conf.d目录下中新建两个新的配置文件:sitea.conf和siteb.conf

sitea.conf的内容:

server{
        listen 81;
        listen [::]:81;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name siliconcircuit.asia;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

         # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                           include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
}

siteb.conf的内容:

server{
        listen 82;
        listen [::]:82;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name jianwangzhan.online;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

         # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
                           include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
        }
}

3. 在部署网站的根目录中新建一个多域名测试目录:mulitiWordPress

sudo mkdir /var/www/html/mulitiWordPress

4. 在新目录mulitiWordPress目录下新建两个方式测试网站内容的目录:siliconcircuit.asia和jianwangzhan.online

并在其中创建一个index.php测试文件

a. 新建siliconcircuit.asia目录

sudo mkdir /var/www/html/mulitiWordPress/siliconcircuit.asia

 在siliconcircuit.asia目录下创建index.php测试文件

sudo nano /var/www/html/mulitiWordPress/siliconcircuit.asia/index.php

index.php文件的内容为:

<?php 

echo "Welcome! This is the second test domain-->siliconcicuit.asia";

phpinfo(); 

?>

 b. 新建jianwangzhan.online目录

sudo mkdir /var/www/html/mulitiWordPress/jianwangzhan.online

 在jianwangzhan.online目录下创建index.php测试文件

sudo nano /var/www/html/mulitiWordPress/jianwangzhan.online/index.php

index.php文件的内容为:

<?php 

echo "Welcome! This is the second test domain-->jianwangzhan.online";

phpinfo(); 

?>

5. 重启Nginx服务

sudo systemctl restart nginx

6. 测试效果

在浏览器地址栏输入:siliconcircuit.asia/mulitiWordPress/siliconcircuit.asia/index.php来测试域名siliconcircuit.asia

0e526041544e4ab380e76460fa0e6e11.png

在浏览器地址栏输入:jianwangzhan.online/mulitiWordPress/jianwangzhan.online/index.php来测试域名jianwangzhan.online

e46eebfda27a41048a21ce2fb2d8ec60.png

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Nginx配置多个域名,您可以按照以下步骤进行操作: 1. 打开Nginx配置文件 /etc/nginx/nginx.conf。 2. 在http块中添加多个server块,每个server块用来配置一个域名。 3. 在每个server块中,使用listen指令指定监听的端口和域名。 4. 在每个server块的location块中,使用proxy_pass指令将请求转发到相应的本地端口。 以下是一个示例配置,假设您要配置两个域名 domain1.com和 domain2.com: ``` http { include /etc/nginx/mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; # 配置 domain1.com server { listen 80; server_name domain1.com www.domain1.com; location / { proxy_pass http://localhost:8001; } } # 配置 domain2.com server { listen 80; server_name domain2.com; location / { proxy_pass http://localhost:8002; } } } ``` 请注意,以上配置中的localhost:8001和localhost:8002是示例本地端口,您需要根据实际情况修改为相应的端口。 另外,在域名服务器上还需要将不同的域名解析到Nginx服务器的IP地址上,这可以通过修改域名解析配置或在本地进行hosts文件配置来实现。 希望以上信息对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [nginx配置多个服务域名](https://blog.csdn.net/qq_42892856/article/details/130821302)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [nginx如何配置多个域名访问](https://blog.csdn.net/qq_38378384/article/details/92839567)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值