0%

nginx反向代理Jenkins问题

由于只开放了一个端口,要使用jenkins发布业务的话,需要使用nginx把jenkins的端口反向代理出来;关于jenkins搭建参加本博客之前文章

修改jenkins配置文件

vim /etc/sysconfig/jenkins

1
2
3
4
# 由于我这里把8080端口占用了,把jenkins修改为7999端口
JENKINS_PORT="7999"
# 为jenkins添加一个上下文对象
JENKINS_ARGS="--prefix=/jenkins"

修改nginx配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
worker_processes  1;
events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
client_max_body_size 60M;
client_body_buffer_size 512k;
upstream jenkins {
keepalive 32;
server 127.0.0.1:7999 ;
}

server {
listen 8080;
server_name localhost;
ignore_invalid_headers off;
#project config file
location /esp-web {
root html;
index index.html index.htm;
}
location /file {
root html;
}
location /esp {
proxy_pass http://127.0.0.1:8081/esp;
}
#jenkins config file
location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
#rewrite all static files into requests to the root
#E.g /static/12345678/css/something.css will become /css/something.css
rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}
location /userContent {
#have nginx handle all the static requests to the userContent folder files
#note : This is the $JENKINS_HOME dir
root /var/lib/jenkins/;
if (!-f $request_filename){
#this file does not exist, might be a directory or a /**view** url
rewrite (.*) /$1 last;
break;
}
sendfile on;
}
location @jenkins {
sendfile off;
proxy_pass http://jenkins;
proxy_redirect default;
proxy_http_version 1.1;

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;

#this is the maximum upload size
client_max_body_size 10m;
client_body_buffer_size 128k;

proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffering off;
proxy_request_buffering off; # Required for HTTP CLI commands in Jenkins > 2.54
proxy_set_header Connection ""; # Clear for keepalive
}
location / {
# Optional configuration to detect and redirect iPhones
if ($http_user_agent ~* '(iPhone|iPod)') {
rewrite ^/$ /view/iphone/ redirect;
}

try_files $uri @jenkins;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

重启nginx、jenkins

1
2
systemctl restart jenkins.service
nginx -s reload

参考文档:
https://wiki.jenkins.io/display/JENKINS/Jenkins+says+my+reverse+proxy+setup+is+broken
https://www.jianshu.com/p/8315657465ac