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

NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟

 更新時間:2025年05月21日 11:22:18   作者:阿常囈語  
本文主要介紹了NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟,Nginx的geo模塊限制域名訪問權(quán)限,僅允許內(nèi)網(wǎng)/辦公室IP訪問,具有一定的參考價值,感興趣的可以了解一下

需求

我們有一個測試站的域名: https://test.zhiexa.com/ 這個域名是公網(wǎng)域名, 我希望限制其訪問,只有在內(nèi)網(wǎng),或者辦公室 IP 能夠訪問 ,該如何配置呢?

1. geo 模塊配置

首先在虛擬主機(jī)文件中domain-vhost.conf添加,全局模塊

# 添加允許的IP地址
geo $allowed_ip {
    default 0;  # 默認(rèn)拒絕所有IP

    # 內(nèi)網(wǎng)地址范圍
    10.0.0.0/8 1;      # 允許所有10開頭的內(nèi)網(wǎng)IP
    172.16.0.0/12 1;   # 允許172.16-172.31范圍的內(nèi)網(wǎng)IP
    192.168.0.0/16 1;  # 允許所有192.168開頭的內(nèi)網(wǎng)IP

    # 添加特定的外網(wǎng)IP(示例) xxx.xxx.xxx.137 改成自己希望可以訪問的IP
    xxx.xxx.xxx.137 1;  # 允許特定的辦公室IP
   
}

這部分使用 geo 模塊創(chuàng)建了一個變量 $allowed_ip ,用于判斷訪問IP是否在允許列表中:

  • 值為0表示禁止訪問
  • 值為1表示允許訪問

2. 訪問控制判斷

在server 段里面配置

# 在server 段配置
if ($allowed_ip = 0) {
    return 403;  # 如果IP不在允許列表中,返回403禁止訪問錯誤
}

3. 錯誤頁面配置

在server 段里面配置

# 先配置錯誤頁面,將403錯誤重定向到一個命名location
error_page 403 @403_handler;

# 使用命名location來處理403錯誤
location @403_handler {
    root /usr/local/nginx/html;
    try_files /403.html =404;
    
    # 強(qiáng)制添加調(diào)試頭信息 可以不用添加,調(diào)試使用
    add_header X-Debug-Path $document_root always;
    add_header X-Debug-File $request_filename always;
    add_header X-Debug-Uri $uri always;
    add_header X-Debug-Request-Uri $request_uri always;
    add_header X-Debug-Remote-Addr $remote_addr always;
    
    # 確保內(nèi)容類型正確
    default_type text/html;
    charset utf-8;
    
    # 詳細(xì)的錯誤日志
    error_log /usr/local/nginx/logs/403_debug.log debug;
}

# 正確配置錯誤頁面 403.html 放在這個位置 /usr/local/nginx/html/403.html
location = /403.html {
    root /usr/local/nginx/html;
    internal;  # 只允許內(nèi)部重定向訪問,不能直接從外部訪問
}
  • error_page 403 @403_handler 將403錯誤重定向到一個命名location

  • location @403_handler 定義了處理403錯誤的具體方式,包括顯示自定義錯誤頁面和添加調(diào)試信息

  • location = /403.html 定義了403錯誤頁面的位置,并設(shè)置為internal,防止直接訪問

整個配置的工作流程是:

  • 當(dāng)有請求訪問服務(wù)器時,Nginx檢查訪問IP
  • 通過geo模塊判斷IP是否在允許列表中
  • 如果不在允許列表中,返回403錯誤
  • 403錯誤被重定向到自定義錯誤頁面
  • 同時記錄詳細(xì)的調(diào)試信息和日志
  • 這樣就實(shí)現(xiàn)了只允許特定IP訪問,其他IP都會被拒絕并顯示自定義錯誤頁面的功能。

