0

I have installed jenkins on amazon ec2, ubuntu 16.04 machine. jenkins up on port 9090. I wanted to redirect port 80(http) to port 9090 and access jenkins with http://ci.mymachine.com/. I have followed Running Jenkins on Port 80 or 443 using iptables and setup rules in iptables to do port redirection from 80 to 9090. below are the rules created.

sudo iptables -L -n --line-numbers

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:9090
2    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
3    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443
4    ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8443

Chain FORWARD (policy ACCEPT)
num  target     prot opt source               destination         
1    ACCEPT     tcp  --  0.0.0.0/0            127.0.0.1            state NEW tcp dpt:8080

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination  

and redirection rules are

sudo iptables -t nat -L -n --line-numbers

Chain PREROUTING (policy ACCEPT)
num  target     prot opt source               destination         
1    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80 redir ports 9090
2    REDIRECT   tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:443 redir ports 8443

Chain INPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain OUTPUT (policy ACCEPT)
num  target     prot opt source               destination         

Chain POSTROUTING (policy ACCEPT)
num  target     prot opt source               destination  

Anyway when I access the jenkins using url http://ci.mymachine.com/, I get 502 connection refused page. But I can access jenkins with http://ci.mymachine.com:9090/. what I am missing here?

1

I propose you to install nginx to forward HTTP requests from standard 80 port to the Jenkins page. Here is a small example of setting for nginx.conf file (it works fine for me). On my Ubuntu machine I edited file /etc/nginx/sites-available/default (insert your ip address instead of mine):

location / {
    # First attempt to serve request as file, then
    # as directory, then fall back to displaying a 404.
    #try_files $uri $uri/ =404;
    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_pass http://192.168.56.103:8080;
    proxy_redirect http://192.168.56.103:8080/ $scheme://$host:8080/
}

Also I had to comment the line '#try_files $uri $uri/ =404;' 'cos otherwise I receiver 404 error. Restart nginx sudo /etc/init.d/nginx restart. Now if you point your browser to the http://ci.mymachine.com you should be redirected to the Jenkins page. Press Ctrl+F5 for 'hard' refresh of the page. Hope that helps. Here is the link to the discussion.

  • Thanks for the suggestion. Yes. This can be done. I planned to go to this if iptable approach not working. Anyway when doing this there is overhead for nginx. So jenkins also recommend to go for iptable approach. not sure why iptable redirection is not working as expected. – lakshman Sep 8 '17 at 10:27
  • You're welcome. I added 3 more lines in case you want to see the correct URL in you browser. – M. Dm. Sep 8 '17 at 10:31
0

I was able to connect to port 80 with the iptable configurations mentioned in the jenkins document, mentioned in question(that configuration works correctly for ubuntu 16.04 eventhough that documentation says they did not test that for ubuntu 16.04).

The problem was, this machine is an amazon ec2 instance as mentioned in the question. There was no inbound rules created for http port(80) for this machine in amazon console. After creating inbound rule using amazon console, now I can access the url http://ci.mymachine.com without any issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.