NGINX 配置內(nèi)網(wǎng)訪問的實(shí)現(xiàn)步驟
需求
我們有一個(gè)測試站的域名: https://test.zhiexa.com/ 這個(gè)域名是公網(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)建了一個(gè)變量 $allowed_ip
,用于判斷訪問IP是否在允許列表中:
- 值為0表示禁止訪問
- 值為1表示允許訪問
2. 訪問控制判斷
在server 段里面配置
# 在server 段配置 if ($allowed_ip = 0) { return 403; # 如果IP不在允許列表中,返回403禁止訪問錯(cuò)誤 }
3. 錯(cuò)誤頁面配置
在server 段里面配置
# 先配置錯(cuò)誤頁面,將403錯(cuò)誤重定向到一個(gè)命名location error_page 403 @403_handler; # 使用命名location來處理403錯(cuò)誤 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ì)的錯(cuò)誤日志 error_log /usr/local/nginx/logs/403_debug.log debug; } # 正確配置錯(cuò)誤頁面 403.html 放在這個(gè)位置 /usr/local/nginx/html/403.html location = /403.html { root /usr/local/nginx/html; internal; # 只允許內(nèi)部重定向訪問,不能直接從外部訪問 }
error_page 403 @403_handler 將403錯(cuò)誤重定向到一個(gè)命名location
location @403_handler 定義了處理403錯(cuò)誤的具體方式,包括顯示自定義錯(cuò)誤頁面和添加調(diào)試信息
location = /403.html 定義了403錯(cuò)誤頁面的位置,并設(shè)置為internal,防止直接訪問
整個(gè)配置的工作流程是:
- 當(dāng)有請求訪問服務(wù)器時(shí),Nginx檢查訪問IP
- 通過geo模塊判斷IP是否在允許列表中
- 如果不在允許列表中,返回403錯(cuò)誤
- 403錯(cuò)誤被重定向到自定義錯(cuò)誤頁面
- 同時(shí)記錄詳細(xì)的調(diào)試信息和日志
- 這樣就實(shí)現(xiàn)了只允許特定IP訪問,其他IP都會(huì)被拒絕并顯示自定義錯(cuò)誤頁面的功能。
我需要準(zhǔn)備一個(gè) 403.html
頁面 , 這個(gè)頁面放到 /usr/local/nginx/html
這個(gè)目錄下面
<!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">🚫</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. 一個(gè)完整的配置
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; # 先配置錯(cuò)誤頁面 error_page 403 @403_handler; # 使用命名location來處理403錯(cuò)誤 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ì)的錯(cuò)誤日志 error_log /usr/local/nginx/logs/403_debug.log debug; } if ($allowed_ip = 0) { return 403; } # 正確配置錯(cuò)誤頁面 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 文件的緩存時(shí)間為 4 小時(shí) location ~* \.(css|js)$ { expires 4h; add_header Cache-Control "public, no-transform"; } # 設(shè)置圖片文件的緩存時(shí)間為 4 小時(shí) 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 文件的緩存時(shí)間為 4 小時(shí) location ~* \.(css|js)$ { expires 4h; add_header Cache-Control "public, no-transform"; } # 設(shè)置圖片文件的緩存時(shí)間為 4 小時(shí) 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超時(shí)時(shí)間 proxy_send_timeout 600; #web回傳數(shù)據(jù)至代理超時(shí)時(shí)間 proxy_read_timeout 600; #代理等待web響應(yīng)超時(shí)時(shí)間 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超時(shí)時(shí)間 proxy_send_timeout 300; #web回傳數(shù)據(jù)至代理超時(shí)時(shí)間 proxy_read_timeout 300; #代理等待web響應(yīng)超時(shí)時(shí)間 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; # 緩沖代理接收單個(gè)長連接內(nèi)包含的web相應(yīng)的數(shù)量和大小 } }
配置完成后 重啟 NGINX ,或者重新加載配置文件即可 。
# 檢查配置文件 是否存在語法錯(cuò)誤 nginx -t # 重新加載配置文件 nginx -s reload
參考文檔
Nginx 官方文檔 ngx_http_geo_module
到此這篇關(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同一個(gè)域名配置多個(gè)項(xiàng)目的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx同一個(gè)域名配置多個(gè)項(xiàng)目的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03nginx外網(wǎng)訪問內(nèi)網(wǎng)站點(diǎn)配置操作
這篇文章主要介紹了nginx外網(wǎng)訪問內(nèi)網(wǎng)站點(diǎn)配置操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08nginx支持.htaccess文件實(shí)現(xiàn)偽靜態(tài)的方法分享
這篇文章主要介紹了nginx支持.htaccess文件實(shí)現(xiàn)偽靜態(tài)的方法分享,需要的朋友可以參考下2015-01-01Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法
這篇文章主要介紹了Nginx+Keepalived實(shí)現(xiàn)雙機(jī)主備的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03