Jenkins with reverse proxy using Nginx

Jenkins reverse proxy using Nginx

Install jenkins server

wget https://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo
rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install jenkins
chkconfig jenkins on
service jenkins start
service iptables stop
chkconfig iptables off
setenforce 0

Now login with http://ipaddress:8080 in your web browser, it will ask for the password

Go back to the server and copy the password for the following file and paste in the browser

cat /var/lib/jenkins/secrets/initialAdminPassword

Now create the user and start using jenkins

Install nginx

[root@jenkins ~]# yum install nginx -y

Open a file  /etc/nginx/conf.d/default.conf

and change listen 80 to 8000

[root@jenkins ~]# vi /etc/nginx/conf.d/default.conf
#
# The default server
#

server {
    listen       8000 default_server;
    listen       [::]:8000 default_server;
    server_name  _;
    root         /usr/share/nginx/html;

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

    location / {
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

Then in /etc/nginx/conf.d/ create a directory with you domain-name.conf

vi /etc/nginx/conf.d/lab.local.conf

and copypaste below lines

[root@jenkins ~]# vi /etc/nginx/conf.d/lab.local.conf

server {
 listen 80;
 server_name localhost;

 access_log /var/log/nginx/jenkins.access.log;
 error_log /var/log/nginx/jenkins.error.log;

 proxy_buffers 16 64k;
 proxy_buffer_size 128k;

 location / {
 proxy_pass http://127.0.0.1:8080;
 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
 proxy_redirect off;

 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 https;
 }

}
~

IF you want to use https then copy the following instead

For this extra steps are needed to create ssl certificates. Now we will need to create and put ssl certificates in /etc/nginx/ssl directory. Create if not there and put *.crt file and *.key file.

[root@jenkins ~]# vi /etc/nginx/conf.d/lab.local.conf
#upstream jenkins {
#  server 127.0.0.1:8080 fail_timeout=0;
#}

server {
  listen 80;
  server_name localhost;
#  return 301 https://$host$request_uri;
}

server {
  listen 443 ssl;
  server_name localhost;

  ssl_certificate /etc/nginx/ssl/server.crt;
  ssl_certificate_key /etc/nginx/ssl/server.key;

  location / {
    proxy_set_header        Host $host:$server_port;
    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 $scheme;
    proxy_redirect http:// https://;
    proxy_pass              http://127.0.0.1:8080;

 

restart service and use chkconfig

[root@jenkins ~]# service nginx restart
Stopping nginx:                                            [  OK  ]
Starting nginx:                                            [  OK  ]
[root@jenkins ~]# chkconfig nginx on

 

go to your browser and type in http://ipaddress it will redirect to the https jenkins  page

 

 

 

 

 

refrences :1    2

 

Leave a Reply