title: Using Nginx as a proxy for Jenkins using Docker
body: <p>I'm using Nginx as a proxy for Jenkins server. Both in Docker container.</p>
  
  <p>The idea is Jenkins running on port 8080, with port 8080 exposed. Nginx listening on port 80 and redirecting traffic to Jenkins on port 8080. If you try to access port 8080 directly it will refuse the connection.</p>
  
  <p>Please see docker-compose.yml file:</p>
  
  <pre><code>version: '3.7'
  
  services:
      master:
          build: ./jenkins-master
          networks:
            - jenkins-net
          volumes:
            - jenkins-log:/var/log/jenkins
            - jenkins-data:/var/jenkins_home
  
     nginx:
        build: ./jenkins-nginx
        ports:
          - "80:80"
        networks:
          - jenkins-net
  
  networks:
   jenkins-net:
  
  volumes:
   jenkins-log:
   jenkins-data:
  </code></pre>
  
  <p>Jenkins-master Dockerfile:</p>
  
  <pre><code>FROM jenkins/jenkins:alpine
  LABEL maintainer=''
  
  USER root
  RUN mkdir /var/log/jenkins
  RUN mkdir /var/cache/jenkins
  RUN chown -R jenkins:jenkins /var/log/jenkins
  RUN chown -R jenkins:jenkins /var/cache/jenkins
  USER jenkins
  
  ENV JAVA_OPTS='-Xmx8192m'
  ENV JENKINS_OPTS=' --handlerCountMax=300 -- logfile=/var/log/jenkins/jenkins.log --webroot=/var/cache/jenkins/war'
  </code></pre>
  
  <p>This is the nginx.conf file:</p>
  
  <pre><code>server {
      listen 80;
      server_name localhost;
      access_log off;
  
     location / {
  
     proxy_pass         http://master:8080;
  
     proxy_set_header   Host             $host;
     proxy_set_header   X-Real-IP        $remote_addr;
     proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
     proxy_set_header   X-Forwarded-Proto http;
     proxy_max_temp_file_size 0;
  
  
     proxy_connect_timeout      150;
     proxy_send_timeout         100;
     proxy_read_timeout         100;
  
     proxy_buffer_size          8k;
     proxy_buffers              4 32k;
     proxy_busy_buffers_size    64k;
     proxy_temp_file_write_size 64k;
  
   }
  
  }
  </code></pre>
  
  <p>this is my jenkins-nginx Dockerfile:</p>
  
  <pre><code>FROM nginx:mainline-alpine
  
  RUN rm /etc/nginx/conf.d/default.conf
  COPY jenkins.conf /etc/nginx/conf.d/jenkins.conf
  COPY nginx.conf /etc/nginx/nginx.conf
  EXPOSE 80
  CMD ["nginx"]
  </code></pre>
  
  <p>Adding Nginx Dockerfile for completeness:</p>
  
  <pre><code>FROM nginx:mainline-alpine
  
  RUN rm /etc/nginx/conf.d/default.conf
  COPY jenkins.conf /etc/nginx/conf.d/jenkins.conf
  COPY nginx.conf /etc/nginx/nginx.conf
  EXPOSE 80
  CMD ["nginx"]
  </code></pre>
  
  <p>Jenkins.conf file:</p>
  
  <pre><code>daemon off;
  user nginx;
  worker_processes 2;
  
  error_log  /var/log/nginx/error.log warn;
  pid        /var/run/nginx.pid;
  
  events {
     worker_connections  1024;
     use epoll;
     accept_mutex off;
  }
  
  http {
  include  /etc/nginx/mime.types;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  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;
    keepalive_timeout  65;
    client_max_body_size  300m;
    client_body_buffer_size  128k;
  
    gzip on;
    gzip_http_version 1.0;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml text/javascript application/xml application/xml+rss application/javascript application/json;
    gzip_disable 'MSIE [1-6]\.';
    gzip_vary on;
    include /etc/nginx/conf.d/*.conf;
  
   }
  </code></pre>
  
  <p>The problem is both works independently but as soon as I try to connect them on one network they crash.</p>
  
  <p>Error throws <code>localhost refused to connect</code> on both services</p>