nginx のバーチャルホストでキャッシュを取ることができない場合は header を確認してみる

nginx で複数のドメインでサイトを作っているのですが、リバースプロキシが上手く取れません。ちなみに、構築したい環境は以下のような感じです。要は一部のドメインはキャッシュを取って、一部のドメインはキャッシュを取らないという感じです。

  • Aサイト (リバースプロキシ有り)
  • Bサイト (リバースプロキシ有り)
  • Cサイト (リバースプロキシ無し)

現状以下の設定で example.com に関してはキャッシュが取れているのですが、example2.com についてはキャッシュがとれません。PHP とかは動くんですが、単純にまったくキャッシュが取られないような感じです。

/etc/nginx/nginx.conf の内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;
 
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
 
    access_log  /var/log/nginx/access.log  main;
 
    sendfile        on;
    server_tokens    off;
 
    keepalive_timeout   30;
 
    gzip  on;
    gzip_http_version 1.0;
    gzip_vary         on;
    gzip_comp_level   6;
    gzip_types        text/xml text/css application/xhtml+xml application/xml application/rss+xml application/atom_xml application/x-javascript application/x-httpd-php;
    gzip_disable      "MSIE [1-6].";
 
    proxy_cache_path  /var/cache/nginx levels=1:2 keys_zone=czone:4m max_size=50m inactive=120m;
    proxy_temp_path   /var/tmp/nginx;
    proxy_cache_key   "$scheme://$host$request_uri";
    proxy_set_header  Host               $host;
    proxy_set_header  X-Real-IP          $remote_addr;
    proxy_set_header  X-Forwarded-Host   $host;
    proxy_set_header  X-Forwarded-Server $host;
    proxy_set_header  X-Forwarded-For    $proxy_add_x_forwarded_for;
 
    upstream backend { server 127.0.0.1:8080; }
 
    include /etc/nginx/conf.d/*.conf;
 
}

/etc/nginx/conf.d/virtual.conf の内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# Aサイト
 
server {
 
    listen      80;
    server_name example.com;
    root        /var/www/vhosts/example.com/www;
    access_log  /var/log/nginx/example.com.access.log   main;
    error_log   /var/log/nginx/example.com.error.log;
    port_in_redirect off;
 
    location /wp-admin {
        proxy_pass http://backend;
    }
 
    location ~ .*.php {
        proxy_pass http://backend;
    }
 
    location / {
        set $mobile "";
        if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter)') {
            set $mobile "@ktai";
        }
        if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry)') {
            set $mobile "@mobile";
        }
        if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
            set $do_not_cache 1;
        }
 
        proxy_no_cache     $do_not_cache;
        proxy_cache_bypass $do_not_cache;
        proxy_cache czone;
        proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile";
        proxy_cache_valid  200 301 302 60m;
        proxy_cache_valid  404 5m;
        proxy_cache_use_stale  error timeout invalid_header updating
        http_500 http_502 http_503 http_504;
        proxy_pass http://backend;
        proxy_redirect off;
    }
 
    location ~ /purge(/.*) {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge czone "$scheme://$host$1$is_args$args$mobile";
    }
 
    location ~ (.ht|.git|.svn) {
        deny  all;
    }
 
}
 
server {
 
    listen      8080;
    server_name .example.com;
    root        /var/www/vhosts/example.com/www;
    access_log  /var/log/nginx/example.com.access.log   main;
    error_log   /var/log/nginx/example.com.error.log;
    client_max_body_size 36M;
    port_in_redirect off;
 
    location / {
        index  index.php index.html index.htm;
        if (-f $request_filename) {
            expires 14d;
            break;
        }
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?q=$1 last;
        }
    }
 
    location ~ .php$ {
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/example.com/www/$fastcgi_script_name;
    }
 
}
 
# Bサイト
 
server {
 
    listen      80;
    server_name example2.com;
    root        /var/www/vhosts/example2.com/www;
    access_log  /var/log/nginx/example2.com.access.log   main;
    error_log   /var/log/nginx/example2.com.error.log;
    port_in_redirect off;
 
    location /wp-admin {
        proxy_pass http://backend;
    }
 
    location ~ .*.php {
        proxy_pass http://backend;
    }
 
    location / {
        set $mobile "";
        if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter)') {
            set $mobile "@ktai";
        }
        if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|PSP|BlackBerry)') {
            set $mobile "@mobile";
        }
        if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
            set $do_not_cache 1;
        }
 
        proxy_no_cache     $do_not_cache;
        proxy_cache_bypass $do_not_cache;
        proxy_cache czone;
        proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile";
        proxy_cache_valid  200 301 302 60m;
        proxy_cache_valid  404 5m;
        proxy_cache_use_stale  error timeout invalid_header updating
        http_500 http_502 http_503 http_504;
        proxy_pass http://backend;
        proxy_redirect off;
    }
 
    location ~ /purge(/.*) {
        allow 127.0.0.1;
        deny all;
        proxy_cache_purge czone "$scheme://$host$1$is_args$args$mobile";
    }
 
    location ~ (.ht|.git|.svn) {
        deny  all;
    }
 
}
 
server {
 
    listen      8080;
    server_name .example2.com;
    root        /var/www/vhosts/example2.com/www;
    access_log  /var/log/nginx/example2.com.access.log   main;
    error_log   /var/log/nginx/example2.com.error.log;
    client_max_body_size 36M;
    port_in_redirect off;
 
    location / {
        index  index.php index.html index.htm;
        if (-f $request_filename) {
            expires 14d;
            break;
        }
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?q=$1 last;
        }
    }
 
    location ~ .php$ {
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/example2.com/www/$fastcgi_script_name;
    }
 
}
 
# Cサイト
 
server {
 
    listen      80;
    server_name example3.com;
    root        /var/www/vhosts/example3.com/www/app/webroot;
    access_log  /var/log/nginx/example3.com.access.log   main;
    error_log   /var/log/nginx/example3.com.error.log;
    client_max_body_size 36M;
    port_in_redirect off;
 
    location / {
        index  index.php index.html index.htm;
        if (-f $request_filename) {
            expires 14d;
            break;
        }
        if (!-e $request_filename) {
            rewrite ^(.+)$  /index.php?url=$1 last;
        }
    }
 
    location ~ .php$ {
        include        fastcgi_params;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_param  SCRIPT_FILENAME  /var/www/vhosts/example3.com/www/$fastcgi_script_name;
    }
}

で、色々調べていたら「header() が問題なんじゃない?」って助言を頂きまして調べてみたら下記のような header が勝手に出力されていました。

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0

これを除去した所普通にキャッシュを取ることができました。ゆえに、上記の設定は問題がないということです。キャッシュうんぬんで困ってる人は header を確認しましょう。

あと下記ページは必読です。

コメント

コメントは受け付けていません。