Skip to content

Hosting an image proxy server

If you prefer not to rely on third-party image proxy servers, you can set up your own! By hosting your own proxy server, you can access images from Pixiv by simply changing the Referer to https://www.pixiv.net/.

By hosting your own Pixiv image proxy server, you can have more control over the caching and access to Pixiv images without relying on external services.

Caddy

The configuration uses Caddy and reverse proxies three different domains:

  • pximg.example.com for i.pximg.net
  • static-pximg.example.com for s.pximg.net
  • ugoira.example.com for t-hk.ugoira.com

Note

To restrict access to same-origin requests only, set Cross-Origin-Resource-Policy to same-origin instead of cross-origin.

Text Only
*.example.com {
    @pximg host pximg.example.com
    handle @pximg {
        reverse_proxy https://i.pximg.net {
            header_up Host "i.pximg.net"
            header_up Referer "https://www.pixiv.net/"

            header_down -Server
            header_down -Via

            header_down Cross-Origin-Embedder-Policy "require-corp"
            header_down Cross-Origin-Opener-Policy "same-origin"
            header_down Cross-Origin-Resource-Policy "cross-origin"

            transport http {
                versions 2
            }
        }
    }

    @static-pximg host static-pximg.example.com
    handle @static-pximg {
        reverse_proxy https://s.pximg.net {
            header_up Host "s.pximg.net"
            header_up Referer "https://www.pixiv.net/"

            header_down -Server
            header_down -Via

            header_down Cross-Origin-Embedder-Policy "require-corp"
            header_down Cross-Origin-Opener-Policy "same-origin"
            header_down Cross-Origin-Resource-Policy "cross-origin"

            transport http {
                versions 2
            }
        }
    }

    @ugoira host ugoira.example.com
    handle @ugoira {
        reverse_proxy https://t-hk.ugoira.com {
            header_up Host "t-hk.ugoira.com"
            header_up Referer "https://ugoira.com/"
            header_up Origin "https://ugoira.com"

            header_up Sec-Fetch-Dest "empty"
            header_up Sec-Fetch-Mode "cors"
            header_up Sec-Fetch-Site "same-site"

            header_down -Server
            header_down -Via

            header_down Cross-Origin-Embedder-Policy "require-corp"
            header_down Cross-Origin-Opener-Policy "same-origin"
            header_down Cross-Origin-Resource-Policy "cross-origin"

            transport http {
                versions 2
            }
        }
    }
}

nginx

To set up an image proxy server using nginx, follow these steps:

1. Configure the proxy cache path

Set the cache path and parameters using the proxy_cache_path directive under the http context:

Nginx Configuration File
proxy_cache_path /path/to/cache levels=1:2 keys_zone=pximg:10m max_size=10g inactive=7d use_temp_path=off;

2. Set up the server block

Nginx Configuration File
server {
    listen 443 ssl http2;

    ssl_certificate /path/to/ssl_certificate.crt;
    ssl_certificate_key /path/to/ssl_certificate.key;

    server_name pximg.example.com; # (1)!
    access_log off;

    location / {
        proxy_cache pximg;
        proxy_pass https://i.pximg.net;
        proxy_cache_revalidate on;
        proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
        proxy_cache_lock on;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_set_header Host i.pximg.net;
        proxy_set_header Referer "https://www.pixiv.net/";
        proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; rv:122.0) Gecko/20100101 Firefox/122.0";

        proxy_cache_valid 200 7d;
        proxy_cache_valid 404 5m;
    }
}
  1. Replace pximg.example.com with your desired domain.

Cloudflare Workers

Alternatively, you can set up an image proxy server using Cloudflare Workers:

JavaScript
addEventListener("fetch", event => {
  event.respondWith(handleRequest(event.request));
});

async function handleRequest(originalRequest) {
  try {
    let url = new URL(originalRequest.url);
    url.hostname = "i.pximg.net";

    let modifiedRequest = new Request(url, originalRequest);
    let response = await fetch(modifiedRequest, {
      headers: {
        'Referer': 'https://www.pixiv.net/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; rv:122.0) Gecko/20100101 Firefox/122.0'
      }
    });

    if (!response.ok) {
      return new Response("Error fetching the resource.", { status: response.status });
    }

    return response;
  } catch (error) {
    console.error("Failed to fetch resource: ", error.message);

    return new Response("An error occurred while fetching the resource.", { status: 500 });
  }
}

Using the proxy server

Once you have set up your image proxy server, you can access Pixiv images by replacing the original domain with your proxy server domain:

Additional resources