Nginx反代cloudflare站点

laomoy
2023-10-26 / 1 评论 / 340 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2024年08月18日,已超过154天没有更新,若内容或图片失效,请留言反馈。

Nginx反代cloudflare站点报502或403错误解决办法

本文使用 https://node.aapanel.com/ 作为反代目标站点

以下是宝塔&aapanel的Nginx默认反代配置文件

#PROXY-START/

location ^~ /
{
    proxy_pass https://node.aapanel.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_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;
    #Persistent connection related configuration

    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    set $static_filevY6oU8Aw 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_filevY6oU8Aw 1;
        expires 1m;
    }
    if ( $static_filevY6oU8Aw = 0 )
    {
        add_header Cache-Control no-cache;
    }
}
#PROXY-END/

将上面的配置文件的$host改成你需要反代的目标网站

再加这个配置

    #反代CF增加规则
    proxy_ssl_name $host;
    #把$host改成目标域名
    proxy_ssl_server_name on;

完整的 全局 Nginx反代配置文件如下:

#PROXY-START/

location ^~ /
{
    proxy_pass https://node.aapanel.com;
    proxy_set_header Host node.aapanel.com;
    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_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;
    # proxy_hide_header Upgrade;
    #Persistent connection related configuration

    #反代CF增加规则
    proxy_ssl_name node.aapanel.com;
    #把$host改成目标域名
    proxy_ssl_server_name on;
    
    add_header X-Cache $upstream_cache_status;
    #Set Nginx Cache

    set $static_filevY6oU8Aw 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_filevY6oU8Aw 1;
        expires 1m;
    }
    if ( $static_filevY6oU8Aw = 0 )
    {
        add_header Cache-Control no-cache;
    }
}
#PROXY-END/

方法二(推荐临时使用)

指定 路径(aapanel) 全局反代


# PROXY-START/
location /aapanel/ {
    # 去掉 /aapanel 前缀后再转发
    rewrite ^/aapanel(/.*)$ $1 break;

    proxy_pass https://node.aapanel.com;
    proxy_set_header Host node.aapanel.com;
    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_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_http_version 1.1;

    proxy_ssl_name node.aapanel.com;
    proxy_ssl_server_name on;

    add_header X-Cache $upstream_cache_status;

    set $static_file9uY9s480 0;
    if ($uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$") {
        set $static_file9uY9s480 1;
        expires 1m;
    }
    if ($static_file9uY9s480 = 0) {
        add_header Cache-Control no-cache;
    }
}
# PROXY-END/

现在你就可以成功的反代cloudflare的站点了~

25

评论 (1)

取消
  1. 头像
    龙腾之父
    Windows 10 · Google Chrome

    催更

    回复