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這個是訪問宿主機(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ù)器之間的連接保持時間,超過這個時間將會自動關(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添加網(wǎng)橋并設(shè)置ip地址范圍操作
這篇文章主要介紹了docker添加網(wǎng)橋并設(shè)置ip地址范圍操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
Docker部署Ragflow(完美解決502 bad gateway)
本文主要介紹了Docker部署Ragflow,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
使用Docker查看Elasticsearch錯誤日志的詳細(xì)步驟
在使用 Elasticsearch(簡稱 ES)的過程中,我們可能會遇到各種問題,為了快速定位和解決這些問題,查看錯誤日志是關(guān)鍵,本文將介紹如何使用 Docker 查看 Elasticsearch 的錯誤日志,并提供一些實(shí)用技巧,需要的朋友可以參考下2025-08-08
修改Docker容器內(nèi)文件的三種實(shí)現(xiàn)方式
這篇文章主要介紹了修改Docker容器內(nèi)文件的三種實(shí)現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08
excel導(dǎo)出在docker環(huán)境中總是失敗的問題
這篇文章主要介紹了excel導(dǎo)出在docker環(huán)境中總是失敗的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,2020-09-09
Docker網(wǎng)絡(luò)代理設(shè)置詳解
在一些實(shí)驗(yàn)室環(huán)境,服務(wù)器沒有直接連接外網(wǎng)的權(quán)限,需要通過網(wǎng)絡(luò)代理。本篇文章主要介紹了Docker網(wǎng)絡(luò)代理設(shè)置詳解,有興趣的可以了解一下。2017-02-02

