{"id":62,"date":"2024-08-04T17:06:14","date_gmt":"2024-08-04T09:06:14","guid":{"rendered":"https:\/\/www.weigot.com\/?p=62"},"modified":"2024-08-04T17:06:14","modified_gmt":"2024-08-04T09:06:14","slug":"%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2024\/08\/04\/%e5%8f%8d%e5%90%91%e4%bb%a3%e7%90%86\/","title":{"rendered":"\u53cd\u5411\u4ee3\u7406"},"content":{"rendered":"\n<p>\u597d\u7684,\u6211\u6765\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8fd9\u4e2a\u4ee3\u7801\u96c6\u6210\u4e86\u4e4b\u524d\u8ba8\u8bba\u7684\u4f18\u5316\u65b9\u6848,\u5305\u62ec Nginx \u914d\u7f6e\u548c PHP \u540e\u7aef\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u662f Nginx \u914d\u7f6e\u6587\u4ef6 <code>nginx.conf<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>events {\n    worker_connections 1024;\n}\n\nhttp {\n    include mime.types;\n    default_type application\/octet-stream;\n\n    sendfile on;\n    tcp_nopush on;\n    tcp_nodelay on;\n    keepalive_timeout 65;\n    types_hash_max_size 2048;\n\n    # Gzip \u538b\u7f29\n    gzip on;\n    gzip_disable \"msie6\";\n    gzip_vary on;\n    gzip_proxied any;\n    gzip_comp_level 6;\n    gzip_buffers 16 8k;\n    gzip_http_version 1.1;\n    gzip_types text\/plain text\/css application\/json application\/javascript text\/xml application\/xml application\/xml+rss text\/javascript;\n\n    # \u7f13\u5b58\u914d\u7f6e\n    location ~* \\.(jpg|jpeg|png|gif)$ {\n        root \/var\/www\/html\/cache;\n        try_files $uri @fetch_remote;\n        expires 30d;\n        access_log off;\n        add_header Cache-Control \"public\";\n    }\n\n    location @fetch_remote {\n        rewrite ^(.*)$ \/index.php?resource=$1 last;\n    }\n\n    location \/ {\n        try_files $uri $uri\/ \/index.php?$query_string;\n    }\n\n    # PHP \u914d\u7f6e\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/var\/run\/php\/php7.4-fpm.sock;\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u63a5\u4e0b\u6765\u662f PHP \u4ee3\u7801 <code>index.php<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\/\/ index.php\n\n\/\/ \u8bbe\u7f6e Curl \u9009\u9879\n$curl_options = array(\n    CURLOPT_RETURNTRANSFER =&gt; true,\n    CURLOPT_FOLLOWLOCATION =&gt; true,\n    CURLOPT_TIMEOUT        =&gt; 60,\n    CURLOPT_CONNECTTIMEOUT =&gt; 10,\n    CURLOPT_FORBID_REUSE   =&gt; false, \/\/ \u4fdd\u6301\u8fde\u63a5\u4e0d\u65ad\u5f00\n);\n\n\/\/ \u83b7\u53d6\u8bf7\u6c42\u7684\u8d44\u6e90\u8def\u5f84\n$resource = isset($_GET&#91;'resource']) ? $_GET&#91;'resource'] : null;\n\nif ($resource !== null) {\n    $cache_file = 'cache\/' . md5($resource) . '.' . pathinfo($resource, PATHINFO_EXTENSION);\n\n    \/\/ \u68c0\u67e5\u7f13\u5b58\u6587\u4ef6\u662f\u5426\u5b58\u5728\n    if (file_exists($cache_file)) {\n        \/\/ \u8fd4\u56de\u7f13\u5b58\u7684\u56fe\u7247\u8d44\u6e90\n        $content_type = pathinfo($cache_file, PATHINFO_EXTENSION);\n        header(\"Content-Type: image\/$content_type\");\n        readfile($cache_file);\n        exit;\n    } else {\n        \/\/ \u4ece\u8fdc\u7a0b\u670d\u52a1\u5668\u83b7\u53d6\u8d44\u6e90\u5e76\u7f13\u5b58\n        $ch = curl_init();\n        curl_setopt_array($ch, $curl_options);\n        curl_setopt($ch, CURLOPT_URL, 'http:\/\/remote_server' . $resource);\n        $response = curl_exec($ch);\n        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n        if ($response !== false &amp;&amp; $http_code === 200) {\n            \/\/ \u5c06\u8d44\u6e90\u7f13\u5b58\u5230\u672c\u5730\n            $dir = dirname($cache_file);\n            if (!is_dir($dir)) {\n                mkdir($dir, 0755, true);\n            }\n            file_put_contents($cache_file, $response);\n\n            \/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934\u5e76\u8f93\u51fa\u5185\u5bb9\n            $content_type = pathinfo($cache_file, PATHINFO_EXTENSION);\n            header(\"Content-Type: image\/$content_type\");\n            echo $response;\n            exit;\n        } else {\n            http_response_code($http_code);\n            echo 'Error fetching resource';\n            exit;\n        }\n    }\n}\n\n\/\/ \u5904\u7406\u5176\u4ed6\u7c7b\u578b\u7684\u8bf7\u6c42\n$request_uri = $_SERVER&#91;'REQUEST_URI'];\n$ch = curl_init();\ncurl_setopt_array($ch, $curl_options);\ncurl_setopt($ch, CURLOPT_URL, 'http:\/\/remote_server' . $request_uri);\n$response = curl_exec($ch);\n$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\nif ($response !== false &amp;&amp; $http_code === 200) {\n    \/\/ \u8bbe\u7f6e\u54cd\u5e94\u5934\u5e76\u8f93\u51fa\u5185\u5bb9\n    foreach (curl_getinfo($ch) as $key =&gt; $value) {\n        if (is_string($key) &amp;&amp; strpos($key, 'content_') === 0) {\n            header($key . ': ' . $value);\n        }\n    }\n    echo $response;\n} else {\n    http_response_code($http_code);\n    echo 'Error fetching resource';\n}\n\ncurl_close($ch);<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u5305\u542b\u4e86\u4ee5\u4e0b\u4f18\u5316\u63aa\u65bd:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Nginx \u914d\u7f6e\u4e2d\u542f\u7528\u4e86 Gzip \u538b\u7f29,\u53ef\u4ee5\u51cf\u5c0f\u54cd\u5e94\u5185\u5bb9\u7684\u5927\u5c0f\u3002<\/li>\n\n\n\n<li>\u56fe\u7247\u8d44\u6e90\u7684\u7f13\u5b58\u7b56\u7565\u5df2\u7ecf\u8bbe\u7f6e,\u5305\u62ec\u7f13\u5b58\u76ee\u5f55\u3001\u7f13\u5b58\u6587\u4ef6\u540d\u751f\u6210\u3001\u7f13\u5b58\u65f6\u95f4\u7b49\u3002<\/li>\n\n\n\n<li>PHP \u4ee3\u7801\u4e2d\u589e\u52a0\u4e86\u5bf9\u7f13\u5b58\u7684\u68c0\u67e5\u548c\u66f4\u65b0\u903b\u8f91,\u63d0\u9ad8\u4e86\u54cd\u5e94\u901f\u5ea6\u3002<\/li>\n\n\n\n<li>\u5bf9\u4e8e\u975e\u56fe\u7247\u8d44\u6e90\u7684\u8bf7\u6c42,\u4ecd\u7136\u4f1a\u8f6c\u53d1\u5230\u8fdc\u7a0b\u670d\u52a1\u5668\u5e76\u8fd4\u56de\u54cd\u5e94\u5185\u5bb9\u3002<\/li>\n<\/ol>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u6839\u636e\u5b9e\u9645\u9700\u6c42\u8fdb\u884c\u8fdb\u4e00\u6b65\u7684\u8c03\u6574\u548c\u4f18\u5316\u3002\u5982\u679c\u6709\u4efb\u4f55\u5176\u4ed6\u95ee\u9898,\u6b22\u8fce\u7ee7\u7eed\u63d0\u51fa\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684,\u6211\u6765\u4e3a\u60a8\u63d0\u4f9b\u4e00\u4e2a\u5b8c\u6574\u7684\u793a\u4f8b\u4ee3\u7801\u3002\u8fd9\u4e2a\u4ee3\u7801\u96c6\u6210\u4e86\u4e4b\u524d\u8ba8\u8bba\u7684\u4f18\u5316\u65b9\u6848,\u5305\u62ec Nginx \u914d\u7f6e\u548c PHP \u540e\u7aef [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-62","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/62","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":63,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/62\/revisions\/63"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}