宝塔面板使用Nginx进行反代,提升网站访问速度和增强防护;反代Cloudflare CDN设置

1、假设你有2台VPS,一台为Linode日本,IP为1.1.1.1(国内直接访问速度慢),一台为香港CN2,IP为2.2.2.2(国内访问速度快)

网站建在Linode日本VPS上,域名为 xxxxx.com。

在香港CN2 VPS上安装宝塔面板,并安装Nginx。

2、在你的网站域名解析处,域名A记录设置为香港CN2 VPS的IP:2.2.2.2

3、在香港CN2 VPS宝塔面板页面设置:

新建网站 xxxxx.com,SSL证书,反向代理设置为 xxxxx.com,并开启缓存

修改 /etc/hosts 文件,在文件末尾添加如下信息:

1.1.1.1 xxxxx.com
1.1.1.1 www.xxxxx.com

4、文件缓存位置默认为  /www/server/nginx/proxy_cache_dir

反代配置文件为 /www/server/nginx/conf/proxy.conf

5、这样别人访问你的网站时就是香港CN2的速度,比直接访问Linode日本快多了。

建议,最好是位置相近的区域进行反代。另外,前端的节点可以有多个,也是同样的设置。

本文主要参考了:https://www.openos.org/post/107/

6、宝塔面板使用Nginx反代Cloudflare CDN设置

同样是上面的设置,然后修改网站 配置文件 ,替换为下面的代码:(主要是#PROXY-START —— #PROXY-END的内容)

a、自己域名反代自己在cloudflare的域名进行加速(把其中 你的域名 替换)

#PROXY-START/
location  ~* \.(php|jsp|cgi|asp|aspx)$
{
    proxy_pass https://你的域名;
    proxy_set_header Host 你的域名;
    #向后端传递访客ip
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    #向后端传递访客ip
   
    proxy_ssl_name 你的域名;
    proxy_ssl_server_name on;

}
location /
{
    proxy_pass https://你的域名;
    proxy_set_header Host 你的域名;
    #向后端传递访客ip
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    #向后端传递访客ip
   
    proxy_ssl_name 你的域名;
    proxy_ssl_server_name on;


    #缓存设置
    add_header X-Cache $upstream_cache_status;
        #Set Nginx Cache

    proxy_ignore_headers Set-Cookie Cache-Control expires;
    proxy_cache cache_one;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 304 301 302 120m;
    expires 12h;
}

#PROXY-END/

b、自己的域名反代别人在cloudflare的域名

#PROXY-START/
location  ~* \.(php|jsp|cgi|asp|aspx)$
{
    proxy_pass https://对方的域名;
    proxy_set_header Host 对方的域名;
    #向后端传递访客ip
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    #向后端传递访客ip
   
    proxy_ssl_name 对方的域名;
    proxy_ssl_server_name on;

}
location /
{
    proxy_pass https://对方的域名;
    proxy_set_header Host 对方的域名;
    #向后端传递访客ip
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    #向后端传递访客ip
   
    proxy_ssl_name 对方的域名;
    proxy_ssl_server_name on;


    #缓存设置
    add_header X-Cache $upstream_cache_status;
        #Set Nginx Cache

    proxy_ignore_headers Set-Cookie Cache-Control expires;
    proxy_cache cache_one;
    proxy_cache_key $host$uri$is_args$args;
    proxy_cache_valid 200 304 301 302 120m;
    expires 12h;
}

#PROXY-END/

转自:https://hostloc.com/thread-643951-5-1.html

c、

#PROXY-START/
location /
{
    #缓存媒体文件
    location ~* .jpg|.png|.gif|.jpeg|.ttf|.webp|.woff|.woff2|.ico$
    {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass https://zhujiwiki.com;
        proxy_set_header Host zhujiwiki.com;
        proxy_cache cache_one;
    	proxy_cache_valid 1d;
        proxy_ignore_headers Cache-Control;
    	add_header X-Cache "$upstream_cache_status from $server_addr";
    }
    
    #缓存css、js、html
     location ~* .css|.js|.html$
    {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header REMOTE-HOST $remote_addr;
        proxy_pass https://zhujiwiki.com;
        proxy_set_header Host zhujiwiki.com;
        proxy_cache cache_one;
    	proxy_cache_valid 1d;
        proxy_ignore_headers Cache-Control;
    	add_header X-Cache "$upstream_cache_status from $server_addr";
    }
    
    proxy_pass https://zhujiwiki.com;
    proxy_set_header Host zhujiwiki.com;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header REMOTE-HOST $remote_addr;
    
    #持久化连接相关配置
    #proxy_connect_timeout 30s;
    #proxy_read_timeout 86400s;
    #proxy_send_timeout 30s;
    #proxy_http_version 1.1;
    #proxy_set_header Upgrade $http_upgrade;
    #proxy_set_header Connection "upgrade";

    #add_header X-Cache $upstream_cache_status;

    proxy_ssl_name zhujiwiki.com;
    proxy_ssl_server_name on;
 

    #expires 12h;
}

#PROXY-END/
    所有主机信息仅是为及时分享并收录,不做推荐!
    专门搜集、分享国内高速、稳定的香港、日本、韩国、新加坡、美西CN2等优质线路的主机:FastVM