SoFunction
Updated on 2025-03-10

Docker deploys nginx to access host service and use cache operation method

1. Pull docker

docker pull nginx

2. Create configuration folders, cache data storage folders and log folders

For example:

mkdir -p /etc/nginx/
mkdir -p /etc/nginx/log
mkdir -p /data/nginx/cache

3. Add a new configuration file under the configuration folder

, this is the host name that accesses the host, fixed.

server {
        listen       80;
        listen  [::]:80;
        server_name  domain 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 :8080/file;
        }
        error_page   500 502 503 504  /;
        location = / {
            root   html;
        }
    }

Added, general configuration

#user  nobody;
worker_processes  1;
#error_log  logs/;
#error_log  logs/  notice;
#error_log  logs/  info;
#pid        logs/;
events {
    worker_connections  1024;
}
http {
        # Introduce a file that defines the MIME type map    include       /etc/nginx/;
    # Set the default MIME type to application/octet-stream    default_type  application/octet-stream;
    # Set the log format main to record client access logs    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" ';
    # Specify the storage location of the access log and the log format used    access_log  /var/log/nginx/log/  main;
    error_log  /var/log/nginx/log/ warn;
    access_log /var/log/nginx/log/ cache;
    # Enable sendfile function to improve file transfer performance    sendfile        on;
    # If the client connection is very fast, tcp_nopush may be enabled, otherwise comment out this line    # tcp_nopush     on;
    # The connection between the client and the server will be automatically closed after this time.    keepalive_timeout  65;
    # If you need to enable gzip compression, you can remove the comments on this line    gzip  on;
    upstream onedrive_download{    
        server :8080;
    }
    proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=video_cache:10m max_size=10g inactive=1440m use_temp_path=off;
    # Introduce all .conf configuration files in the /etc/nginx// directory    include /etc/nginx//*.conf;
}

4. Start the service

docker run -d --name nginx --restart=always --add-host=:host-gateway \
 -v /etc/nginx//:/etc/nginx/ \
 -v /etc/nginx//:/etc/nginx// \ 
 -v /etc/nginx/log/log/:/var/log/nginx/log/ \ 
 -v /data/nginx/cache:/etc/nginx/cache \
 -p 80:80 -t nginx

--add-host=:host-gatewayThe function is to map the host host. The others are mapped and cached files.

This is the article about docker deploying nginx to access host services and using cache. For more related docker deploying nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!