1. 程式人生 > >Docker教程系列六:Docker上部署Nginx

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

res host format 系列 ive nec efault bin con

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

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