nginx配置基于域名的虚拟主机

其实基于域名和基于ip的虚拟主机配置是差不多的,在配置基于ip的虚拟主机上我们只需要修改几个地方就能变成基于域名的虚拟主机,一个是要修改域名,一个是host文件直接看代码

[root@localhost nginx]# !428
cat conf/nginx.conf|grep -v "#"|grep -v "^$"
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.bp1.com;   #修改这里
        location / {
            root   html;
            index  index.html index.htm index.php;
        }
        error_page  404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ .php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
    server{
        listen 192.168.3.123:80;
        server_name www.bp3.com;       #修改这里
        access_log logs/bp2.access.log combined;
        location /
        {
            index index.html index.php;
            root html/bp2;
        }
        location ~ .php$ {
            root           html/bp2;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

    }
    server{
        listen 192.168.3.125:80;
        server_name www.bp2.com;        #修改这里
        location /{
            root html/bp3;
            index index.html index.php;
        }
        location ~ .php$ {
            root           html/bp3;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
#include    /opt/nginx/conf/vhosts/www.domain2.com.conf;  #这一句是包含另一个nginx虚拟机主机的配置文件,其内容类似于上面最后一个server里面的内容,去掉注释后其功能相当于新增一台虚拟主机 } [root@localhost nginx]# cat /etc/host host.conf hostname hosts hosts.allow hosts.deny [root@localhost nginx]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.3.128 www.bp1.com #随便写的 192.168.3.125 www.bp2.com 192.168.3.123 www.bp3.com [root@localhost nginx]#

看一下效果
这里写图片描述

原文地址:https://www.cnblogs.com/biaopei/p/7730516.html