美文网首页
使用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绑定不同域名

    1.配置 接上篇我们创建了Nexus3,并且创建了docker group/dockerhost/docker p...

  • Nginx 绑定多个域名的方法

    Nginx 绑定多个域名的方法更新时间:2017-06-07 13:26:11 分享:nginx 绑定多个域名...

  • 使用Nginx问题记录

    Nginx禁止未绑定域名访问 error message "无效的域名 localhost"解决方案: Nginx...

  • [Nginx]多个域名绑定一个IP 的nginx设置

    多个域名绑定在一个ip上 通过nginx反向代理 使得ip上的80端口在多个域名中可以使用 相关配置如下: ngi...

  • nginx域名绑定 php

    linux上切换目录至/usr/local/nginx/conf/vhost/ 新建配置文件 需要绑定的域名.c...

  • nginx实现域名绑定

    生产环境利用nginx对后端服务器进行反向代理和负载均衡,再把外网域名解析到nginx的vip地址,这样在外网可以...

  • linux 使用nginx绑定https 以及域名

    第一步肯定是需要安装nginx(使用linux apt-get安装) 第二步使用https 就需要证书 ...

  • nginx详解

    基于域名的配置(核心配置如下) nginx的多server规范使用 nginx的主配置文件为/usr/local/...

  • 域名配置Https

    以Nginx服务器为例:1.首先购买云服务器和域名,并完成绑定(域名要通过备案)。 2.申请免费证书(腾讯云有效期...

  • Nginx服务器绑定域名

    1. 安装Nginx 2. 打开文件夹 3. 新建文件 4. 添加域名绑定 重启Nginx服务

网友评论

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

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