最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Nginx三種不同類型的虛擬主機的配置(基于域名、IP 和端口)

 更新時間:2025年04月11日 09:03:27   作者:神秘泣男子  
本文主要介紹了Nginx多種虛擬主機配置方式,能夠根據(jù)域名、IP 或端口區(qū)分不同的站點,本文就來介紹一下,感興趣的可以了解一下

Nginx 是一款高性能的 Web 服務(wù)器,支持多種虛擬主機配置方式,能夠根據(jù)域名、IP 或端口區(qū)分不同的站點。這種靈活性讓 Nginx 成為搭建多站點服務(wù)的首選工具。本文將帶你一步步實現(xiàn)三種常見的虛擬主機配置方法:基于域名、基于 IP 和基于端口的虛擬主機。無論你是初學者還是有經(jīng)驗的運維人員,這篇教程都能幫助你快速掌握虛擬主機的配置技巧。

以下案例演示 是基于源碼包安裝的nignx (如果你是rpm包 也差不多 只用把路徑改為你nginx的路徑即可 其他沒什么大的變化,如果你是小白請繞道!)

1. 基于域名的虛擬主機

步驟 1:準備網(wǎng)站根目錄

為每個域名創(chuàng)建獨立的子目錄,并添加測試頁面:

[root@localhost ~]# mkdir -p /usr/local/nginx/html/site1
[root@localhost ~]# mkdir -p /usr/local/nginx/html/site2

[root@localhost ~]# echo "Welcome to Site 1" > /usr/local/nginx/html/site1/index.html
[root@localhost ~]# echo "Welcome to Site 2" > /usr/local/nginx/html/site2/index.html

步驟 2:修改 Nginx 配置文件

打開 Nginx 的配置文件:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在 http 配置段中添加以下內(nèi)容:

注釋:如果需要兩個虛擬主機 只用將再額外添加一個server即可

# 全局配置
user  nobody;
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.site1.com;

        # 網(wǎng)站根目錄
        root   html/site1;
        index  index.html index.htm;

        # 日志配置
        access_log  logs/site1_access.log;
        error_log   logs/site1_error.log;

        # 主路徑配置
        location / {
            try_files $uri $uri/ =404;
        }

        # 狀態(tài)監(jiān)控
        location /status {
            stub_status on;
            access_log off;
            allow 192.168.14.112;
            deny all;
        }

        # 錯誤頁面配置
        error_page   404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /404.html {
            root   html/site1;
        }
        location = /50x.html {
            root   html;
        }

        # 禁止訪問 .ht 文件
        location ~ /\.ht {
            deny all;
        }
    }

    server {
        listen       80;
        server_name  www.site2.com;

        # 網(wǎng)站根目錄
        root   html/site2;
        index  index.html index.htm;

        # 日志配置
        access_log  logs/site2_access.log;
        error_log   logs/site2_error.log;

        # 主路徑配置
        location / {
            try_files $uri $uri/ =404;
        }

        # 錯誤頁面配置
        error_page   404              /404.html;
        error_page   500 502 503 504  /50x.html;
        location = /404.html {
            root   html/site2;
        }
        location = /50x.html {
            root   html;
        }

        # 禁止訪問 .ht 文件
        location ~ /\.ht {
            deny all;
        }
    }
}

步驟 3:測試配置并重啟 Nginx

測試配置文件語法:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

重啟 Nginx 服務(wù):

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

步驟 4:訪問測試

在瀏覽器中訪問:

  • http://www.site1.com,應(yīng)顯示 Welcome to Site 1。
  • http://www.site2.com,應(yīng)顯示 Welcome to Site 2。

客戶端測試

修改hosts文件(本地dns解析)

[root@localhost ~]# vim /etc/hosts

2. 基于 IP 的虛擬主機

步驟 1:準備網(wǎng)站根目錄

為每個 IP 創(chuàng)建獨立的子目錄,并添加測試頁面:

[root@localhost ~]# mkdir -p /usr/local/nginx/html/ip1
[root@localhost ~]# mkdir -p /usr/local/nginx/html/ip2

