首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复错误400钩子应包含在Nginx服务器后运行的Jenkins的有效负载

如何修复错误400钩子应包含在Nginx服务器后运行的Jenkins的有效负载
EN

Stack Overflow用户
提问于 2020-08-06 11:45:20
回答 1查看 119关注 0票数 0

Jenkins在CentOS虚拟机上的Nginx服务器后面运行。我可以通过web浏览器中的web界面访问Jenkins。由于我希望在将代码推送到GitHub存储库时触发自动构建,因此我定义了一个Github存储库web挂钩。

然后我编辑了NGINX配置文件

代码语言:javascript
复制
/etc/nginx/nginx.conf

通过使用以下命令添加位置:

代码语言:javascript
复制
 location /github-webhook {
   proxy_pass http://localhost:8080/github-webhook;
   proxy_method POST;
   proxy_connect_timeout 150;
   proxy_send_timeout 100;
   proxy_read_timeout 100;
   proxy_buffers 4 32k;
   client_max_body_size 8m;
   client_body_buffer_size 128k;
 }

但是当Github发送POST请求时,Jenkins会发回400 Hook should contain payload响应。我能做些什么来解决这个问题吗?

以下是完整的Nginx配置文件(域名已改为xyz.com):

代码语言:javascript
复制
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

upstream jenkins{
   server 127.0.0.1:8080;
   keepalive 16;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name xyz.com;

    ssl_certificate /etc/letsencrypt/live/xyz.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/xyz.com/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    # intermediate configuration
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers off;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;


    # replace with the IP address of your resolver 
    resolver 127.0.0.1;

    ignore_invalid_headers off;

 location /github-webhook {
   proxy_pass http://localhost:8080/github-webhook;
   proxy_method POST;
   proxy_connect_timeout 150;
   proxy_send_timeout 100;
   proxy_read_timeout 100;
   proxy_buffers 4 32k;
   client_max_body_size 8m;
   client_body_buffer_size 128k;
 }

location / {
        proxy_pass http://jenkins;

        # we want to connect to Jenkins via HTTP 1.1 with keep-alive connections
        proxy_http_version 1.1;

        # has to be copied from server block, 
        # since we are defining per-location headers, and in 
        # this case server headers are ignored
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

        # no Connection header means keep-alive
        proxy_set_header Connection "";

        # Jenkins will use this header to tell if the connection
        # was made via http or https
        proxy_set_header X-Forwarded-Proto $scheme;

        # increase body size (default is 1mb)
        client_max_body_size       10m;

        # increase buffer size, not sure how this impacts Jenkins, but it is recommended
        # by official guide
        client_body_buffer_size    128k;

        # block below is for HTTP CLI commands in Jenkins

        # increase timeouts for long-running CLI commands (default is 60s)
        proxy_connect_timeout      90;
        proxy_send_timeout         90;
        proxy_read_timeout         90;

        # disable buffering
        proxy_buffering            off;
        proxy_request_buffering    off;
    }
}
}

下面是GitHub的webhook设置:

在Jenkins项目配置中,Github配置为:

EN

回答 1

Stack Overflow用户

发布于 2020-08-06 23:06:36

通过将Jenkins URL字段设置为http://localhost:8080/而不是xyz.com:8080/,解决了此问题。您可以转到Jenkins > Manage Jenkins > Configure System来访问此字段

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63276557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档