2

Server administration is absolutly not my domain - unfortunatly i am stuck with setting up a server that has nginx as static asset server and apache behind with php-fpm for serving php pages.

im trying to get proxy caching established and it works only if i dont use php sessions. i may be missing a point here but to my understanding i dont see why caching should not be possible providing a standard php session cookie not used for auth?

so i am supposing its a configuration issue but i am banging my head with this one. i post my settings here - hopefully someone can bump me in the right direction. i have a php file with nothing else but:

session_cache_limiter('public');
ini_set('session.cache_expire', 1000);
session_start();
echo "OK";
die();

the nginx.conf:

user www-data;
worker_processes 6;
worker_rlimit_nofile 50000;
pid /run/nginx.pid;

events {
        worker_connections  8096;
        multi_accept        on;
    use                 epoll;
}

http {
    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 15;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    log_format cache '***$time_local '
                   'req_time=$request_time '
                   '$upstream_cache_status '
                   'Cache-Control: $upstream_http_cache_control '
                   'Expires: $upstream_http_expires '
                   '"$request" ($status) '
                   '"$http_user_agent" ';

    access_log /var/log/nginx/cache.log cache;

    ##
    # Gzip Settings
    ##
        gzip on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;


    ##
    # Proxy Settings
    ##
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto $scheme;

    proxy_temp_path /var/data/nginx/tmp;
    proxy_cache_path /var/data/nginx/cache/proxy levels=1:2 keys_zone=proxy_cache:10m max_size=1024m inactive=60m use_temp_path=off;
    fastcgi_cache_path /var/data/nginx/cache/fcgi levels=1:2 keys_zone=fcgi_cache:10m max_size=1024m inactive=60m; add_header X-Cache $upstream_cache_status;

    ## cookie mapping
    map $http_cookie $session_cookie {
        default ""; 
        ~PHPSESSID=(?<sessionkey>[a-zA-Z0-9]+) $sessionkey;
    }

    ## mapping cache to request method
    map $request_method $no_cache {
        default 1; # by default do not cache
        HEAD 0; # cache HEAD requests
        GET 0; # cache GET requests
    }

    ## map purge request
    map $request_method $purge_method {
            PURGE   1;
            default 0;
    }

    # Allow underscores in header
       underscores_in_headers on;

        # Set Max Client Body size to 10 MB        
        client_max_body_size 10M;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

the vhost conf:

# http
server {
    listen 80 default_server;
    listen [::]:80 default_server;
    return 302 https://$server_name$request_uri;
}

# https
server {
    listen 443 ssl http2 default_server;
    listen [::]:443 ssl http2 default_server;

    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    root /var/www/;
    index index.php;

    sendfile           on;
    tcp_nopush         on;
    tcp_nodelay        on;
    keepalive_timeout  15;

    location / {
        access_log off;
        log_not_found off;
        try_files $uri $uri/ @apache;
        index index.html index.htm index.php;
    }
    location ~* \.(jpg|jpeg|gif|png|svg|css|js|ico|xml|woff|woff2|eot|ttf)$ {
        access_log          off;
        log_not_found       off;
        expires             30d;
        autoindex           off;
        add_header          Pragma "public";
        add_header          Cache-Control public; 
        add_header          Cache-Control "max-age=43200, public, must-revalidate, proxy-revalidate"; 
        gzip_static         on;
        gzip_min_length     1000;
        gzip_comp_level     2;
    }
    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }
    location = /robots.txt {
        access_log off;
        log_not_found off;
    }
    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
    location ~*|\.php*$ {
        access_log off;
        log_not_found off;

        # Apache reverse proxy and caching turn proxy buffering to "on" to enable
        proxy_buffering on;
        include /etc/nginx/proxy_params;

        proxy_pass https://127.0.0.1:8088;
        proxy_cache proxy_cache;
        proxy_cache_revalidate on;
        proxy_cache_lock on;
        proxy_cache_valid 200 302 5m;
        proxy_cache_valid 404 1m;
        proxy_cache_valid any 3m;
        proxy_cache_min_uses 3;
        proxy_cache_key $proxy_host$request_uri$session_cookie;
    }
    location @apache {
        access_log off;
        log_not_found off;

        # Apache reverse proxy and caching turn proxy buffering to "on" to enable
        proxy_buffering on;
        include /etc/nginx/proxy_params;
        proxy_pass https://127.0.0.1:8088;
    }
}

and the proxy params:

client_max_body_size 100M;
client_body_buffer_size 1m;
proxy_intercept_errors on;
proxy_buffer_size 128k;
proxy_buffers 256 16k;
proxy_busy_buffers_size 256k;
proxy_temp_file_write_size 256k;
proxy_max_temp_file_size 10m;
proxy_read_timeout 300;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;

add_header X-Cache-Status $upstream_cache_status;
add_header X-Handled-By $proxy_host;

i keep on getting a cache miss:

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 22 Nov 2017 12:27:25 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Set-Cookie: PHPSESSID=c183rru5i3fgj5map7jqgbf2do; path=/
Expires: Thu, 23 Nov 2017 05:07:25 GMT
Cache-Control: public, max-age=60000
Last-Modified: Wed, 22 Nov 2017 12:20:41 GMT
Vary: Accept-Encoding
X-Cache-Status: MISS
X-Handled-By: 127.0.0.1:8088

Important: the host runs under ssl

does any body have a hint for me here? thank you

0

From what I get, you want to cache @apache location but it fails, right? If that your problem, then its because you are not activating nginx cache in @apache location. You need to add proxy_cache proxy_cache; to /etc/nginx/proxy_params. If that is not what you want to do, please describe your problem or what you wanna do more clearly.

0

it turned out i was too stupid to realize that via doing a curl request would not permit session storage (naturally) so every request a cache hit since no cookie can be set that way. and since i include the session id in the cache key it wont work ;)

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.