nginx存在文件直接返回,不存在的时候才执行反向代理

当开启反向代理以后,根目录的文件就无法访问了,这个时候可以使用这个技巧来实现。
当文件存在的时候,不走反向代理,直接返回;
当文件不存在的时候,正常走反向代理;

#PROXY-START/
location /{
    try_files $uri @goproxy;
}
#反向代理
location @goproxy{
    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_pass http://127.0.0.1:8080;
    # proxy_hide_header Upgrade;

    add_header X-Cache $upstream_cache_status;

    #Set Nginx Cache
    set $static_fileGQUxlfBU 0;
    if ( $uri ~* "\.(gif|png|jpg|css|js|woff|woff2)$" )
    {
        set $static_fileGQUxlfBU 1;
        expires 1m;
    }
    if ( $static_fileGQUxlfBU = 0 )
    {
        add_header Cache-Control no-cache;
    }
}
#PROXY-END/
最后修改:2023 年 08 月 06 日 10 : 11 AM
如果觉得我的文章对你有用,请随意赞赏

发表评论