ご覧いただきありがとうございます。
現環境としてリバースプロキシとしてNginx、バックエンドにNodeを使用しています。
NodeやExpressの公式のリファレンスにもある通り、css、js、pngなどの静的ファイルをNodeで扱うのは不向きとの事なので、Nginxにやってもらおうと考えています。
Nginxの公式のリファレンスの元、色々とproxy_cacheについて調べ、実際に設定して見たのですが
うまくキャッシュできず困っております。
どなたかお知恵をお貸しいただければ幸いです。
Nginx
1#/etc/nginx/nginx.conf 2user nginx; 3worker_processes 1; 4 5error_log /var/log/nginx/error.log warn; 6pid /var/run/nginx.pid; 7 8events { 9 worker_connections 1024; 10} 11 12http { 13 include /etc/nginx/mime.types; #file type include. 14 default_type application/octet-stream; #??? 15 16 proxy_cache_path /var/cache/nginx/cache keys_zone=proxy:15m; #cache data place. 17 proxy_temp_path /var/cache/nginx/temp 1 2; #temp data place. 18 19 20 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 21 '$status $body_bytes_sent "$http_referer" ' 22 '"$http_user_agent" "$http_x_forwarded_for"'; 23 24 access_log /var/log/nginx/access.log main; #access data log place. 25 sendfile on; 26 keepalive_timeout 65; 27 include /etc/nginx/conf.d/node.conf; 28}
Nginx
1#/etc/nginx/conf.d/node.conf(nginx.confにてinclude) 2 3upstream node { 4 ip_hash; 5 server xxx.xx.xxx.xxx:3000; 6} 7 8server { 9 listen 80; 10 server_name xxx.xx.xxx.xxx; 11 location / { 12 proxy_redirect off; 13 set $do_not_cache 0; 14 if ($request_method != GET) { 15 set $do_not_cache 1; 16 } 17 if ($uri !~* ".(html|png|gif|css|js)$") { 18 set $do_not_cache 1; 19 } 20 proxy_no_cache $do_not_cache; 21 proxy_cache_bypass $do_not_cache; 22 proxy_cache proxy; 23 proxy_cache_key $scheme$host$uri$is_args$args; 24 proxy_cache_valid 200 1h; 25 proxy_cache_valid any 1m; 26 proxy_pass http://node; 27 28 #websoket grade up 29 proxy_set_header Upgrade $http_upgrade; 30 proxy_set_header Connection "upgrade"; 31 } 32}
js
1//app.js 2var express = require('express'); 3var app = express(); 4var http = require('http').Server(app); 5var io = require('socket.io')(http); 6var fs = require('fs'); 7 8const MongoClient = require('mongodb').MongoClient; 9const f = require('util').format; 10const assert = require('assert'); 11app.use(express.static('public')); 12 13 14 15app.get('/', function(req, res){ 16 res.sendFile(__dirname + '/index.html'); 17}); 18 19app.get('/page2',function(req,res){ 20 res.sendFile(__dirname + '/page2.html'); 21}); 22 23http.listen(3000, function(){ 24 console.log('listening on *:3000'); 25});
html
1<!--index.html--> 2<!doctype html> 3<html> 4 <head> 5 <title>Express Sample</title> 6 <link rel="stylesheet" href="stylesheets/index.css"> 7 </head> 8 <body> 9 <h1>Hello World</h1> 10 <a href="/page2">page2へ</a> 11 <script src="/socket.io/socket.io.js"></script> 12 <script src="https://code.jquery.com/jquery-1.11.1.js"></script> 13 <script src="/javascripts/index.js"></script> 14 </body> 15</html>
css
1//index.css 2h1{ 3 color: red; 4}
javascript
1$(function(){ 2 console.log('Hello Javascript'); 3});
tree
1//index.css,index.jsのディレクトリルート 2home 3 └ username 4 └ www 5 └ sample 6 └public 7 ├── stylesheets --index.css 8 ├── javascripts --index.js 9 └── images 10
一応上記のような設定、ファイルで行なっています。
症状としては、<script src="/socket.io/socket.io.js"></script>のキャッシュは
/var/cache/nginx/cacheにキャッシュファイルが確認できるのですが、
index.css、index.jsのキャッシュファイルが確認できませんでした。
望んでいる動作としては、キャッシュされていないcss,js,pngファイルはキャッシュさせ、
次回アクセス以降、Nginxからそのキャッシュファイルをクライアントへ返却したいです。
もし何か心当たりありましたらお知恵をお貸しいただければ幸いです。
あなたの回答
tips
プレビュー