Windows系统如何在本地(localhost)配置Nginx支持多个网站?

说明

1.为提高网站创建效率和演示效率,文中用于演示的网站均采用当前流行的内容管理系统(CMS)WordPress平台创建

2. 在本地(localhost)配置Nginx支持多域名前,应确保本地已经成功支持了一个本地网站的成功运行。

支持一个本地(localhost)站点的Nginx配置文件内容:​​​​​​​

#user  nobody;
worker_processes  auto;
events {
    worker_connections  1024;
    multi_accept on;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        off;
    tcp_nopush     on;
    client_max_body_size 200m;
    #默认支持的站点
    server {
        listen       80 default_server;
        server_name  localhost;
        location / {
            root   html;
            #include wordpress.conf; #这里加载支持wordpress网站修改固定链接的配置
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   html;
        }
        # 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 $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

正题:

先预设两个模拟站点

1.用来测试用的两个模拟域名(模拟域名可以自行随意命名)

siliconcircuit.asiaicon-default.png?t=M85Bhttps://siliconcircuit.asia

jianwangzhan.onlineicon-default.png?t=M85Bhttps://jianwangzhan.online

对于Windows系统:

  1. 先进入下面这个地址:C:\Windows\System32\drivers\etc
  2. 打开hosts文件,在文件最下面添加前面自定义的两个模拟域名​​​​​​​

 

3. 进入Nginx的安装目录,打开conf文件夹下的nginx.conf配置文件

在http的默认server{}后面再添加两个新的server{},两个新server的内容分别是:

#新添加的模拟域名站点1:siliconcircuit.asia
    server {
        listen    81;
        server_name  siliconcircuit.asia;
     
        root  D:/WNMP/Nginx/html/mulitiWordPress/siliconcircuit.asia/;
        index   index.php index.html index.htm;
     
        location / {
          allow  all;
        }
    }
#新添加的模拟域名站点2:jianwangzhan.online
    server {
        listen    82;
        server_name  jianwangzhan.online;
     
        root  D:/WNMP/Nginx/html/mulitiWordPress/jianwangzhan.online/;
        index   index.php index.html index.htm;
     
        location / {
          allow  all;
        }
    }

4. 最终的整个nginx.conf配置文件内容如下

#user  nobody;
worker_processes  auto;
events {
    worker_connections  1024;
    multi_accept on;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        off;
    tcp_nopush     on;
    client_max_body_size 200m;
    #默认支持的站点
    server {
        listen       80 default_server;
        server_name  localhost;
        root   html;
        location / {
            #include wordpress.conf; #这里加载支持wordpress网站修改固定链接的配置
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
           root   html;
        }

        # 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  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    #新添加的模拟域名站点1:siliconcircuit.asia
    server {
        listen    81;
        server_name  siliconcircuit.asia;
     
        root  D:/WNMP/Nginx/html/mulitiWordPress/siliconcircuit.asia/;
        index   index.php index.html index.htm;
     
        location / {
          allow  all;
        }
    }
    #新添加的模拟域名站点2:jianwangzhan.online
    server {
        listen    82;
        server_name  jianwangzhan.online;
     
        root  D:/WNMP/Nginx/html/mulitiWordPress/jianwangzhan.online/;
        index   index.php index.html index.htm;
     
        location / {
          allow  all;
        }
    }
}

5. 重启Nginx服务,即可同时支持多个网站

访问:siliconcircuit.asia/mulitiWordPress/siliconcircuit.asia/index.php

访问:jianwangzhan.online/mulitiWordPress/jianwangzhan.online/index.php

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值