在负载均衡器后面覆盖nginx中的$scheme

前端之家收集整理的这篇文章主要介绍了在负载均衡器后面覆盖nginx中的$scheme前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我有一个Nginx服务器坐在负载均衡器后面.负载均衡器处理SSL终止,所有请求都在端口80上命中Nginx.我还使用SRCache module使用Redis进行整页缓存.缓存模块使用URL作为缓存键,如$schemeGET $host $request_uri.我以为我可以以某种方式覆盖Nginx的$scheme变量,因此缓存密钥方案将是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(来自你的负载均衡器)的连接,但你的缓存模块正在使用来自$http_x_forwarded_proto的方案,它反映了实际使用的方案.

只做出改变就足够了.

猜你在找的Nginx相关文章