一、拉取docker
docker pull nginx
二、创建配置文件夹、缓存数据存放文件夹和日志文件夹
举例:
mkdir -p /etc/nginx/nginx.conf
mkdir -p /etc/nginx/log
mkdir -p /data/nginx/cache
三、在配置文件夹下面新增配置文件
default.conf,host.docker.internal这个是访问宿主机的主机名,固定的。
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;
# 设置默认 MIME 类型为 application/octet-stream
default_type application/octet-stream;
# 设置日志格式 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;
# 客户端与服务器之间的连接保持时间,超过这个时间将会自动关闭连接
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;
}
四、启动服务
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
的作用就是为了能映射宿主机host。其他的都是映射被指文件和缓存文件。