Nginx

哪些標頭優先 - 由 Nginx 或應用程序伺服器設置的標頭?

  • March 3, 2018

我在帶有 Rails 伺服器的 CentOS 上使用 Nginx。我對如何設置標題感到困惑。如果 Nginx 設置標頭和應用程序伺服器(本例中為 Ruby on Rails),哪一個勝出?我有這個 Nginx 伺服器塊

server {
 listen 80;
 server_name www.example.com;
 root /home/rails/scale_production/public; # I assume your app is located at this location

 location / {
   proxy_pass http://scale; # match the name of upstream directive which is defined above
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

   if ($request_uri ~* "($\/image\/.*)|(.*\.(ico|gif|jpe?g|png)$)") {
     expires 60d;
     access_log off;
     add_header Pragma public;
     add_header Cache-Control "public";
     break;
   }
 }

 location ~* ^/assets/ {
   # Per RFC2616 - 1 year maximum expiry
   expires 1y;
   add_header Cache-Control public;

   # Some browsers still send conditional-GET requests if there's a
   # Last-Modified header or an ETag header even if they haven't
   # reached the expiry date sent in the Expires header.
   add_header Last-Modified "";
   add_header ETag "";
   break;
 }

但是,當我呼叫與我的正則表達式之一匹配的 URL 時,我沒有看到記憶體標頭被設置….

localhost:tmp davea$ curl -I "http://www.example.com/people/image/27"
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Sat, 03 Mar 2018 18:20:43 GMT
Content-Type: image/jpeg; charset=binary
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Expires: Sun, 03 Mar 2019 18:20:43 GMT
Content-Disposition: inline; filename="Bill Smith"
Content-Transfer-Encoding: binary
Cache-Control: private
ETag: W/"b0c3f986a9c7f967e58733702e71a395"
X-Request-Id: 2f9728bb-3b6f-4d67-9344-afc1e29cacd5
X-Runtime: 0.007781

所以我想知道為什麼會這樣。我是在我的塊中做錯了什麼,還是在我的應用程序伺服器中設置了覆蓋 Nginx 標頭的標頭?

由於該正則表達式中的錯誤,您的標題未設置。刪除第一次出現的$. 僅當有一行正好在此位置結束時,包含的正則表達式$才會匹配。

關於標題覆蓋。這不像相同的標頭來自瀏覽器,通過伺服器並返回到同一個瀏覽器 - 這是沒有意義的。

請求標頭- 它們來自瀏覽器,您可以在傳遞到應用伺服器之前在 nginx 中覆蓋這些標頭。(主要例子:Host, Accept, User-Agent)。

還有響應頭- 它們是由應用伺服器創建的,您可以在將響應傳遞給瀏覽器之前在 nginx 中覆蓋這些頭。(這包括Expires等)

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