[root@localhost ~]# echo "Welcome to IP 192.168.14.111" > /usr/local/nginx/html/ip1/index.html
[root@localhost ~]# echo "Welcome to IP 192.168.14.112" > /usr/local/nginx/html/ip2/index.html

步驟 2:修改 Nginx 配置文件

打開 Nginx 的配置文件:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在 http 配置段中添加以下內(nèi)容:

server {
    listen 192.168.14.111:80;
    server_name 192.168.14.111;
    root html/ip1;  # 使用默認路徑的子目錄
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # 錯誤頁面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;  # 默認錯誤頁面路徑
    }
}

server {
    listen 192.168.14.112:80;
    server_name 192.168.14.112;
    root html/ip2;  # 使用默認路徑的子目錄
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # 錯誤頁面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;  # 默認錯誤頁面路徑
    }
}

步驟 3:測試配置并重啟 Nginx

測試配置文件語法:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

重啟 Nginx 服務(wù):

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

步驟 4:訪問測試

在瀏覽器中訪問:

  • http://192.168.14.111,應(yīng)顯示 Welcome to IP 192.168.14.111。
  • http://192.168.14.112,應(yīng)顯示 Welcome to IP 192.168.14.112。

客戶端測試

因為我在虛擬機測試 只有一個網(wǎng)卡 所以我在虛擬一個網(wǎng)卡 這個你可以忽視 看測試結(jié)果即可

 ip addr add 192.168.14.110/24 dev ens33

3. 基于端口的虛擬主機

步驟 1:準備網(wǎng)站根目錄

為每個端口創(chuàng)建獨立的子目錄,并添加測試頁面:

[root@localhost ~]# mkdir -p /usr/local/nginx/html/port1
[root@localhost ~]# mkdir -p /usr/local/nginx/html/port2

[root@localhost ~]# echo "Welcome to Port 8080" > /usr/local/nginx/html/port1/index.html
[root@localhost ~]# echo "Welcome to Port 9090" > /usr/local/nginx/html/port2/index.html

步驟 2:修改 Nginx 配置文件

打開 Nginx 的配置文件:

[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

在 http 配置段中添加以下內(nèi)容:

server {
    listen 8080;
    server_name localhost;
    root html/port1;  # 使用默認路徑的子目錄
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # 錯誤頁面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;  # 默認錯誤頁面路徑
    }
}

server {
    listen 9090;
    server_name localhost;
    root html/port2;  # 使用默認路徑的子目錄
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }

    # 錯誤頁面
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;  # 默認錯誤頁面路徑
    }
}

步驟 3:測試配置并重啟 Nginx

測試配置文件語法:

[root@localhost ~]# /usr/local/nginx/sbin/nginx -t

重啟 Nginx 服務(wù):

