Search code examples
dockernginxnugetnexus

Nuget proxy via Nexus repository behind Nginx


I run the Nexus Repository behind the Nginx both of them through Docker. This is my docker-compose file

services:
  nexus:
    image: sonatype/nexus3:latest
    container_name: nexus
    restart: always
    ports:
      - "8081:8081"
    volumes:
      - nexus-data:/nexus-data
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    restart: always
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx/conf.d/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/certs:/etc/nginx/certs

volumes:
  nexus-data:

I created a Nuget proxy in Nexus with the following settings

Name: nuget-proxy
URL: http://nexusrepo.example.local/repository/nuget-proxy/index.json
Remote URL: https://api.nuget.org/v3/index.json

I also create a location on Nginx to proxy pass that repo with the following configuration

server {
        listen 443 ssl;
        server_name nexusrepo.example.local;

        ssl_certificate /etc/nginx/certs/nexusrepo.example.local.pem;
        ssl_certificate_key /etc/nginx/certs/nexusrepo.example.local.key;
        
        location / {
            proxy_pass http://nexus:8081/;
            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 $scheme;
        }

        location /nuget {
            proxy_pass http://nexus:8081/repository/nuget-proxy;
            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 $scheme;
        }
}

my problem is when I want to install Nuget packages with Nexus itself I can. E.g I can install it via the following command

dotnet add package Polly --version 8.4.1 --source http://nexusrepo.example.local:8081/repository/nuget-proxy/index.json

But I can't install packages with Nginx e.g when I run

dotnet add package Polly --version 8.4.1 --source https://nexusrepo.example.local/nuget/index.json

I get an error.

Unable to load the service index for source https://nexusrepo.example.local/nuget/index.json

I would truly be grateful if anyone could help

EDIT

My question is not related to Nuget connection attempt failed "Unable to load the service index for source"

It's just about how Nginx passes the URL to Nexus. I explain that when I use the Nexus URL I can install packages but when I use the Nginx URL I can't. I suppose it is somehow related to how Nginx passes URL to Nexus.

In my case, I don't set any proxy to access the Nexus and it works but the above question is about how to access the main Nuget repo through proxy.


Solution

  • Considering that the NuGet repository is located at /repository/nuget-proxy/, you should configure the Nginx reverse proxy as follows:

    server
    {
        listen 443 ssl;
        server_name nexusrepo.example.local;
    
        ssl_certificate /etc/nginx/certs/nexusrepo.example.local.pem;
        ssl_certificate_key /etc/nginx/certs/nexusrepo.example.local.key;
    
        location /
        {
            proxy_pass http://nexus:8081/;
            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 $scheme;
        }
    
        location /repository/nuget-proxy/
        {
            proxy_pass http://nexus:8081/repository/nuget-proxy/;
            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 $scheme;
        }
    }