使用 Nginx 作为 Jenkins 代理时出现 502 Bad Gateway

使用 Nginx 作为 Jenkins 代理时出现 502 Bad Gateway

我使用 Centos 7 和 Nginx,并使用 letsencrypt 的 SSL 作为同一 droplet 上 jenkins 的代理。一切正常,直到我尝试代理 Jenkins.. 出现 502 Bad Gateway 错误。服务器块如下,有人知道为什么会发生这种情况吗?

server {
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/DOMAIN.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/DOMAIN.com/privkey.pem;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/ssl/certs/dhparam.pem;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:E$
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_stapling on;
    ssl_stapling_verify on;
    add_header Strict-Transport-Security max-age=15768000;
    server_name DOMAIN.com;

    location ~ /.well-known {
            allow all;
    }

    # The rest of your server block
    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            try_files $uri $uri/ =404;
            # Uncomment to enable naxsi on this location
            # include /etc/nginx/naxsi.rules
    }


  location ^~ /jenkins {

  sendfile off;
  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;
  proxy_set_header        X-Forwarded-Proto https;
  #proxy_redirect http:// https://;

  add_header Pragma "no-cache";

  # Fix the “It appears that your reverse proxy set up is broken" error.
  proxy_pass          http://localhost:8080;
  proxy_read_timeout  90;

  proxy_redirect      http://localhost:8080 https://DOMAIN.com/jenkins;

  # Optionally, require HTTP basic auth.
  # auth_basic "Please authenticate";
  # auth_basic_user_file /opt/nginx/htpasswd;}}

以下是从 nginx 错误日志中提取的几行:

2016/05/12 11:43:03 [error] 2514#0: *1 no resolver defined to resolve localhost, client: xxx.xxx.xxx.xxx, server: DOMAIN.com, request: "GET /jenkins HTTP/1.1", host$
2016/05/12 12:18:24 [error] 2724#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: xxx.xxx.xxx.xxx, server: DOMAIN.com, request$
2016/05/12 12:18:25 [error] 2724#0: *1 no live upstreams while connecting to upstream, client: xxx.xxx.xxx.xxx, server: DOMAIN.com, request: "GET /jenkins HTTP/1.1"$

编辑:一位朋友通过像这样编辑 SSL 指令下方的服务器块使它工作正常,但他尝试了任何可行的方法,不确定这是否是最好的方法?

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://127.0.0.1:8080;
proxy_redirect http:// https://;
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_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_buffer_size          4k;
proxy_buffers              4 32k;
proxy_busy_buffers_size    64k;
proxy_temp_file_write_size 64k;}  
location / {
# Optional configuration to detect and redirect iPhones
if ($http_user_agent ~* '(iPhone|iPod)') {
rewrite ^/$ /view/iphone/ redirect;}
try_files $uri @jenkins;}}

答案1

Centos 7 上的 Nginx 经常与 SELinux 发生冲突,解决办法如下:

sudo cat /var/log/audit/audit.log | grep nginx | grep denied

如果有任何线路那么这可以修复:

sudo cat /var/log/audit/audit.log | grep nginx | grep denied | audit2allow -M mynginx
sudo semodule -i mynginx.pp 

引荐来源

相关内容