nginx配置虛擬主機

目錄

1.前言

1.通過端口區分不同虛擬機

2.通過域名區分虛擬主機


nginx配置虛擬主機就是在一臺服務器啓動多個網站。

如何區分不同的網站:

  1. 端口不同
  2. 域名不同

1.前言

Nginx的配置文件:

/usr/local/nginx/conf/nginx.conf

我們使用notepad++遠程編輯文件(https://blog.csdn.net/pdsu161530247/article/details/81702176),也可以直接使用vim等進行編輯。

在nginx.conf中最重要的是,這段配置:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
  • 一個server節點就是一個虛擬主機。
  • servcer裏面的listen表示監聽的端口
  • server_name表示訪問的域名
  • location是訪問的資源,其中html是nginx安裝目錄下的html目錄,裏面是nginx服務器訪問時展示的靜態頁面。index是默認的首頁

1.通過端口區分不同虛擬機

可以配置多個server,配置了多個虛擬主機,根據端口區分不同主機。

添加兩臺虛擬主機完整nginx.conf:

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  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	server {
        listen       81;
        server_name  localhost;

        location / {
            root   html-81;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
[root@localhost nginx]# cp -r html/ html-81

重新加載nginx配置

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

如果是阿里雲服務器記得添加安全組規則

訪問nginx,默認的80端口可以訪問一個server

通過81端口,可以訪問另一個server

2.通過域名區分虛擬主機

通過端口來區分虛擬主機,是不方便的,不可能一個網站使用80端口,另一個網站使用81端口。因爲80端口是默認的,用戶不需要加80,訪問的就是80端口的網站,不可能讓用戶進入另一個網站時,讓他加上端口號81,這是不現實的。而且幾乎沒有那個網站使用ip來訪問的,所以我們可以通過域名區分,配置虛擬主機。

 

我們先假設一個場景:我手頭有兩個網站一個淘寶、一個天貓,分別對應域名www.taobao.com、www.tianmao.com。但是我比較窮,我只有一臺linux服務器(一個公網ip)。由於一個域名對應一個ip地址,一個ip地址可以被多個域名綁定。所有我們可以通過ngnix配置兩個虛擬主機。

這裏我們修改hosts(C:\Windows\System32\drivers\etc)文件,配置域名和ip的映射關係,不走dns服務器。

比如:

192.168.25.148 www.taobao.com
192.168.25.148 www.tianmao.com

修改nginx配置(/usr/local/nginx/conf/nginx.conf):

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  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	server {
        listen       81;
        server_name  localhost;

        location / {
            root   html-81;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
	#域名區分
	server {
        listen       80;
        server_name  www.taobao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-taobao;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;
        server_name  www.tianmao.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html-tianmao;
            index  index.html index.htm;
        }
    }
}

copy nginx的默認訪問資源, 爲taobao和tianmao兩個站點設置,訪問的默認資源

[root@localhost nginx]# cp -r html/ html-taobao
[root@localhost nginx]# cp -r html/ html-tianmao

修改html-taobao中index.html加個淘寶的標識

修改html-taobao中index.html加個天貓的標識

重新加載配置

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

訪問www.taobao.com效果:

訪問www.tianmao.com效果:

 

至此nginx配置虛擬主機完成。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章