Nginx

在負載均衡器後面時覆蓋 nginx 中的 $scheme

  • January 2, 2017

我有一個 nginx 伺服器位於負載均衡器後面。負載均衡器處理 SSL 終止,所有請求在埠 80 上命中 nginx。我還使用SRCache 模組使用 Redis 進行全頁記憶體。記憶體模組使用 URL 作為記憶體鍵,例如$schemeGET$host$request_uri. 我在想我可以$scheme以某種方式覆蓋 nginx 的變數,所以記憶體密鑰方案將https代替http我不確定如何做到這一點,或者它是否可能。

我的應用程序在各種事件後進行記憶體清除,它使用記憶體鍵生成記憶體鍵,https但 nginx在記憶體鍵中使用http記憶體。這意味著由於記憶體鍵名不匹配,記憶體沒有被正確清除。

如果有幫助,這是我的站點配置:

server {
listen 80;

server_name example.com example.org example.net ;

set $redirect_to_https 0;
if ( $http_x_forwarded_proto != 'https' ) {
 set $redirect_to_https 1;
}
if ( $request_uri = '/health-check.php' ) {
 set $redirect_to_https 0;
}
if ( $redirect_to_https = 1 ) {
 return 301 https://$host$request_uri;
}

# Uncomment the following line for domain mapping
server_name_in_redirect off;

access_log /var/log/nginx/example.com.access.log rt_cache_redis;
error_log /var/log/nginx/example.com.error.log;


root /var/www/example.com/htdocs;
index index.php index.html index.htm;

include  common/redis-php7.conf;

include common/wpcommon-php7-modified.conf;
include common/locations-php7.conf;
include /var/www/example.com/conf/nginx/*.conf;
}

更新這是記憶體配置

# Redis NGINX CONFIGURATION
# DO NOT MODIFY, ALL CHANGES LOST AFTER UPDATE EasyEngine (ee)
set $skip_cache 0;
# POST requests and URL with a query string should always go to php
if ($request_method = POST) {
 set $skip_cache 0;
}
if ($query_string != "") {
 set $skip_cache 1;
}
# Don't cache URL containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|wp-.*.php|index.php|/feed/|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
 set $skip_cache 1;
}
# Don't use the cache for logged in users or recent commenter
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
 set $skip_cache 1;
}
# Use cached or actual file if they exists, Otherwise pass request to WordPress
location / {
 try_files $uri $uri/ /index.php?$args;
}

location /redis-fetch {
   internal  ;
   set  $redis_key $args;
   redis_pass  redis;
}
location /redis-store {
   internal  ;
   set_unescape_uri $key $arg_key ;
   redis2_query  set $key $echo_request_body;
   redis2_query expire $key 14400;
   redis2_pass  redis;
}

location ~ \.php$ {
 set $key "nginx-cache:$scheme$request_method$host$request_uri";
 try_files $uri =404;

 srcache_fetch_skip $skip_cache;
 srcache_store_skip $skip_cache;

 srcache_response_cache_control off;

 set_escape_uri $escaped_key $key;

 srcache_fetch GET /redis-fetch $key;
 srcache_store PUT /redis-store key=$escaped_key;

 more_set_headers 'X-SRCache-Fetch-Status $srcache_fetch_status';
 more_set_headers 'X-SRCache-Store-Status $srcache_store_status';

 include fastcgi_params;
 fastcgi_pass php7;
}

好的,所以在這裡我們看到了用於 redis 查找的記憶體鍵:

location ~ \.php$ {
 set $key "nginx-cache:$scheme$request_method$host$request_uri";

問題在於,這$scheme反映了與 nginx 的連接(來自您的負載均衡器),但您的記憶體模組正在使用 from 的方案$http_x_forwarded_proto,這反映​​了實際使用的方案。

只需進行該更改就足夠了。

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