1

上一篇:使用Docker Nginx容器代理本地网络另一台机器上的网站

我真的很想从外面访问我的 3D 打印机 (OctoPrint)。它目前可以http://192.168.1.198在我家访问。

我在另一台服务器上设置了 docker,我家的 80 和 443 端口指向那个。在该服务器上,我使用 Traefik 将所有域和子域转发到任何需要它的容器。

所以我想,好吧,如果我只是创建另一个 Nginx 容器,它只是 OctoPrint 的代理。猜猜看,它并不那么容易。

目前我的docker-compose.yml样子是这样的:

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

和 Nginx 配置:

# <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不起作用,那么我还能如何代理它?

4

0 回答 0