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]#

看一下效果
這里寫圖片描述

 


免責聲明!

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



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