我需要準(zhǔn)備一個 403.html 頁面 , 這個頁面放到 /usr/local/nginx/html 這個目錄下面

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>訪問被拒絕 - 智AI</title>
    <style>
        body {
            font-family: 'PingFang SC', 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            padding: 0 20px;
        }
        .container {
            max-width: 600px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 40px;
            text-align: center;
        }
        .icon {
            font-size: 64px;
            margin-bottom: 20px;
            color: #f44336;
        }
        h1 {
            font-size: 28px;
            margin-bottom: 20px;
            color: #333;
        }
        p {
            font-size: 16px;
            line-height: 1.6;
            color: #666;
            margin-bottom: 30px;
        }
        .btn {
            display: inline-block;
            background-color: #1890ff;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 4px;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        .btn:hover {
            background-color: #40a9ff;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="icon">&#x1F6AB;</div>
        <h1>訪問被拒絕</h1>
        <p>很抱歉,您當(dāng)前的IP地址沒有權(quán)限訪問此頁面。此頁面僅限內(nèi)部網(wǎng)絡(luò)或授權(quán)IP訪問。</p>
        <p>如需訪問,請使用公司網(wǎng)絡(luò)或聯(lián)系管理員將您的IP添加到白名單。</p>
        <!-- 換成 公網(wǎng)正式域名  -->
        <a  rel="external nofollow"  class="btn">前往公開網(wǎng)站</a>
    </div>
</body>
</html>

4. 一個完整的配置

map $http_upgrade $connection_upgrade {
    default upgrade;
    '' close;
}


# 添加允許的IP地址
geo $allowed_ip {
    default 0;

    # 內(nèi)網(wǎng)地址范圍
    10.0.0.0/8 1; # 10.0.0.0 - 10.255.255.255
    172.16.0.0/12 1; # 172.16.0.0 - 172.31.255.255
    192.168.0.0/16 1; # 192.168.0.0 - 192.168.255.255

    # 添加特定的外網(wǎng)IP(示例)
    222.65.141.137 1; # office ip
    47.116.213.148 1; # 測試服務(wù)器IPIP_ADDRESS 1;
}


server {
    listen 80;
    server_name test.zhiexa.com;

    # 添加訪問日志以便調(diào)試
    access_log /usr/local/nginx/logs/test.com.access.log main;
    error_log /usr/local/nginx/logs/test.com.error.log debug;
	
    # 重定向到https
    return 302 https://$host$request_uri;
}


server {
    listen 443 ssl;
    server_name test.zhiexa.com;


    # 添加訪問日志以便調(diào)試
    access_log /usr/local/nginx/logs/test.com.ssl.access.log main buffer=16k flush=5s;
    error_log /usr/local/nginx/logs/test.com.ssl.error.log debug;

    # 先配置錯誤頁面
    error_page 403 @403_handler;

    # 使用命名location來處理403錯誤
    location @403_handler {
        root /usr/local/nginx/html;
        try_files /403.html =404;

        # 強(qiáng)制添加調(diào)試頭信息
        add_header X-Debug-Path $document_root always;
        add_header X-Debug-File $request_filename always;
        add_header X-Debug-Uri $uri always;
        add_header X-Debug-Request-Uri $request_uri always;
        add_header X-Debug-Remote-Addr $remote_addr always;

        # 確保內(nèi)容類型正確
        default_type text/html;
        charset utf-8;

        # 詳細(xì)的錯誤日志
        error_log /usr/local/nginx/logs/403_debug.log debug;
    }


    if ($allowed_ip = 0) {
        return 403;
    }

    # 正確配置錯誤頁面
    location = /403.html {
        root /usr/local/nginx/html;
        internal;
    }

    ssl_certificate cert/zhiexa.com.pem;
    ssl_certificate_key cert/zhiexa.com.key;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;


    # 開啟 Gzip 壓縮
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;


    # 根路徑配置
    location / {
        root /aaa/zhiexa-cloud-web/dist/;
        try_files $uri $uri/ /index.html;
        index index.html;
        error_log /usr/local/nginx/logs/test.com.root.error.log debug;

        # HTML 文件緩存控制
        location ~* \.(html|htm)$ {
            add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
            expires off;
            # add_header X-Debug-Path $document_root always;
            # add_header X-Debug-Uri $uri always;
        }

        # 設(shè)置 .css 和 .js 文件的緩存時間為 4 小時
        location ~* \.(css|js)$ {
            expires 4h;
            add_header Cache-Control "public, no-transform";
        }

        # 設(shè)置圖片文件的緩存時間為 4 小時
        location ~* \.(gif|jpg|jpeg|png|svg)$ {
            expires 4h;
            add_header Cache-Control "public, no-transform";
        }
    }


    location /h5 {
        root /service/customized-h5;
        try_files $uri $uri/ /index.html;
        index index.html;

        # 禁用 HTML 文件的緩存
        location ~* \.(html|htm)$ {
            add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0" always;
            expires off;
        }

        # 設(shè)置 .css 和 .js 文件的緩存時間為 4 小時
        location ~* \.(css|js)$ {
            expires 4h;
            add_header Cache-Control "public, no-transform";
        }

        # 設(shè)置圖片文件的緩存時間為 4 小時
        location ~* \.(gif|jpg|jpeg|png|svg)$ {
            expires 4h;
            add_header Cache-Control "public, no-transform";
        }
    }


    location /api/file-assistant {
        # 真實(shí)代理的IP:PORT 
        proxy_pass http://172.xxx.xxxx.xxx:8200;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 300; #代理連接web超時時間
        proxy_send_timeout 600; #web回傳數(shù)據(jù)至代理超時時間
        proxy_read_timeout 600; #代理等待web響應(yīng)超時時間


        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";

        proxy_cache off;
        proxy_buffering off;
    }


    location /zhiexa/prompt/api/v1 {
        # 真實(shí)代理的IP:PORT 
        proxy_pass http://172.xxx.xxxx.xxx:8009/zhiexa/prompt/api/v1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 300; #代理連接web超時時間
        proxy_send_timeout 300; #web回傳數(shù)據(jù)至代理超時時間
        proxy_read_timeout 300; #代理等待web響應(yīng)超時時間


        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "Upgrade";

        proxy_buffering on; #開啟代理緩沖區(qū),web回傳數(shù)據(jù)至緩沖區(qū),代理邊收邊傳給客服端
        proxy_buffer_size 32k; #代理接收web響應(yīng)的頭部信息的緩沖區(qū)大小
        proxy_buffers 4 128k; # 緩沖代理接收單個長連接內(nèi)包含的web相應(yīng)的數(shù)量和大小
    }

}

配置完成后 重啟 NGINX ,或者重新加載配置文件即可 。

# 檢查配置文件 是否存在語法錯誤
nginx -t 

# 重新加載配置文件
nginx -s reload 

參考文檔

Nginx 官方文檔 ngx_http_geo_module

Nginx 官方文檔

Nginx 核心模塊文檔

Nginx 變量說明

Nginx 開發(fā)從入門到精通

到此這篇關(guān)于NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟的文章就介紹到這了,更多相關(guān)NGINX 配置內(nèi)網(wǎng)訪問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • Nginx內(nèi)存占用過高排查與處理過程

    Nginx內(nèi)存占用過高排查與處理過程

    Nginx內(nèi)存使用率過高就像是一場暴風(fēng)雨,會給我們的網(wǎng)站和應(yīng)用帶來不小的麻煩,但只要我們能夠冷靜分析,找出問題的根源,對癥下藥,就一定能夠化解危機(jī),所以本文給大家介紹了Nginx內(nèi)存占用過高排查與處理過程,需要的朋友可以參考下
    2025-08-08
  • 教你使用Nginx限制百度蜘蛛頻繁抓取的問題

    教你使用Nginx限制百度蜘蛛頻繁抓取的問題

    這篇文章主要介紹了使用Nginx限制百度蜘蛛頻繁抓取的問題,百度蜘蛛對網(wǎng)站的抓取頻率高和抓取量驟增導(dǎo)致服務(wù)器負(fù)載高,經(jīng)常收到警告信息,每分鐘允許百度蜘蛛抓取200次,超過頻率限制的返回503,對Nginx限制蜘蛛頻繁抓取相關(guān)知識感興趣的朋友一起看看吧
    2022-01-01
  • 使用log_format為Nginx服務(wù)器設(shè)置更詳細(xì)的日志格式方法

    使用log_format為Nginx服務(wù)器設(shè)置更詳細(xì)的日志格式方法

    下面小編就為大家分享一篇使用log_format為Nginx服務(wù)器設(shè)置更詳細(xì)的日志格式方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-03-03
  • Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持

    Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持

    這篇文章主要介紹了Nginx下配置pathinfo及ThinkPHP的URL Rewrite模式支持,使用Nginx運(yùn)行ThinkPHP的必備配置,需要的朋友可以參考下
    2015-07-07
  • keepalived監(jiān)控nginx進(jìn)程的實(shí)現(xiàn)示例

    keepalived監(jiān)控nginx進(jìn)程的實(shí)現(xiàn)示例

    本文主要介紹了keepalived監(jiān)控nginx進(jìn)程的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-08-08
  • nginx反向代理導(dǎo)致session失效的問題解決

    nginx反向代理導(dǎo)致session失效的問題解決

    這篇文章主要介紹了nginx反向代理導(dǎo)致session失效的問題解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Nginx下將http改為https的操作步驟

    Nginx下將http改為https的操作步驟

    HTTPS是HTTP協(xié)議的安全版本,通過使用SSL(安全套接層)或TLS(傳輸層安全)協(xié)議加密通信,為數(shù)據(jù)傳輸提供了保密性、完整性和身份認(rèn)證,本文給大家介紹了Nginx下將http改為https的操作方法,需要的朋友可以參考下
    2024-08-08
  • Nginx服務(wù)器https配置的方法示例

    Nginx服務(wù)器https配置的方法示例

    這篇文章主要介紹了Nginx服務(wù)器https配置的方法示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-10-10
  • 討論nginx?location?順序問題

    討論nginx?location?順序問題

    在有一次配置時發(fā)現(xiàn),請求?uri?明明是符合了前綴匹配?^~?規(guī)則,但?nginx?卻沒有使用,這讓我對上述結(jié)論產(chǎn)生了疑惑。后續(xù)通過調(diào)研、實(shí)踐后發(fā)現(xiàn),上述結(jié)論可以說對,但也不對,是不是更疑惑了?沒關(guān)系,看完這篇文章你就知道我為什么會這樣說了
    2022-05-05
  • Nginx 502 Bad Gateway的原因及解決方法

    Nginx 502 Bad Gateway的原因及解決方法

    這篇文章主要給大家介紹了Nginx 502 Bad Gateway 錯誤的原因及解決方法,在php服務(wù)當(dāng)中,有兩個參數(shù)非常的重要:max_requestst和max_children,具體的原因必須要查看日志才可以弄明白,接下就和小編一起來看看具體原因及解決方法吧
    2023-08-08

最新評論

康定县| 吉安市| 永年县| 巴里| 政和县| 福安市| 高要市| 巴林左旗| 若尔盖县| 东乡| 政和县| 广州市| 筠连县| 奉节县| 小金县| 万盛区| 西藏| 青龙| 昌黎县| 昂仁县| 淮阳县| 南平市| 遵化市| 安顺市| 腾冲县| 平罗县| 奉贤区| 洛浦县| 武鸣县| 承德县| 大足县| 扎鲁特旗| 图木舒克市| 黑水县| 武汉市| 兴隆县| 遵义市| 泰和县| 招远市| 博客| 乳山市|