Docker教程系列六:Docker上部署Nginx

1下载Nginx镜像

docker pull nginx

2创建Nginx容器

docker run -di --name=nginx -p 80:80 nginx/bin/bash

3测试Nginx

浏览器地址栏输入: Linux系统ip

4配置反向代理

官方的nginx镜像,nginx配置文件nginx.conf 在/etc/nginx/目录下。

在容器内编辑配置文件不方便,我们可以先将配置文件从容器内拷贝到宿主机,编辑修改后再拷贝回去。

(1)从容器拷贝配置文件到宿主机

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

(2)编辑nginx.conf,添加反向代理配置(将之前部署tomcat反向代理到nginx的80端口,172.17.0.4:8080是tomcat的ip,需要用docker inspect tomcat查看)

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;

upstream tomcat {

server 172.17.0.4:8080;

}

server {

listen 80;

server_name localhost;

location / {

proxy_pass http://tomcat;

index index.html index.htm;

}

}

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

}

(3)将修改后的配置文件拷贝到容器

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

(4)重新启动容器

docker restart nginx

浏览器测试:Linux系统ip