nginx 配置虛擬主機( 基於域名 )


一、創建網站目錄及文件:

[root@localhost data]# tree /data
/data
└── wwwroot
    ├── www.1.com
    │   └── index.html
    └── www.2.com
        └── index.html

二、修改nginx.conf:

[root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    include vhost/*.conf; #vhost目錄下會包含所有的虛擬主機的配置文件
}

三、創建虛擬主機的配置文件目錄:

[root@localhost conf]mkdir /usr/local/nginx/conf/vhost

四、創建虛擬主機配置文件:

[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.conf 
server{
    listen 80;
    server_name 1.com www.1.com;
    index index.html;
    root /data/wwwroot/www.1.com;
}
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.2.com.conf  
server{
    listen 80;
    server_name 2.com www.2.com;
    index index.html;
    root /data/wwwroot/www.2.com;
}
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf          
server{
 listen 80 default_server; #使用default_server指定nginx的默認虛擬主機 deny all;
}

若使用其他域名來訪問虛擬主機時,會匹配到默認虛擬主機,該配置會拒絕未定義的域名的虛擬主機。若不配置該選項,默認排在最前邊的server會成為默認虛擬主機。

五、測試配置文件是否存在問題:

[root@localhost root]# cd /usr/local/nginx/sbin
[root@localhost sbin]# ./nginx -t nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

六、當配置文件修改時,可以使用一下命令重新加載配置文件

[root@localhost sbin]# ./nginx -s reload


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM