1

I'm trying to use nginx as proxy cache for tomcat, using two separate containers. I've created a nginx container from official nginx image; same for tomcat, listening on port 8080.

Now, how should i configure nginx as proxy cache for tomcat content?

Contents in tomcat webserver are in the path: localhost:8080/shaka-player-master/demo/

This is nginx.conf in /etc/nginx:

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {

    proxy_cache_path /tmp/nginx levels=1:2 keys_zone=my_zone:10m inactive=60m;
    proxy_cache_key "$scheme$request_method$host$request_uri";

    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;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

I have to tell that there are, from nginx docker official image, folders /etc/nginx/sites-available nor /etc/nginx/site-enabled.

I want tell that in path /usr/share/nginx/html/ of nginx container there is only an index.html created automatically from nginx image. The unique other configuration file is under /etc/nginx/conf.d/default.conf... :

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    location / {
         proxy_cache my_zone;
         add_header X-Proxy-Cache $upstream_cache_status;

         include proxy_params;
         proxy_pass http://localhost:8080/shaka-player-master/demo;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

My gol is: when i run http://localhost:9000 (or some other path on the same port), the first time (cache miss) i could give content from localhost:8080/shaka-player-master/demo/index.html

and then, when i reload localhost:9000, there is a cache hit.

I would use nginx as proxy cache because i would to setup a CDN using more nginx instances (containers).

I hope someone could help me in that nginx configuration, because i'm spending a lot of time trying to fix this problem.

Thanks.

0

I don't see anything in your config that would enable caching. The definition of proxy is: a server that acts as an intermediary. Sometimes it can cache, sometimes it does other intermediary stuff. The proxy_pass doesn't implement caching.

The simplest ngingx caching is proxy_cache_path plus proxy_cache.

  • Thanks for your answer, @kubanczyk. I've already read your link and i've found this: serversforhackers.com/nginx-caching . I've edited my two config files.. do you think it's all correct or there are some other problems? – pier92 Dec 20 '16 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.