docker安装nginx容器小记

前言: 使用docker安装了nginx容器,很久才成功跑起来,对安装过程做下记录

linux系统:centos7.4

docker安装不阐述,直接记录安装创建nginx容器的过程

1. 拉取nginx的镜像,此处拉取的最新版

docker pull nginx

2. 创建nginx容器之前需要先确认下要挂载的文件,进入到自己想要的放置挂载文件的目录下,此处我的为/usr/fordocker,并进入。

3. 创建容器

docker run -p 80:80 --name nginx -v $PWD/www:/www -v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf -v $PWD/logs:/wwwlogs -v $PWD/conf/conf.d:/etc/nginx/conf.d -d nginx

 -v 参数后面代表的是宿主机文件路径和容器文件路径

 -v $PWD/www:/www  意思为 将当前目录下的www文件挂载到 容器的www目录下

    此处挂载了www目录,nginx.conf文件(修改配置),logs日志文件,conf.d文件(用来存放*.conf文件)

4. 执行完3,容器创建完成,这个时候我们需要配置下nginx.conf文件,只需要修改刚刚配置的在宿主机的nginx.conf即可

#user  nobody;
worker_processes  1;

error_log  /wwwlogs/error.log; #pid 日志路径   此处的路径均为容器内路径
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

pid        /www/nginx.pid;  #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  /wwwlogs/access.log  main;  # access_log路径

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    gzip  on;

    include /etc/nginx/conf.d/*.conf; #引入的conf文件存放路径
}

 配置完成后进入刚刚配置的conf.d文件中创建test.conf,并配置如下

server {
    listen       80;
    server_name  www.itryfirst.top;
    root /www/webapps/test;
    index index.php index.html index.htm;

    location ~ .php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ .*.(gif|jpg|jpeg|png|bmp|swf)$
    {
        expires 30d;
    }
    location ~ .*.(js|css)?$
    {
        expires 1h;
    }
}

 最后进入上面配置的/www/webapps/test路径中创建index.html文件,并在其中输入

<html>
    <p>hello world</p>
</html>

  访问域名,页面出现hello world, 容器安装成功!

原文地址:https://www.cnblogs.com/cyclzdblog/p/9613178.html