Nginx 反代 Imgur

2024/08/12 更新,尝试转换为 Cloudflare Workers(不稳定,切换回来)。

1
2
3
4
5
6
7
8
addEventListener("fetch", event => {
let url = new URL(event.request.url);
url.protocol = 'https:'
url.hostname = "i.imgur.com";
let request = new Request(url, event.request);
console.log(request)
event.respondWith(fetch(request));
})

最近在弄自用图床,主要考虑两个点:一是上传必须方便,二是方便管理,我后期如果不打算分享了,可以删除或者屏蔽。Imgur 是个不错的选择(另一个选择是 OneDrive 但总感觉分享太麻烦),唯一不愉快的地方是近期被墙了,所以做个反代吧。

Nginx 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
server {
listen 80;
server_name imgur.ihainan.me;

return 301 https://$host$request_uri;
}

server {
listen 443 ssl;
server_name imgur.ihainan.me;

if ($server_port !~ 443){
rewrite ^(/.*)$ https://$host$1 permanent;
}

location / {
client_max_body_size 1000m;
proxy_pass https://i.imgur.com/;
proxy_read_timeout 300s;
proxy_send_timeout 300s;


# proxy_set_header Host $host;
proxy_set_header Referer "";
proxy_set_header User-Agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


proxy_http_version 1.1;
proxy_set_header Upgrade "websocket";
proxy_set_header Connection "Upgrade";
}

ssl_certificate /etc/letsencrypt/live/ihainan.me/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/ihainan.me/privkey.pem;
}

测试地址:

demo

油猴脚本:

tampermonkey_script

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// ==UserScript==
// @name Imgur Custom Link Copier
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Add a button to copy modified imgur link to clipboard
// @author Jigao Fu
// @include /^https?://imgur.com/a/.*/
// @grant GM_setClipboard
// ==/UserScript==

(function() {
'use strict';

// Function to create the new button
function createCustomButton(imageUrl) {
const button = document.createElement('button');
button.type = 'button';
button.className = 'customCopyLink Button'; // Add classes for styling
button.innerHTML = '<span class="Button-label">Copy custom link</span>';

// Add margin to the button to create space between the new button and existing buttons
button.style.marginRight = '10px';

button.onclick = function() {
// Replace the domain in the image URL
const customUrl = imageUrl.replace('i.imgur.com', 'imgur.ihainan.me');
// Copy the new URL to the clipboard
GM_setClipboard(customUrl);
alert('Copied to clipboard: ' + customUrl);
};

return button;
}

// Function to add the new button next to existing "Copy link" buttons
function addCustomButtons() {
const copyLinkButtons = document.querySelectorAll('.PostContentMenu .copyLink.Button');

copyLinkButtons.forEach(button => {
// Find the image URL
const imageUrl = button.closest('.PostContent').querySelector('.PostContent-imageWrapper-rounded img').src;
if (!button.parentElement.querySelector('.customCopyLink')) {
// Create the custom button
const customButton = createCustomButton(imageUrl);
// Insert the custom button before the existing "Copy link" button
button.before(customButton);
}
});
}

function setupObserver() {
const targetNode = document.querySelector('.UploadPost-files');
if (targetNode) {
addCustomButtons();
const observer = new MutationObserver(addCustomButtons);
observer.observe(targetNode, { childList: true, subtree: true });
} else {
console.log("Target node not found, retrying in 1 second...");
setTimeout(setupObserver, 1000); // Retry after 1 second
}
}

setupObserver();
})();

参考: