How to Nginx reverse proxy (proxy_pass) from inside Docker container to ouside (local network)

1.1k views Asked by At

Previous title: Use Docker Nginx container to proxy website on another machine in local network

I would really like to access my 3D Printer (OctoPrint) from outside. It is currently accessible at http://192.168.1.198 at my home.

I have docker setup on another server, and my home's port 80 & 443 is pointing to that one. On that server, I am using Traefik to forward all the domains & subdomains to whatever container needs it.

So I thought, well, what if I just create another Nginx container where is just proxy to OctoPrint. Guess what, it doesn't work that easily.

Currently my docker-compose.yml looks like this:

version: "2"

services:
  web:
    image: nginx
    restart: always
    networks:
      - traefik_proxy
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
    labels:
      - traefik.enable=true
      
      - traefik.http.routers.manuele0.entrypoints=http
      - traefik.http.routers.manuele0.rule=Host(`<my-site>.`) || Host(`www.<my-site>`) || Host(`3d-printer-1.<my-site>`)
      - traefik.http.routers.manuele0.middlewares=to_https

      # Redirect to main domain
      - traefik.http.routers.manuele1.entrypoints=https
      - traefik.http.routers.manuele1.rule=Host(`<my-site>`)
      - traefik.http.routers.manuele1.tls=true
      - traefik.http.routers.manuele1.tls.certresolver=le
      - traefik.http.routers.manuele1.middlewares=redirect_to_manuele
      - traefik.http.middlewares.redirect_to_manuele.redirectregex.regex=^https:\/\/([^\/]*)\/(.*)
      - traefik.http.middlewares.redirect_to_manuele.redirectregex.replacement=https://www.<my-site>/$${2}

      - traefik.http.routers.manuele.entrypoints=https
      - traefik.http.routers.manuele.rule=Host(`www.<my-site>`) || Host(`3d-printer-1.<my-site>`)
      - traefik.http.routers.manuele.tls=true
      - traefik.http.routers.manuele.tls.certresolver=le

networks:
  traefik_proxy:
    external: true

And the Nginx configuration:

# <here is another proxy to my GitHub page, which works and is not related to this problem>

server {
    listen 80;
    listen [::]:80;

    server_name 3d-printer-1.<my-site>;

    location / {
        proxy_pass              http://192.168.1.198;
        # proxy_http_version      1.1;
        # proxy_buffering         off;
        proxy_set_header        Host                $http_host;
        # proxy_set_header        Upgrade             $http_upgrade;
        # proxy_set_header        Connection          $proxy_connection;
        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   $proxy_x_forwarded_proto;
        # proxy_set_header        X-Forwarded-Ssl     $proxy_x_forwarded_ssl;
        # proxy_set_header        X-Forwarded-Port    $proxy_x_forwarded_port;

        proxy_intercept_errors  on;
        expires                 off;
    }

    #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;
    }
}

http://192.168.1.198 doesn't work, so how else could I proxy to it?

0

There are 0 answers