Ubuntu 下使用 Docker 安装 Nginx

1、安装 nginx 镜像

docker pull nginx

2、在主机中创建挂载目录

  • 挂载 nginx 的静态文件目录 mkdir /your/dir/html
  • 挂载 nginx 的配置目录 mkdir /your/dir/conf/conf.d
  • 挂载 nginx 的日志目录 mkdir /your/dir/log

3、配置文件

将 nginx.conf 配置文件放在 /your/dir/conf/ 下。


user  root;
worker_processes  1;
 
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
 
 
events {
    worker_connections  1024;
}
 
 
http {

    include  /etc/nginx/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  /var/log/nginx/access.log  main;
 
    #sendfile  on;

    #tcp_nopush  on;
 
    keepalive_timeout  65;
 
    autoindex  on;
    
    gzip  on;
 
    client_max_body_size 100M;
 
    client_header_buffer_size  128k;

    large_client_header_buffers  4  128k;

    include  /etc/nginx/conf.d/*.conf;
}

将 default.conf 配置文件放在 /your/dir/conf/conf.d 下。

server {

    listen  80;

    server_name  localhost;

    charset koi8-r;

    access_log  /var/log/nginx/access.log  main;

    location / {
        root  /usr/share/nginx/html;
        index  index.html;
    }

    error_page  500 502 503 504  /50x.html;

    location = /50x.html {
        root  /usr/share/nginx/html;
    }

}

4、启动容器

将容器 80 端口映射到主机 80 端口。

docker run \
    --name docker-nginx \
    -d -p 80:80 \
    -v /your/dir/config/nginx.conf:/etc/nginx/nginx.conf \
    -v /your/dir/config/conf.d:/etc/nginx/conf.d \
    -v /your/dir/html:/usr/share/nginx/html \
    -v /your/dir/log:/var/log/nginx \
    nginx
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容