一、建立網站目錄及檔案:##
[root@localhost data]# tree /data /data └── wwwroot ├── www.1.com_8080 │ └── index.html └── www.1.com_8081 └── 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.8080.conf server{ listen 8080; server_name 1.com www.1.com; index index.html; root /data/wwwroot/www.1.com_8080; }
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8081.conf server{ listen 8081; server_name 1.com www.1.com; index index.html; root /data/wwwroot/www.1.com_8081; }
[root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf server{ listen 80 default_server; #使用default_server指定nginx的默认虚拟主机 deny all; }
五、測試設定檔是否有問題:
[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
#六、當設定檔修改時,可以使用一下指令重新載入設定檔
[root@localhost sbin]# ./nginx -s reload
以上是nginx基於連接埠如何設定虛擬主機的詳細內容。更多資訊請關注PHP中文網其他相關文章!