Nginx

Nginx ( Openresty ) 代理,添加自定義頭到靜態文件

  • July 19, 2019

我正在嘗試將標題添加到靜態文件,我該如何正確地做到這一點?

我試過嵌套但沒有用

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

語句的結尾/proxy_pass錯誤消息中提到的“URI 部分”。

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

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

/將(從location)轉換為(/proxy_pass語句)是沒有意義的——所以“URI 部分”是不必要的,應該刪除。

當您嘗試proxy_pass在各種正則表達式 location塊中實現時,“URI 部分”觸發了錯誤消息。但由於您不需要proxy_pass執行任何 URI 轉換,您可以安全地刪除尾隨/.

有關詳細資訊,請參閱此文件

引用自:https://serverfault.com/questions/975946