Nginx(Openresty)代理,向静态文件添加自定义标头

Nginx(Openresty)代理,向静态文件添加自定义标头

我正在尝试向静态文件添加标题,我该如何正确地做到这一点?

我尝试过嵌套,但没有成功

server {


    location / {
        proxy_pass http://apache.backend.local/;
        proxy_no_cache $http_pragma $http_authorization;

        proxy_cache radish_cache;
        proxy_cache_bypass $http_cache_control;
        add_header X-Proxy-Cache $upstream_cache_status;

        proxy_redirect          off;

        proxy_pass_header Set-Cookie;

        proxy_ignore_headers Cache-Control Expires;

        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 X-Forwarded-Proto  $scheme;
        proxy_set_header X-Forwarded-Host   $host;
        proxy_set_header X-Forwarded-Port   $server_port;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header Accept-Encoding '';
        proxy_set_header Connection '';
        proxy_set_header Referer $http_referer;
        proxy_set_header Cookie $http_cookie;

        proxy_hide_header X-Powered-By;

        proxy_connect_timeout 59s;
        proxy_send_timeout 600;
        proxy_read_timeout 600;
        proxy_buffer_size 64k;
        proxy_buffers 16 32k;
        proxy_busy_buffers_size 128k;
        proxy_temp_file_write_size 64k;

        location ~* \.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso)$ {
            gzip_static off;
            #add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            access_log off;
            expires 30d;
            break;
        }

        location ~* \.(js)$ {
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            access_log off;
            expires 10d;
            break;
        }

        location ~* \.(css)$ {
            add_header Pragma public;
            add_header Cache-Control "public, must-revalidate, proxy-revalidate";
            access_log off;
            expires 10d;
            break;
        }
    }

}

还尝试不嵌套并在每个位置包含代理配置,然后我收到此错误

"proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except"

我正在使用最新的 openresty

答案1

/语句后面跟着的proxy_pass是错误信息中提到的“URI部分”。

在您的配置文件中,您有:

location / {
    proxy_pass http://apache.backend.local/;
    ...
}

/将(从)翻译location/(从proxy_pass语句)是没有意义的 - 因此“URI 部分”是不必要的,应该删除。

当你尝试proxy_pass在各种正则表达式 location块,“URI 部分”触发了错误消息。但由于您不需要proxy_pass执行任何 URI 转换,因此您可以安全地删除尾随的/

这个文件了解详情。

相关内容