Back to articles

Setting Up a Free Pixiv Image Proxy with Cloudflare

1. Purchase a Domain

  • Purpose: You need a domain name to host your proxy service.
  • Recommended: Choose a cheap domain extension like .xyz, .ga, or .cf.

2. Register Cloudflare Account


3. Add Site to Cloudflare

  • Step 1: Click Add Site > Add a Site.
  • Step 2: Enter your domain name (e.g., yourdomain.com) and click Next.
  • Step 3: Cloudflare will scan your domain's current DNS records. Click Continue Setup.
  • Step 4: Choose Free Plan (the basic plan is sufficient for a proxy service).
  • Step 5: Update DNS Nameservers:
    • Your domain registrar (e.g., Alibaba Cloud) requires updating nameservers to Cloudflare's:
      • Nameserver 1: ns1.cloudflare.com
      • Nameserver 2: ns2.cloudflare.com
    • Follow your domain registrar's instructions to update nameservers. This step ensures Cloudflare manages your DNS.

4. Configure DNS Records

  • Proxy Settings: Go to the DNS tab in Cloudflare.
  • Add a New A Record:
    • Click Add Record > select A record.
    • Fill in the fields:
      • Name: Enter pixiv (or a custom subdomain name, e.g., proxy).
      • IPv4 Address: Enter any valid IP address (e.g., 8.8.8.8 for testing purposes).
      • TTL: Keep it as Auto (recommended).
  • Enable Proxy Status:
    • Check the box Proxy this record (the icon will turn orange).
    • This enables Cloudflare's proxy for this subdomain.
  • Save Changes:
    • Click Save to apply the configuration.

5. Set Up Cloudflare Workers

  1. Go to Workers > Create a Worker.
  2. Create a New Service:
    • Click Create a Worker.
    • Name: Enter a name (e.g., pixiv-proxy).
    • Route: Set the route to https://pixiv.yourdomain.com/* (replace yourdomain.com with your domain).
    • Click Create Service.
  3. Edit the Worker Code:
    • Go to the Resources tab.
    • Click Quick Edit to open the code editor.
    • Replace the default code with the following script (explained below):
javascript
addEventListener('fetch', event => {
    let url = new URL(event.request.url);
    url.hostname = 'i.pximg.net';
    let request = new Request(url, event.request);
    event.respondWith(
        fetch(request, {
            headers:{
                'Referer': 'https://www.pixiv.net',
                'User-Agent':'Cloudflare Workers'
            }
        })
    );
});

6. Bind the Custom Domain

  1. Return to the Dashboard: Go back to the Cloudflare dashboard homepage.
  2. Select Your Worker: Navigate to Workers > Your newly created service (e.g., pixiv-proxy).
  3. Add a Custom Domain:
    • Click Add Custom Domain.
    • Enter the subdomain you configured in the A record (e.g., pixiv.yourdomain.com).
    • Click Add Domain.

7. Test the Proxy

Example Test
  1. Pixiv Original Image Link: https://i.pximg.net/img-original/img/2019/03/06/00/40/39/73532572_p0.jpg
  2. Proxy Link: https://pixiv.yourdomain.com/img-original/img/2019/03/06/00/40/39/73532572_p0.jpg
  3. Access the Proxy Link: Open the modified link in a browser. If successful, the image should display.

Original post: https://www.theresa3rd.cn/#/imgProxy

cloudflareimagepixivproxyjavascript