1. Create Tomcat Container
docker run -it --rm -p 8888:8080 -d tomcat:8.0
(The tomcat url is "http://172.17.0.3:8080" 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/nginx.pid;
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 http://172.17.0.3:8080; #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 "http://www.example.com:9090", then "server_name" should be "www.example.com" and "listen" should be "9090".
If your backend service app address is "http://192.168.10.100:8080", then "proxy_pass" should be "http://192.168.10.100:8080", which means that this value is pointing to your real service address. (Here we set this value as "http://172.17.0.3:8080", 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.