Home > Back-end >  Configure nginx to run jenkins on specific path
Configure nginx to run jenkins on specific path

Time:01-05

I am new in linux world so hope my question is formulated correctly.

I have hosted my .net core app on linux vm (centos) running on localhost:5000 and have nginx set up to access it from outisde. I access this website like this http://myipaddress/

I also installed jenkins and it is running on localhost:8080 inside vm.

I want to configure nginx.config file like that so I had access to jenkins on path http://myipaddress/jenkins

nginx.config file:

       server {
       listen 80 default_server;
       listen [::]:80 default_server;
       location /  {
           proxy_pass http://localhost:5000;
           proxy_http_version 1.1;
           proxy_set_header Upgrade $http_upgrade;
           proxy_set_header Connection keep-alive;
           proxy_set_header Host $host;
           proxy_cache_bypass $http_upgrade;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           proxy_set_header X-Forwarded-Proto $scheme;
        }

I think I should add new 'location' entry to point to jenkins but not sure how to do it. any help would be appreciated.

CodePudding user response:

First, you have to configure Jenkins with a context path. You can do this by passing a launch parameter --prefix=/jenkins. If you are on docker you can pass it like below.

docker run --name myjenkins -p 8080:8080 -p 50000:50000 --env JENKINS_OPTS="--prefix=/jenkins" jenkins/jenkins:lts

Else you can export the following env variable.

export JENKINS_OPTS="--prefix=/jenkins"

Once that's done a simple Nginx rule like below should work for you.

server {
    listen 80;
    server_name jenkins.ycr.com;

    location /jenkins {
            proxy_pass  http://127.0.0.1:8080;
            proxy_redirect http://127.0.0.1:8080/ http://jenkins.ycr.com/;
        }
}

CodePudding user response:

  • Docker:
docker run --name jenkins -p 8080:8080 -p 50000:50000 --env JENKINS_OPTS="--prefix=/jenkins" jenkins/jenkins:lts-jdk11

-p 50000:50000: will be used when you connect a slave agent, more details

  • Docker file:
FROM jenkins/jenkins:lts-jdk11
.......
ENV JENKINS_OPTS="--httpPort=8080 --prefix=/jenkins"
......
EXPOSE 8080
  • Docker compose
version: '3.8'
services:
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    ports:
      - 8080:8080
      - 50000:50000
    environment:
      - JENKINS_OPTS=--prefix=/jenkins
  • SYS_VM_MACHINE:

    Edit jenkins config file and add --prefix=/jenkins to 'JENKINS_ARGS'.

    • Ubuntu/Debian => '/etc/default/jenkins'
    • Rhel/CentOS => '/etc/sysconfig/jenkins'
JENKINS_ARGS="... --prefix=/jenkins"

Nginx config like :

upstream jenkins_server {
  keepalive 64; # keepalive connections
  server 127.0.0.1:8080; # jenkins ip and port
}

server {
   listen       80;
   #Add mapping in file /etc/hosts to using a HostName Instead of Ip Adress
   server_name  "my.servername.local" ;

   ...

   location ^~ /jenkins/ {
       proxy_pass         http://jenkins_server/jenkins;

       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 $scheme;
       proxy_max_temp_file_size 0;

       # for jenkins websocket agents
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";  

       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;

   }

}
  • Related