無根生

無根生

Using traefik to solve the problem of unable to access the ChatGPT API.

traefik.png
OpenAI has just updated the API interface for ChatGPT, but it was blocked in an unknown region on the same day. I saw various reverse proxy tutorials online, some using cf work, some using NGINX. I also want to share my experience.


First#

Make sure you have installed Traefik and configured the file provider directory.

[providers]
  [providers.file]
    directory = "/etc/traefik/conf.d/"

Mount the corresponding volume in the Docker file as follows.

    volumes:
      - /etc/localtime:/etc/localtime:ro
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - ./data/traefik.yml:/traefik.yml:ro
      - ./data/acme.json:/acme.json
      # Add folder with dynamic configuration yml
      - ./data/configurations://etc/traefik/conf.d/

Configuration#

Create an openai.yaml file in data/configurations and add the following content:

http:
  routers:
    openai:
      entryPoints:
        - websecure
      rule: "Host(`yourdomain.com`) && Path(`/v1/chat/completions`)"
      service: openai
  services:
    openai:
      loadBalancer:
        passHostHeader: true
        servers:
          - url: "https://api.openai.com"

After configuring, wait for a while and check if the configuration is loaded in the Traefik dashboard. traefik.png

I thought this would be enough. But it seems that it cannot proxy non-container services when I tried. However, this configuration works fine for container services. Is there something wrong with my usage? If anyone knows, please let me know. Thank you.

I make a new one#

I created a Docker image: https://hub.docker.com/r/mirrors2/chatgpt-api-proxy

version: '3.7'
services: 
  chatgpt-api-proxy:
    image: mirrors2/chatgpt-api-proxy
    container_name: chatgpt-api-proxy 
    restart: unless-stopped
    networks:
      - traefik
    labels:
      - "traefik.enable=true"
      - "traefik.docker.network=traefik"
      - "traefik.http.routers.chatgpt-api-proxy.entrypoints=websecure"
      - "traefik.http.routers.chatgpt-api-proxy.rule=Host(`your.domains`)"
      
networks:
  traefik:
    external: true

This time it works.

I also deployed a public proxy on Koyeb (no stability guaranteed):

https://chatgpt.mirrors2.eu.org

IMG_0136.PNG

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.