linux环境下docker中搭建 jenkins 及自定义访问路径,利用nginx反向代理

前言

前两天发布了完整的 linux服务器上Docker中安装jenkins
在实际的开发中,可能我们并不能直接开放8081或者8080端口给jenkins使用,常常是通过nginx方向代理来实现的,这里我们来配置一下。

linux环境下docker中搭建 jenkins 及自定义访问路径,利用nginx反向代理_第1张图片


linux环境下docker中搭建 jenkins 及自定义访问路径,nginx反向代理

  • 前言
  • 1 linux服务器上Docker中安装jenkins
  • 2 请求路径
    • 修改nginx
    • 给jenkins项目配置统一的请求前缀 (推荐)
  • 3 删除原来的容器
  • 4 创建挂载目录
    • 修改已经挂载的目录
    • 删除容器
    • 启动容器
  • 5 nginx配置
    • 完成

1 linux服务器上Docker中安装jenkins

如果你还没有安装 可以参考我这篇文章 linux服务器上Docker中安装jenkins
看到目录: 创建一个jenkins目录 就可以回来继续看如何自定义访问路径和nginx反向代理了。

2 请求路径

我们以下面这个请求的域名举例子

https://app.域名.com/jenkins/

也就是请求进来之后,/jenkins
这个/jenkins 可以根据需要自行配置
nginx转发的时候会带上这个统一的前缀/jenkins

两种解决思路:

修改nginx

查一下如何配置nginx自动去除调这个统一前缀/jenkins

给jenkins项目配置统一的请求前缀 (推荐)

-e JENKINS_OPTS=“–prefix=/jenkins”

启动jenkins容器的时候,加上上面的参数,-e 为启动的时候的必要环境变量

》 - - prefix= /jenkins 为统一的访问前缀前缀。

3 删除原来的容器

如果原来有 则需要删除,没有则不需要删除

docker ps -a
docker rm -f 容器id

g6grfnalpZ:/data/jenkins_home# docker ps
CONTAINER ID   IMAGE                 COMMAND                  CREATED         STATUS                          PORTS                                                                                                                                                                                                                                                             NAMES
0f9ea3076687   jenkins/jenkins:lts   "/usr/bin/tini -- /u…"   4 minutes ago   Restarting (1) 18 seconds ago                                                                                                                                                                                                                                                                     jxj_jenkins
0fdbf676ad87   emqx/emqx:latest      "/usr/bin/docker-ent…"   5 months ago    Up 2 weeks                      4369-4370/tcp, 5369/tcp, 6369-6370/tcp, 0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:8083-8084->8083-8084/tcp, :::8083-8084->8083-8084/tcp, 8081/tcp, 0.0.0.0:8883->8883/tcp, :::8883->8883/tcp, 0.0.0.0:18083->18083/tcp, :::18083->18083/tcp, 11883/tcp   emqx
bb3e68e10a26   mysql:latest          "docker-entrypoint.s…"   7 months ago    Up 2 weeks                      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   

grfnalpZ:/data/jenkins_home# docker rm -f 0f9ea3076687
0f9ea3076687

4 创建挂载目录

mkdir /data/jenkins_home
chown -R 1000:1000 /data/jenkins_home

如果原来没有启动容器挂载过

docker run -d --name jenkins -p 8899:8080 --restart=always -e TZ=“Asia/Shanghai” -e JENKINS_OPTS=“- -prefix=/jenkins” -e JENKINS_ARGS=“- -prefix=/jenkins” -v /data/jenkins_home:/var/jenkins_home jenkins/jenkins:lts;

如果已经挂载过了
两个思路

修改已经挂载的目录

提供一篇较好的博客(不是我写的,感觉挺详细的)
docker对已经启动的容器添加目录映射(挂载目录)

删除容器

docker ps
docker kill 容器id
docker rm -f 容器id

启动容器

docker run -d --name jxj_jenkins -p 8899:8080 --restart=always -e TZ=“Asia/Shanghai” -e JENKINS_OPTS=“- -prefix=/jenkins” -e JENKINS_ARGS=“- -prefix=/jenkins” -v /data/jenkins_home:/var/jenkins_home jenkins/jenkins:lts;

注意 - -prefix=/jenkins 是两个横杠中间一个空格,我复制过来就变成了一条线无奈再中间又加了一个空格,不知道会不会影响

参数解析:


-d           后台运行容器,并返回容器ID--name       为容器起一个容易区分且容易书写的名字
-p           映射宿主机端口到容器端口,宿主机端口:容器端口
--restart=always  机器重启时自动启动容器
-e       设定一些必须的环境变量。
          -e TZ="Asia/Shanghai" 设定时区为上海,强烈建议国内设定,否则容器内打印的所有日志时间都会差8小时。
          -e JENKINS_OPTS="--prefix=/jenkins" -e JENKINS_ARGS="--prefix=/jenkins" 重点!!!设定jenkins访问的路径,非必选,如果是直接根目录或ip地址访问,则不需要添加,如果是想nginx反向代理且不在根目录下,则必须,否则只配置nginx会报静态文件404错误,此处名字应与文件夹名称一致。
-v       挂载宿主机文件到容器。本例两个分别为:
          -v /etc/localtime:/etc/localtime:ro 挂载宿主机时间,保持容器时间正确。
          -v /data/jenkins_home:/var/jenkins_home  绑定挂载文件
          

5 nginx配置

前提是已经安装好了nginx,如果没有安装,这里可能需要暂停一下安装好再过来。

cd /
find -name nginx.conf
vim nginx.conf路径

添加配置

          location ~ (^/jenkins).* {
                 proxy_pass   http://127.0.0.1:8899;
                #proxy_set_header Host                $host:$server_port;
              }

完整nginx.conf配置


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;    


#gzip  on;

    server {
        listen       80;
        server_name  localhost 39.107.79.167 whx.ink;        
#charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

          location ~ (^/jenkins).* {
                 proxy_pass   http://127.0.0.1:8899;
                #proxy_set_header Host                $host:$server_port;
              }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

完成

linux环境下docker中搭建 jenkins 及自定义访问路径,利用nginx反向代理_第2张图片
linux环境下docker中搭建 jenkins 及自定义访问路径,利用nginx反向代理_第3张图片

你可能感兴趣的:(nginx,操作系统,docker,jenkins,docker,linux)