dokcer nginx 作为静态资源服务器

1.nginx 启动命令 

mkdir -p /root/data/nginx/{conf,conf.d,html,logs}

本地创建挂载目录 

docker run -p 80:80 -v /root/data/resource:/root/data/resource 
                    -v /root/nginx/conf/nginx.conf:/etc/nginx/nginx.conf  
                    -v /root/nginx/logs:/var/log/nginx -d nginx

-v  挂载数据卷 , 左边的是 本地 目录 , 右边的是 docker 容器目录

分别挂载 静态资源 数据卷 , nginx conf 数据卷, logs 数据卷

注意:要把挂载目录的权限设 777  (不确定?)

2.配置文件

在 /root/nginx/conf/ 中写配置文件

注意以下三点 :

1.

user  root

2.

root  /root/data/resource; 
autoindex on;         

3.    

注释掉 location (本地 resource目录不需要 index.html)

4. 添加 跨域

location / {
    add_header Access-Control-Allow-Origin *;
}
5. 防止缓存

add_header Cache-Control "no-cache, must-revalidate";

user  root;  #####
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 {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen 80;                  #####
        server_name resource;       #####
        root /root/data/resource;   #####
        autoindex on;               #####
        location / {
            add_header Access-Control-Allow-Origin *;
        }


        #charset koi8-r;
 #access_log  logs/host.access.log  main;

        #location / {
        #    root   /root/data/resource;
        #    index  index.html index.htm;
        #}

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker 是一个开源的容器化平台,而 Nginx 是一款高性能的 Web 服务器。你可以使用 Docker 部署和运行 Nginx 作为图片服务器。 以下是一些步骤来实现这个目标: 1. 安装 Docker:首先,确保你的系统上已经安装了 Docker。你可以参考 Docker 官方文档来安装适合你系统的 Docker。 2. 创建一个 Nginx 容器:使用 Docker 命令来创建一个 Nginx 容器。你可以运行以下命令: ``` docker run -d -p 80:80 --name nginx-server -v /path/to/your/images:/usr/share/nginx/html nginx ``` 这个命令会创建一个名为 "nginx-server" 的容器,并将容器的 80 端口映射到主机的 80 端口。同时,将本地存放图片的目录 `/path/to/your/images` 挂载到容器内的 `/usr/share/nginx/html` 目录,这样就可以在容器中访问这些图片了。 3. 配置 Nginx:在容器内部,Nginx 默认的静态文件目录是 `/usr/share/nginx/html`。你可以编辑容器内的 Nginx 配置文件来自定义设置。可以通过以下命令进入 Nginx 容器的 Shell: ``` docker exec -it nginx-server bash ``` 然后,可以修改 `/etc/nginx/nginx.conf` 或其他配置文件来满足你的需求。例如,你可以设置缓存、限制访问等。 4. 访问图片:现在,你可以通过访问 `http://localhost`(或主机的 IP 地址)来访问容器中的图片了。Nginx 会根据配置文件的设置来提供这些图片。 这样,你就可以在 Docker 中使用 Nginx 作为图片服务器了。记得将 `/path/to/your/images` 替换为你实际存放图片的路径。希望这些步骤对你有帮助!如有问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值