Deploy Nginx + Tomcat with Docker

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

1. Create Tomcat Container

docker run -it --rm -p 8888:8080 -d tomcat:8.0

(The tomcat url is "" in this example.)

2. Install Nginx Container

docker run -it -p 8080:8080 -p 80:80 --privileged=true -d centos:7 /usr/sbin/init
#("privileged" and "/usr/sbin/init" is a MUST, otherwise, "systemctl" command will not be available.)

Enter above container and execute following commands.

yum install epel-release -y
yum install nginx -y
systemctl start nginx

Once the the installation is done, you should be able to access the default home page of nginx via http://localhost

3. Config Nginx

vim /etc/nginx/nginx.conf
user nginx;
worker_processes 4; #your CPU core num
error_log /var/log/nginx/error.log;
pid /run/;

In generally,  "worker_processes" should be equal with your CPU core num.

server {
        listen       9090; #exposed port num
        server_name localhost; #exposed internet address 

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
                proxy_pass; #your web app address
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
                proxy_set_header X-Forwarded-Port $server_port;

If your future accessing url is "", then "server_name" should be "" and "listen" should be "9090".

If your backend service app address is  "", then "proxy_pass" should be "", which means that this value is pointing to your real service address. (Here we set this value as "", because there is a tomcat service running on that url.)

Once above configuration is done, pls call following cmd to reload the config.

/usr/sbin/nginx -s reload

4. Access Testing

Access http://localhost:9090, you should be able to see default tomcat home page.

Now, the real accessing sequence is User->Nginx->tomcat. You can check related logs from /var/log/nginx.