说明:之前发过一个反代教程:Linux Centos下Nginx反代教程,现在发个Debian下反代HTTP(S)网站的教程。

安装nginx

系统要求:Debian 7

echo "deb http://packages.dotdeb.org wheezy all" >> /etc/apt/sources.list
apt-get update
apt-get install nginx
# 安装会提示输入两次 Y 来继续安装。

安装完毕之后输入nginx -v ,查看nginx的版本,确定是否安装完成。

修改配置文件

找到下面这个文件,然后修改。

vi /etc/nginx/sites-available/default

按照下面的示例修改完毕后就重启Nginx

service nginx restart

然后访问你的域名看一看是否成功镜像,需要注意的一点是,如果被镜像的网站设置了防盗链,那么静态文件(js/css/图片)可能无法显示,这就没办法了。

1、HTTP示例

一般情况下只需要更改这几个参数。

server_name 你的域名;
sub_filter 欲被镜像的域名 你的域名;
proxy_set_header Referer http://欲被镜像的域名
proxy_set_header Host 欲被镜像的域名
proxy_pass http://欲被镜像的域名

以下示例是以go.doubi.date镜像www.baidu.com为例。自行替换其中的参数:

第二段是屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致收录流失。

server
    {
        listen 80;
        server_name go.doubi.date;
        
        if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
        return  403;
        }
  
        location / {
        sub_filter www.baidu.com go.doubi.date;
        sub_filter_once off;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Referer http://www.baidu.com
        proxy_set_header Host www.baidu.com
        proxy_pass http://www.baidu.com
        proxy_set_header Accept-Encoding "";
        }
}

2、HTTPS示例

当你要镜像的网站不开放HTTP或者强制HTTPS的时候,你就需要加上SSL来转成HTTPS了。
假设SSL证书文件位置是:/root/ssl.crt
假设SSL密匙文件位置是:/root/ssl.key
第二段的301码是,强制走HTTPS,如果不需要可以去掉。
第三段是屏蔽搜索引擎收录,比如镜像自己的网站,如果不屏蔽会导致收录流失。
同时下面这两个选项的记得把http://改成https://

proxy_set_header Referer https://www.baidu.com
proxy_pass https://www.baidu.com
    server
        {
            listen 80;
            listen 443 ssl;
            ssl on;
            ssl_certificate /root/ssl.crt;
            ssl_certificate_key /root/ssl.key;
            ssl_session_cache shared:SSL:10m;
            ssl_session_timeout  10m;
            server_name go.doubi.date;
            add_header Strict-Transport-Security "max-age=31536000";
            
            if ( $scheme = http ){
                return 301 https://$server_name$request_uri;
            }
            
            if ($http_user_agent ~* (baiduspider|360spider|haosouspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler)) {
            return  403;
            }
      
            location / {
            sub_filter www.baidu.com go.doubi.date;
            sub_filter_once off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Referer https://www.baidu.com
            proxy_set_header Host www.baidu.com
            proxy_pass https://www.baidu.com
            proxy_set_header Accept-Encoding "";
            }
    }

  [1]: https://idcfq.com/364.html
Last modification:November 18, 2020
如果觉得我的文章对你有用,请随意赞赏