天天看點

Nginx總結(三)基于端口的虛拟主機配置

 前面講了如何配置基于IP的虛拟主機,大家可以去這裡看看nginx系列文章:https://www.cnblogs.com/zhangweizhong/category/1529997.html

今天就來講講Nginx如何基于端口的虛拟主機。

 需要說明的是:由于本文章是nginx系列文章中的一篇,文章裡面很多其他的配置,可能前面的文章已經說講過,然後後續就沒有在介紹,如果出現有些配置沒有講,大家可能需要去看看前面的文章。

應用場景

nginx對外提供81和82兩個端口監聽服務。

請求81端口則請求html81目錄下的html

請求82端口則請求html82目錄下的html

準備環境

1. 建立192.168.78.132虛拟機,保證本地電腦和虛拟網絡通暢。

2. 在192.168.78.132上安裝nginx。

html目錄建立

将原來nginx的html目錄拷貝兩個目錄 html81和html82,為了友善測試需要修改每個目錄下的index.html内容使之個性化。

配置虛拟主機

修改/usr/local/nginx/conf/nginx.conf檔案,添加兩個虛拟主機,如下:vi /usr/local/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;


events {
    worker_connections  1024;
}
 

http {

    include       mime.types;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    #配置虛拟主機
    server {
        #監聽的ip和端口,配置80
        listen       80;
         #虛拟主機名稱這裡配置ip位址
        server_name  192.168.101.3;
        #所有的請求都以/開始,所有的請求都可以比對此location
        location / {
             #使用root指令指定虛拟主機目錄即網頁存放目錄
             #比如通路http://ip/test.html将找到/usr/local/html3/test.html
             #比如通路http://ip/item/test.html将找到/usr/local/html3/item/test.html

            root   /usr/local/nginx/html80;
            #指定歡迎頁面,按從左到右順序查找
            index  index.html index.htm;

        }
    }

    #配置虛拟主機

    server {
        listen       8080;
        server_name  192.168.101.3;

        location / {

            root   /usr/local/nginx/html8080;

            index  index.html index.htm;

        }

    }

}      

測試

重新加載配置nginx配置檔案,檢視端口監聽狀态:

通路http://192.168.78.132:81

Nginx總結(三)基于端口的虛拟主機配置

通路http://192.168.78.132:82

Nginx總結(三)基于端口的虛拟主機配置

最後

以上,就把nginx 基于ip的配置虛拟主機講完了。後面會繼續講基于域名配置虛拟主機。