Centos7下Nginx上配置多域名多站點的方法

一、準備工作

1、一臺服務器,我這裏爲阿里雲的centos7.4
2、兩個域名

二、配置

1、在Nginx配置目錄下,創建一個”vhost”目錄。本例假設Nginx是默認安裝,配置目錄在”/etc/nginx”

mkdir /etc/nginx/vhost

2、創建siteA的配置文件

vim /etc/nginx/vhost/vhost_siteA.conf

3、輸入以下配置信息

server {
  listen    80;            # 監聽端口
  server_name www.siteA.com siteA.com;  # 站點域名
  root /home/user/www/blog;       # 站點根目錄
  index index.html index.htm index.php;  # 默認導航頁
  
  location / {
    # WordPress固定鏈接URL重寫
    if (!-e $request_filename) {
      rewrite (.*) /index.php;
    }
  }
  
  # PHP配置
  location ~ \.php$ {
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
  }
}

4、同siteA一樣創建siteB的配置文件,兩者僅有的不同是”server_name”和”root”目錄
在這裏插入圖片描述
5、打開nginx.conf文件

vim /etc/nginx/nginx.conf

6、將虛擬目錄的配置文件加入到”http {}”裏面,保存退出

include /etc/nginx/vhost/*.conf;

在這裏插入圖片描述

三、重啓,systemctl restart nginx.service

相關命令

systemctl start nginx.service
systemctl stop nginx.service
systemctl reload nginx.service
systemctl status nginx.service
四、效果

在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章