Question

nginx set up for proxy_pass when ip6 and ip4 used

Is someone willing to tell me how to get this config to work?

# Define separate upstream blocks for IPv4 and IPv6 backend servers
upstream backend_ipv4_ht1 {
    server 192.168.0.1:1080;
}

upstream backend_ipv6_ht1 {
    server [2a02:8070:5181:25a0:da3a:ddff:fe28:17b]:1080;
}

upstream backend_ipv4_ht2 {
    server 192.168.0.1:2080;
}

upstream backend_ipv6_ht2 {
    server [2a02:8070:5181:25a0:da3a:ddff:fe28:17b]:2080;
}

# Map $remote_addr to the appropriate backend based on IP version
map $remote_addr $backend_ht1 {
    ~[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ backend_ipv4_ht1;
    default backend_ipv6_ht1;
}

map $remote_addr $backend_ht2 {
    ~[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ backend_ipv4_ht2;
    default backend_ipv6_ht2;
}

# HTTP and HTTPS server block for ht1.example.net
server {
    listen 443 ssl;
    listen [::]:443 ipv6only=off;

    server_name ht1.example.net;

    ssl_certificate /etc/letsencrypt/live/example.net/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem;

    ssl_protocols TLSv1.3;

    location / {
        # Proxy to the appropriate backend based on the client's IP version
        proxy_pass http://$backend_ht1;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

# HTTP and HTTPS server block for ht2.example.net
server {
    listen 443 ssl;
    listen [::]:443;

    server_name ht2.example.net;

    ssl_certificate /etc/letsencrypt/live/example.net/cert.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.net/privkey.pem

    ssl_protocols TLSv1.3;

    location / {
        # Proxy to the appropriate backend based on the client's IP version
        proxy_pass http://$backend_ht2;
        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_set_header X-Forwarded-Proto $scheme;
    }
}

I want that the reverse proxy proxies both the ipv4 and ipv6 addresses of my server… but the test:

nginx -t 

throws me the error:

nginx: [emerg] "upstream" directive is not allowed here in /etc/nginx/sites-enabled/example.conf:2
nginx: configuration file /etc/nginx/sites-enabled/example_config.conf test failed

Please help… Thank you :'|


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
July 25, 2023

Heya,

Try with the following Nginx config:

http {
    upstream backend_ipv4_ht1 {
        server 192.168.0.1:1080;
    }

    upstream backend_ipv6_ht1 {
        server [2a02:8070:5181:25a0:da3a:ddff:fe28:17b]:1080;
    }

    upstream backend_ipv4_ht2 {
        server 192.168.0.1:2080;
    }

    upstream backend_ipv6_ht2 {
        server [2a02:8070:5181:25a0:da3a:ddff:fe28:17b]:2080;
    }
    
    map $remote_addr $backend_ht1 {
        ~[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ backend_ipv4_ht1;
        default backend_ipv6_ht1;
    }

    map $remote_addr $backend_ht2 {
        ~[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+ backend_ipv4_ht2;
        default backend_ipv6_ht2;
    }

    # Here is the server block
    server {
        # .....
    }
}

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel