Host an image proxy
pixiv only serves images to requests with Referer: https://www.pixiv.net/. An image proxy adds that header, so browsers can load pixiv images from your domain.
PixivFE has a built-in proxy, and on a public instance the reverse proxy in front of PixivFE can serve the same paths (see the Caddyfile), so this is optional. Run a separate image proxy to move image traffic to another server, or to cache images.
Browsers fetch animation frames from the i.pximg.net proxy with JavaScript, so keep the Access-Control-Allow-Origin header that i.pximg.net sends. The setups below pass it through.
Pick one setup below, then point PixivFE at it (see below).
Caddy
This Caddy config proxies three domains:
pximg.example.comfori.pximg.netstatic-pximg.example.comfors.pximg.netugoira.example.comfort-hk.ugoira.com
TIP
To allow only same-origin requests, set Cross-Origin-Resource-Policy to same-origin.
*.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/" # t-hk.ugoira.com will respond with HTTP 500 without this header
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
With nginx:
1. Configure the proxy cache path
Set the cache with the proxy_cache_path directive under the http context:
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
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; # Replace with your proxy hostname
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;
}
}Replace pximg.example.com with your domain.
Cloudflare Workers
With Cloudflare Workers:
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 });
}
}Deno
With Deno Playground or Deno Deploy.
Deno Playground
Paste this into the editor:
async function handleRequest(request: Request): Promise<Response> {
try {
const url = new URL(request.url);
url.hostname = "i.pximg.net";
const modifiedRequest = new Request(url, request);
const response = await fetch(modifiedRequest, {
headers: {
'Referer': 'https://www.pixiv.net/',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.3'
}
});
if (!response.ok) {
return new Response("Error fetching the pixiv image.", { status: response.status });
}
const newHeaders = new Headers(response.headers);
//? You can replace the origin with your own PixivFE domain if you prefer a dedicated proxy.
newHeaders.set('Access-Control-Allow-Origin', '*');
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders
});
} catch (error) {
console.error("Failed to fetch pixiv image: ", error);
return new Response("An error occurred while fetching the pixiv image.", { status: 500 });
}
}
Deno.serve({ port: 4242 }, handleRequest);Deno Deploy
Export the playground to a GitHub repository (in project settings), or deploy a repository with this code as main.ts.
WARNING
Deno's free tier (1,000,000 requests and 100 GB a month) is much smaller than Cloudflare Workers'. It may also need a GitHub account. It can be faster where Cloudflare is slow.
Use it in PixivFE
The proxy keeps pixiv's paths. Only the domain changes:
| URL | |
|---|---|
| pixiv | https://i.pximg.net/img-original/img/2023/06/06/20/30/01/108783513_p0.png |
| Proxy | https://pximg.example.com/img-original/img/2023/06/06/20/30/01/108783513_p0.png |
Set it as the instance default:
PIXIVFE_IMAGEPROXY=https://pximg.example.com
PIXIVFE_STATICPROXY=https://static-pximg.example.com
PIXIVFE_UGOIRAPROXY=https://ugoira.example.comSee content proxy options. Visitors can also pick a proxy in their settings.
More background: pixiv.cat's article.