美文网首页
使用nginx为Nexus3绑定不同域名

使用nginx为Nexus3绑定不同域名

作者: 上岸的魚 | 来源:发表于2020-03-15 21:37 被阅读0次

    1.配置

    接上篇我们创建了Nexus3,并且创建了docker group/dockerhost/docker proxy;后面我们将使用Nexus管理docker/maven/nuget等类型,我们希望能够有专门的域名指向不同的地址,方便记忆且看起来更正规。

    首先创建虚拟网络

     在Nexus3服务器上创建一虚拟网络
    [root@k8s-node2 conf.d]# docker network create nexus-net
    c5d6960fd00e08c60e0713d3057527d1be7264530d50a8c8ef49d735d9e9a7ff
    
    删除旧的Nexus3容器:
      docker stop containerId
      docker rm containerId
    添加上network参数,重新运行启动容器:
    docker run -di --name nexus3 \
     --restart=always \
     --network nexus-net \
    -p 8081:8081 \
    -p 8082:8082  \
    -p 8083:8083  \
    -p 8084:8084  \
    -p 8085:8085   \
    -v /data/nexus/nexus-data:/nexus-data \
    sonatype/nexus3
    

    安装Nginx

    首先创建目录
    mkdir /data/nginx80   #创建一个目录映射nginx文件
    chmod 777 nginx80   #授权
    

    创建配置文件nginx.conf

    cd /data/nginx80
    vim nginx.conf  #创建配置文件
    ################################
    user  nginx;
    worker_processes  1;
    error_log  /var/log/nginx/error.log warn;
    pid        /var/run/nginx.pid;
    events {
        worker_connections  1024;
    }
    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;
        sendfile        on;
        #tcp_nopush     on;
        keepalive_timeout  65;
        #gzip  on;
       # 扫描conf.d目录下所有的conf文件
        include /etc/nginx/conf.d/*.conf;
    }
    

    创建配置文件default.conf

    cd /data/nginx80
    mkdir conf.d
    cd conf.d
    vim default.conf  #创建配置文件
    ###############################
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        #access_log  /var/log/nginx/host.access.log  main;
    
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }
    
        #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   /usr/share/nginx/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;
        #}
    }
    

    创建配置文件hub.conf

    cd /data/nginx80/conf.d
    vim hub.conf  #创建配置文件
    ###############################
    upstream nexus_docker_proxy {
        server nexus3:8084;
    }
    
    upstream nexus_docker_hosted {
        server nexus3:8083;
    }
    upstream nexus_docker_group {
        server nexus3:8082;
    }
    server
        {
            listen 80;
            #listen 443 ssl;
            server_name hub.yourdomain.cn;  #这里绑定自己的域名
            # disable any limits to avoid HTTP 413 for large image uploads
            client_max_body_size 0;
            chunked_transfer_encoding on;
      
            set $upstream "nexus_docker_hosted";
    
            if ( $request_method ~* 'GET') {
               set $upstream "nexus_docker_group";
            }
       
            if ($request_uri ~ '/search') {
              set $upstream "nexus_docker_proxy"; 
            }  
            index index.html index.htm index.php;
            location / {
                proxy_pass http://$upstream;
                proxy_set_header Host $host;
                proxy_connect_timeout 3600;
                proxy_send_timeout 3600;
                proxy_read_timeout 3600;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_buffering off;
                proxy_request_buffering off;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto http;
          }
    }
    

    创建配置文件nexus.conf

    cd /data/nginx80/conf.d
    vim nexus.conf  #创建配置文件
    ###############################
    server 
    {
            listen 80;
            #listen 443 ssl;
            server_name nexus.xxx.cn;#这里填写你的域名,此端口映射的是nexus的web管理端
            #ssl_certificate     /etc/nginx/conf.d/ssl/xxx.com.cer;
            #ssl_certificate_key  /etc/nginx/conf.d/ssl/xxx.com.key;
            location / {
                proxy_redirect off;
                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_pass http://nexus3:8081;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                 root   /usr/share/nginx/html;
            }
        }
    

    安装Nginx

    docker run -di --name nexus-nginx --network nexus-net -p 80:80 --restart=always -v /data/nginx80/html:/usr/share/nginx/html -v /data/nginx80/nginx.conf:/etc/nginx/nginx.conf:ro -v /data/nginx80/conf.d:/etc/nginx/conf.d nginx:latest
    

    nginx启动后,可以用docker logs 容器ID查看下是否有错误日志,因为conf文件编写时可能有错误字符导致配置失效。
    还可以用curl localhost测试下,如果有html nginx字样说明基本配置成功。

    2.配置域名指向及验证

    配置域名指向
    域名管理中,为当前域名添加几个子域名的A记录,指向这台服务器的IP(如果是公网云服务器,则指向其公网IP),没有域名的可以本机host文件模拟。


    测试访问

    如上图所示,两个域名均可以正常访问。

    同理也可以为maven/nuget配置域名指向方便用户使用
    建议每一个域名单独起一个conf的文件,方便后期管理

    相关文章

      网友评论

          本文标题:使用nginx为Nexus3绑定不同域名

          本文链接:https://www.haomeiwen.com/subject/qbrbehtx.html