[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

步驟 4:訪問測試

在瀏覽器中訪問:

  • http://192.168.14.111:8080,應(yīng)顯示 Welcome to Port 8080。
  • http://192.168.14.111:9090,應(yīng)顯示 Welcome to Port 9090。

客戶端測試

4.總結(jié)

通過本文的詳細步驟,我們成功實現(xiàn)了基于域名、IP 和端口的虛擬主機配置。Nginx 的靈活性和高性能使其能夠輕松應(yīng)對多站點服務(wù)的需求。這些配置方法不僅適用于日常開發(fā)和測試環(huán)境,也能在生產(chǎn)環(huán)境中提供穩(wěn)定可靠的服務(wù)。

到此這篇關(guān)于Nginx三種不同類型的虛擬主機的配置(基于域名、IP 和端口)的文章就介紹到這了,更多相關(guān)Nginx 虛擬主機配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置

    詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置

    這篇文章主要介紹了詳解Nginx防盜鏈和Nginx訪問控制與Nginx解析php的配置的相關(guān)資料,這里提供實例幫助大家,學習理解這部分內(nèi)容,需要的朋友可以參考下
    2017-08-08
  • nginx http響應(yīng)限速的具體實現(xiàn)

    nginx http響應(yīng)限速的具體實現(xiàn)

    本文主要介紹了nginx http響應(yīng)限速的具體實現(xiàn),可以使用limite_rate和limit_rate_after來限制HTTP響應(yīng)的速度,具有一定的參考價值,感興趣的可以了解一下
    2024-05-05
  • Nginx 配置TCP代理轉(zhuǎn)發(fā)的實現(xiàn)

    Nginx 配置TCP代理轉(zhuǎn)發(fā)的實現(xiàn)

    本文主要介紹了使用Nginx新版的stream方式,實現(xiàn)TCP/UDP代理轉(zhuǎn)發(fā),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-10-10
  • Nginx隱藏版本號與網(wǎng)頁緩存時間的方法

    Nginx隱藏版本號與網(wǎng)頁緩存時間的方法

    這篇文章主要介紹了Nginx優(yōu)化之隱藏版本號與網(wǎng)頁緩存時間的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-11-11
  • nginx配置偽靜態(tài)和適配客戶端的方法步驟

    nginx配置偽靜態(tài)和適配客戶端的方法步驟

    這篇文章主要介紹了nginx配置偽靜態(tài)和適配客戶端的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • 使用nginx如何實現(xiàn)請求轉(zhuǎn)發(fā)的功能

    使用nginx如何實現(xiàn)請求轉(zhuǎn)發(fā)的功能

    文章介紹了如何配置Nginx作為反向代理服務(wù)器,實現(xiàn)請求轉(zhuǎn)發(fā)和負載均衡,并進行了靜態(tài)和動態(tài)內(nèi)容分離,主要步驟包括修改Nginx默認端口、配置轉(zhuǎn)發(fā)規(guī)則和修改配置文件
    2024-12-12
  • Nginx 配置反向代理使用 Google fonts 字體并開啟 HTTP2/SSL 支持

    Nginx 配置反向代理使用 Google fonts 字體并開啟 HTTP2/SSL 支持

    nginx作為web服務(wù)器一個重要的功能就是反向代理。當然你也可以使用nginx配置正向代理,本是介紹如何配置nginx的反向代理。nginx反向代理的指令不需要新增額外的模塊,默認自帶proxy_pass指令,只需要修改配置文件就可以實現(xiàn)反向代理。
    2017-04-04
  • Nginx下Frp強制重定向為https配置詳解

    Nginx下Frp強制重定向為https配置詳解

    這篇文章主要介紹了Nginx下Frp強制重定向為https配置詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • Nginx 搭建域名訪問環(huán)境的詳細過程

    Nginx 搭建域名訪問環(huán)境的詳細過程

    這篇文章主要介紹了Nginx 搭建域名訪問環(huán)境的詳細過程,通過示例代碼講解了Nginx 搭建轉(zhuǎn)發(fā)網(wǎng)關(guān)進行負載均衡的相關(guān)操作,感興趣的朋友跟隨小編一起看看吧
    2024-06-06
  • Nginx-rewrite模塊概述

    Nginx-rewrite模塊概述

    從功能上看 rewrite 和 location 似乎有點像,都能實現(xiàn)跳轉(zhuǎn),主要區(qū)別在于 rewrite 是在同一域名內(nèi)更改獲取資源的路徑,這篇文章主要介紹了Nginx-rewrite模塊詳細介紹,需要的朋友可以參考下
    2023-06-06

最新評論

江口县| 舟山市| 宜君县| 鄂托克前旗| 天柱县| 鄂温| 宁化县| 洛南县| 广安市| 青河县| 仙居县| 西贡区| 星座| 湾仔区| 龙山县| 扶绥县| 柳江县| 湛江市| 临邑县| 武平县| 萝北县| 本溪| 济阳县| 锡林浩特市| 松溪县| 怀宁县| 南和县| 丹凤县| 石家庄市| 浏阳市| 福安市| 茶陵县| 保山市| 莱西市| 会东县| 新丰县| 正安县| 延吉市| 柞水县| 峨边| 平定县|