ContOS docker nginx安装

2 篇文章 0 订阅
2 篇文章 0 订阅

1.docker的安装

centos下docker安装

2.创建nginx映射路径

mkdir -p /docker/nginx/html && mkdir -p /docker/nginx/conf.d && mkdir -p /docker/nginx/conf &&  mkdir -p /docker/nginx/logs

3.拉取最新镜像

docker pull nginx

4.创建nginx配置文件,两种方式

1.手动创建

/docker/nginx/conf 下创建nginx.conf文件
/docker/nginx/conf.d 下创建default.conf文件

2.从运行的nginx中拷贝,后再做修改,操作如下

先运行nginx:

docker run --name=nginx -p 80:80 -d nginx

拷贝配置文件:

docker cp nginx:/etc/nginx/nginx.conf /docker/nginx/conf
docker cp nginx:/etc/nginx/conf.d/default.conf /docker/nginx/conf.d

删除运行的nginx:

docker rm nginx

5.修改配置文件

nginx.conf修改为:

user  nginx;
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;

    #gzip  on;
	#引入其他配置
    include /etc/nginx/conf.d/*.conf;
}

/docker/nginx/conf.d下创建default.conf文件,以及后面自己的nginx配置文件,这里我创建了名为my.conf的文件用于测试静态资源的访问。default.conf文件内容如下,为了演示效果,我将nginx默认的欢迎页改为自定义的欢迎页1.html(1.html文件事先放置于 /docker/nginx/html 下)

server {
    listen       80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        #用自定义的欢迎页覆盖nginx默认的欢迎页
        index  1.html;
    }

    #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   /usr/share/nginx/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;
    #}
}

my.conf文件内容如下,这里我在 /docker/nginx/html 路径下创建了files文件夹,用于存放媒体文件,以便测试浏览器访问静态文件资源

server {
    listen  80;
    server_name  localhost;
    access_log /var/log/nginx/tt.access.log main;
    error_log /var/log/nginx/tt.error.log error;
	location ~ .*\.(gif|jpg|jpeg|png)$ {
		expires 24h;  
		root /usr/share/nginx/html/files/;#指定图片存放路径  需要在对应的外挂路径下创建files文件夹
		proxy_store on;  
		proxy_temp_path    /usr/share/nginx/html/files/;#图片访问路径  需要在对应的外挂路径下创建files文件夹
		proxy_redirect     off;  
		proxy_set_header    Host 127.0.0.1;  
		client_max_body_size  10m;
		client_body_buffer_size 1280k;  
		proxy_connect_timeout  900;  
		proxy_send_timeout   900;  
		proxy_read_timeout   900;  
		proxy_buffer_size    40k;  
		proxy_buffers      40 320k;  
		proxy_busy_buffers_size 640k;  
		proxy_temp_file_write_size 640k;  
		if ( !-e $request_filename)  
		  {  
			 proxy_pass http://127.0.0.1;#默认80端口  
		  } 
    } 
}

6.运行容器

docker run -d -p 80:80 --name=nginx --restart=always -v /docker/nginx/html:/usr/share/nginx/html -v /docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf:ro -v /docker/nginx/conf.d:/etc/nginx/conf.d -v /docker/nginx/logs:/var/log/nginx nginx

7.浏览器访问80端口查看效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值