Jenkins URL localhost:8080将我重定向到localhost,破坏了网页

时间:2019-02-27 17:04:13

标签: docker nginx jenkins docker-compose

Jenkins代理出现问题。 Jenkins容器位于我的NGINX代理后面。我在http://localhost:8000上访问它。登录后,我被踢到http://localhost。詹金斯(Jenkins)上的某些链接也执行相同的操作,并删除了使屏幕制动的端口。我从Manage Jenkins页面的标题上看到的错误,并尝试也添加proxy_pass URL,但没有任何效果。

我的NGINX conf文件就是这样...

server {
   listen       8000;
   server_name  "";

   access_log off;

   location / {

       proxy_set_header X-Forwarded-Proto $scheme;
       proxy_set_header X-Forwarded-Port $server_port;
       proxy_set_header X-Forwarded-Host $host;
       proxy_pass         http://jenkins_master_1:8080;
       proxy_redirect http://jenkins_master_1:8080 http://localhost:8000;

       proxy_max_temp_file_size 0;

       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;    

   }

}

我的docker-compose.yml文件就像这样...

version: '3'

# Services are the names of each container
services:
  master:
    # Where to build the container from a Dockerfile
    build: ./jenkins-master
    # Open which ports to
    ports: 
      - "50000:50000"
    # Connecting volumes to in a container
    volumes:
      - jenkins-log:/var/log/jenkins
      - jenkins-data:/var/jenkins_home
    # Adding the service to a network
    networks: 
      - jenkins-net

  nginx:
    build: ./jenkins-nginx
    ports:
      - "8000:8000"
    networks: 
      - jenkins-net

# List of volumes to create
volumes: 
  jenkins-data:
  jenkins-log:

# List of netorks to create
networks: 
  jenkins-net:

我正在尝试学习Docker和Jenkins,并在遵循一个教程,jenkins_master_1来自docker-compose。任何帮助或指导都将不胜感激。

谢谢

3 个答案:

答案 0 :(得分:1)

假设1:NGINX位于应用程序的前面,接受端口80上的连接,然后传递到后端端口8080。

假设2:Jenkins应用程序和NGINX在此处位于同一服务器上。

您应该最初从端口80访问它,而不是8080(如果使用代理服务器)。

NGINX在80上收到请求,然后传递到后端8080。如果您使用的是代理,则从浏览器不应看到8080。如果您使用的是8080,并且正在执行某些操作,那么您直接进入应用程序...。也就是绕过了代理。

因此,如何开始解决它:

(1。)导航到http://localhost,它应通过您的代理服务器(如果设置正确)

(2。)在“管理Jenkins->配置系统-> Jenkins URL”中,确保URL设置为http://localhost

(3。)最好在NGINX配置中使用FQDN作为服务器名称,然后确保Jenkins仅在Jenkins.xml配置中侦听localhost上的连接。 Jenkins.xml应将侦听地址设置为127.0.0.1。然后,对该FQDN的外部请求将无法绕过代理,因为Jenkins仅允许来自本地主机(从NGINX或您在本地主机上使用浏览器)的连接。

那么,理想情况下,您有: http://fqdn-> NGINX在端口80上监听-> Jenkins在127.0.0.1:8080上。使用浏览器的用户(安全地在服务器外部)永远不会看到8080端口。

答案 1 :(得分:0)

尝试在proxy_redirect块中添加location指令。这指示Web服务器返回与服务器本身计算的值不同的301/302 http响应代码。有时,网络服务器无法像docker中那样正确计算其地址,而在docker中,容器没有外界的信息,并且该连接已被代理/转发。

location / {
    proxy_pass http://jenkins_master_1:8080;
    proxy_redirect http://jenkins_master_1:8080 http://localhost:8080;
}

SRC:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect

答案 2 :(得分:0)

添加X-Forwarded- *标头是正确的解决方案。

没有这样的标题,我会遇到很多错误,例如,在设置初始密码并单击继续按钮后,我被重定向到https://jenkinsci:8080。很多次,我访问https://jenkins.mydomain.com并单击网页上的链接时,我都被重定向到https://jenkinsci:8080https://jenkinsci:8080显然无法访问。我不知道为什么。也许tomact需要那些X-Forwarded- *标头信息。

本文-Jenkins behind an NGinX reverse proxy强烈建议那些想要在nginx后面运行jenkins的人,即使jenkins和nginx都是通过docker容器创建的。再一次,您最好添加那些X-Forwarded- *标头。

示例nginx vhost配置文件:

$pageNumber = 1;
$totalRowCount = 5000;
pagination($pageNumber, $totalRowCount);

}