docker部署nginx訪問宿主機(jī)服務(wù)并使用緩存的操作方法
一、拉取docker
docker pull nginx
二、創(chuàng)建配置文件夾、緩存數(shù)據(jù)存放文件夾和日志文件夾
舉例:
mkdir -p /etc/nginx/nginx.conf mkdir -p /etc/nginx/log mkdir -p /data/nginx/cache
三、在配置文件夾下面新增配置文件
default.conf,host.docker.internal這個(gè)是訪問宿主機(jī)的主機(jī)名,固定的。
server {
listen 80;
listen [::]:80;
server_name 域名;
location ^~/file {
proxy_cache video_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_lock on;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache "$upstream_cache_status";
proxy_pass http://host.docker.internal:8080/file;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}新增nginx.conf,放通用配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
# 引入 mime.types 文件,該文件定義了 MIME 類型映射
include /etc/nginx/mime.types;
# 設(shè)置默認(rèn) MIME 類型為 application/octet-stream
default_type application/octet-stream;
# 設(shè)置日志格式 main,記錄客戶端訪問日志
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
log_format cache '***$time_local ' '***$upstream_cache_status ' '***Cache-Control: $upstream_http_cache_control ' '***Expires: $upstream_http_expires ' '***"$request" ($status) ' '***"$http_user_agent" ';
# 指定訪問日志的存儲位置和使用的日志格式
access_log /var/log/nginx/log/access.log main;
error_log /var/log/nginx/log/error.log warn;
access_log /var/log/nginx/log/cache.log cache;
# 開啟 sendfile 功能,提高文件傳輸性能
sendfile on;
# 如果客戶端連接非??焖?,則可能啟用 tcp_nopush,否則請注釋掉此行
# tcp_nopush on;
# 客戶端與服務(wù)器之間的連接保持時(shí)間,超過這個(gè)時(shí)間將會自動關(guān)閉連接
keepalive_timeout 65;
# 如果需要開啟 gzip 壓縮功能,可以去掉此行的注釋
gzip on;
upstream onedrive_download{
server host.docker.internal:8080;
}
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=video_cache:10m max_size=10g inactive=1440m use_temp_path=off;
# 引入 /etc/nginx/conf.d/ 目錄下的所有 .conf 配置文件
include /etc/nginx/conf.d/*.conf;
}四、啟動服務(wù)
docker run -d --name nginx --restart=always --add-host=host.docker.internal:host-gateway \ -v /etc/nginx/nginx.conf/nginx.conf:/etc/nginx/nginx.conf \ -v /etc/nginx/nginx.conf/default.conf:/etc/nginx/conf.d/default.conf \ -v /etc/nginx/log/log/:/var/log/nginx/log/ \ -v /data/nginx/cache:/etc/nginx/cache \ -p 80:80 -t nginx
--add-host=host.docker.internal:host-gateway的作用就是為了能映射宿主機(jī)host。其他的都是映射被指文件和緩存文件。
到此這篇關(guān)于docker部署nginx訪問宿主機(jī)服務(wù),并使用緩存的文章就介紹到這了,更多相關(guān)docker部署nginx內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用Docker部署DotNetBrowser應(yīng)用程序流程
這篇文章主要介紹了使用Docker部署DotNetBrowser應(yīng)用程序流程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2026-03-03
docker容器訪問宿主機(jī)host設(shè)置的域名方式
文章討論了在Docker容器中訪問宿主機(jī)的網(wǎng)絡(luò)設(shè)置,特別是當(dāng)容器內(nèi)的服務(wù)需要請求一個(gè)在宿主機(jī)hosts文件中定義的域名時(shí),通過使用`--network=host`模式,容器可以直接使用宿主機(jī)的網(wǎng)絡(luò),從而無需通過Docker網(wǎng)關(guān),這樣可以避免端口沖突2025-12-12
docker發(fā)布sunnyNgrok實(shí)現(xiàn)內(nèi)外網(wǎng)穿透的方法(容器內(nèi)執(zhí)行命令)
這篇文章主要介紹了docker上發(fā)布 sunnyNgrok 實(shí)現(xiàn)內(nèi)外網(wǎng)穿透,容器內(nèi)執(zhí)行命令,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
Docker安裝MySQL并使用Navicat連接的操作方法
這篇文章主要介紹了Docker安裝MySQL并使用Navicat連接,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09

