配置服务器 —— Nginx添加多个二级子域名 --- 2018-06-26

kuaidi.ping-jia.net  作者:佚名   更新日期:2024-06-29
Nginx多文件配置二级子域名(推荐)

首先,在自己的域名控制台中添加解析,这里以添加blog前缀为例

我用的是万网,在 解析设置 中 添加解析

主机记录 :你想要的二级域名 

记录值 :你的IP地址

保存后,我们就完成了第一步把子域名解析到我们的服务器上。

第二步:添加配置文件

进入nginx的 /conf 配置文件夹中,编辑 nginx.conf 文件

[root@iZ2844brz0xZ~]# cd /usr/local/nginx/conf/[root@iZ2844brz0xZ~]# vim nginx.conf

在 http 模块中添加如下语句

include/usr/local/nginx/conf/sites-enabled/*.conf;

告诉这个配置文件要去包含 /sites-enabled 目录下的所有以 .conf 结尾的配置文件。:wq 保存。

此时,我们新建一个 /sites-enabled 文件夹,并在其中添加 blog.***.com.conf 文件

[root@iZ2844brz0xZconf]# mkdir sites-enabled[root@iZ2844brz0xZsites-enabled]# vim blog.***.com.conf

在文件中添加

server {listen80;#监听端口server_name blog.***.com;#绑定域名root /usr/local/nginx/html/blog/;#网站根目录,建议使用绝对路径indexindex.phpindex.htmlindex.htm;#默认文件#添加对php的解析location ~ \.php$ {        fastcgi_pass127.0.0.1:9000;        fastcgi_indexindex.php;        fastcgi_param  SCRIPT_FILENAME$document_root$fastcgi_script_name;        include        /usr/local/nginx/conf/fastcgi_params;    }#添加错误页面,利于搜索引擎收录以及良好的用户体验error_page404/404.html;    location /404.html {        root /usr/local/nginx/html/;    }    error_page500502503504/50x.html;    location =/50x.html {

        root /usr/local/nginx/html/;    }}

内容可自行添加

Nginx单文件配置二级子域名

在 nginx.conf 文件的 server 模块中添加以下语句

if($host~* (\b(?!www\b)\w+)\.\w+\.\w+ ) { set$subdomain/$1;}location / { root html$subdomain;indexindex.htmlindex.phpindex.htmindex;}

即可解析到对应文件夹

最后,重启nginx即可

[root@iZ2844brz0xZsites-enabled]# /usr/local/nginx/sbin/nginx -s reload

来自:https://blog.csdn.net/LBinin/article/details/70188752