Loading... <strong>说明:</strong>之前发过一个反代教程:<a href="http://www.502435.com/1698.html" target="_blank" rel="noopener noreferrer">Linux Centos下Nginx反代教程</a>,现在发个<code>Debian</code>下反代<code>HTTP</code>(<code>S</code>)网站的教程。 <div data-unique="安装nginx"></div> <h2>安装nginx</h2> <strong>系统要求:</strong><code>Debian 7</code> <pre><code class="hljs cs">echo <span class="hljs-string">"deb http://packages.dotdeb.org wheezy all"</span> >> /etc/apt/sources.list apt-<span class="hljs-keyword">get</span> update apt-<span class="hljs-keyword">get</span> install nginx <span class="hljs-meta"># 安装会提示输入两次 Y 来继续安装。</span> </code></pre> 安装完毕之后输入<code>nginx -v</code> ,查看<code>nginx</code>的版本,确定是否安装完成。 <div data-unique="修改配置文件"></div> <h2>修改配置文件</h2> 找到下面这个文件,然后修改。 <pre><code class="hljs nginx"><span class="hljs-attribute">vi</span> /etc/nginx/sites-available/default </code></pre> 按照下面的示例修改完毕后就重启<code>Nginx</code>: <pre><code class="hljs nginx"><span class="hljs-attribute">service</span> nginx restart </code></pre> 然后访问你的域名看一看是否成功镜像,需要注意的一点是,如果被镜像的网站设置了防盗链,那么静态文件(<code>js</code>/<code>css</code>/图片)可能无法显示,这就没办法了。 <strong>1、HTTP示例</strong> 一般情况下只需要更改这几个参数。 <pre><code class="hljs nginx"><span class="hljs-attribute">server_name</span> 你的域名; <span class="hljs-attribute">sub_filter</span> 欲被镜像的域名 你的域名; <span class="hljs-attribute">proxy_set_header</span> Referer http://欲被镜像的域名 proxy_set_header Host 欲被镜像的域名 proxy_pass http://欲被镜像的域名</code></pre> 以下示例是以<code>go.doubi.date</code>镜像<code>www.baidu.com</code>为例。自行替换其中的参数: 第二段是屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致收录流失。 <pre><code class="hljs nginx"><span class="hljs-section">server</span> { <span class="hljs-attribute">listen</span> <span class="hljs-number">80</span>; <span class="hljs-attribute">server_name</span> go.doubi.date; <span class="hljs-attribute">if</span> (<span class="hljs-variable">$http_user_agent</span> <span class="hljs-regexp">~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler))</span> { <span class="hljs-attribute">return</span> <span class="hljs-number">403</span>; } <span class="hljs-attribute">location</span> / { <span class="hljs-attribute">sub_filter</span> www.baidu.com go.doubi.date; <span class="hljs-attribute">sub_filter_once</span> <span class="hljs-literal">off</span>; <span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable">$remote_addr</span>; <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable">$proxy_add_x_forwarded_for</span>; <span class="hljs-attribute">proxy_set_header</span> Referer http://www.baidu.com proxy_set_header Host www.baidu.com proxy_pass http://www.baidu.com proxy_set_header Accept-Encoding <span class="hljs-string">""</span>; } } </code></pre> <strong>2、HTTPS示例</strong> 当你要镜像的网站不开放<code>HTTP</code>或者强制<code>HTTPS</code>的时候,你就需要加上<code>SSL</code>来转成<code>HTTPS</code>了。 假设<code>SSL</code>证书文件位置是:<code>/root/ssl.crt</code>。 假设<code>SSL</code>密匙文件位置是:<code>/root/ssl.key</code>。 第二段的<code>301</code>码是,强制走<code>HTTPS</code>,如果不需要可以去掉。 第三段是屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致收录流失。 同时下面这两个选项的记得把<code>http://</code>改成<code>https://</code>。 <pre><code class="hljs nginx"><span class="hljs-attribute">proxy_set_header</span> Referer https://www.baidu.com proxy_pass https://www.baidu.com </code></pre> <pre><code class="hljs nginx"><span class="hljs-section">server</span> { <span class="hljs-attribute">listen</span> <span class="hljs-number">80</span>; <span class="hljs-attribute">listen</span> <span class="hljs-number">443</span> ssl; <span class="hljs-attribute">ssl</span> <span class="hljs-literal">on</span>; <span class="hljs-attribute">ssl_certificate</span> /root/ssl.crt; <span class="hljs-attribute">ssl_certificate_key</span> /root/ssl.key; <span class="hljs-attribute">ssl_session_cache</span> shared:SSL:<span class="hljs-number">10m</span>; <span class="hljs-attribute">ssl_session_timeout</span> <span class="hljs-number">10m</span>; <span class="hljs-attribute">server_name</span> go.doubi.date; <span class="hljs-attribute">add_header</span> Strict-Transport-Security <span class="hljs-string">"max-age=31536000"</span>; <span class="hljs-attribute">if</span> ( <span class="hljs-variable">$scheme</span> = http ){ <span class="hljs-attribute">return</span> <span class="hljs-number">301</span> https://<span class="hljs-variable">$server_name</span><span class="hljs-variable">$request_uri</span>; } <span class="hljs-attribute">if</span> (<span class="hljs-variable">$http_user_agent</span> <span class="hljs-regexp">~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler))</span> { <span class="hljs-attribute">return</span> <span class="hljs-number">403</span>; } <span class="hljs-attribute">location</span> / { <span class="hljs-attribute">sub_filter</span> www.baidu.com go.doubi.date; <span class="hljs-attribute">sub_filter_once</span> <span class="hljs-literal">off</span>; <span class="hljs-attribute">proxy_set_header</span> X-Real-IP <span class="hljs-variable">$remote_addr</span>; <span class="hljs-attribute">proxy_set_header</span> X-Forwarded-For <span class="hljs-variable">$proxy_add_x_forwarded_for</span>; <span class="hljs-attribute">proxy_set_header</span> Referer https://www.baidu.com proxy_set_header Host www.baidu.com proxy_pass https://www.baidu.com proxy_set_header Accept-Encoding <span class="hljs-string">""</span>; } } </code></pre> <strong>原文地址:</strong><a href="https://doub.io/wlzy-3/" target="_blank" rel="noopener noreferrer">https://doub.io/wlzy-3/</a> 最后修改:2022 年 08 月 02 日 © 允许规范转载 打赏 赞赏作者 赞 0 如果觉得我的文章对你有用,请随意赞赏