一、创建网站目录及文件:

  1. [root@localhost data]# tree /data
  2. /data
  3. └── wwwroot
  4. ├── www.1.com_8080
  5.    └── index.html
  6. └── www.1.com_8081
  7. └── index.html

二、修改nginx.conf:

  1. [root@localhost nginx]# vim /usr/local/nginx/conf/nginx.conf
  2. worker_processes 1;
  3. events {
  4. worker_connections 1024;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. keepalive_timeout 65;
  10. include vhost/*.conf; #vhost目录下会包含所有的虚拟主机的配置文件
  11. }

三、创建虚拟主机的配置文件目录:

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

四、创建虚拟主机配置文件:

  1. [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8080.conf
  2. server{
  3. listen 8080;
  4. server_name 1.com www.1.com;
  5. index index.html;
  6. root /data/wwwroot/www.1.com_8080;
  7. }
  1. [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/www.1.com.8081.conf
  2. server{
  3. listen 8081;
  4. server_name 1.com www.1.com;
  5. index index.html;
  6. root /data/wwwroot/www.1.com_8081;
  7. }
  1. [root@localhost nginx]# vim /usr/local/nginx/conf/vhost/default.conf
  2. server{
  3. listen 80 default_server; #使用default_server指定nginx的默认虚拟主机
  4. deny all;
  5. }

若使用其他域名来访问虚拟主机时,会匹配到默认虚拟主机,该配置会拒绝未定义的域名的虚拟主机。若不配置该选项,默认排在最前边的server会成为默认虚拟主机。

五、测试配置文件是否存在问题:

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

六、当配置文件修改时,可以使用一下命令重新加载配置文件

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

 

版权声明:本文为yyxianren原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/yyxianren/p/10797364.html