3

This isn't Craft issue per se. But I was wondering if anyone can help me out. I’m trying to enable FastCGI Cache in my server. My nginx server block is coming from this helpful article: https://nystudio107.com/blog/static-caching-with-craft-cms.

The caching works because the site is now considerably faster. The issue is when I try to login in Craft's login page: /admin/login, it goes to /admin but it goes back to /admin/login/ not to the dashboard page.

I have reached out to the article's author and he responded that,

it isn't properly not stripping cookies off of the admin cp requests.

Here's my server block:

fastcgi_cache_path /etc/nginx/cache/site levels=1:2 keys_zone=SITE:100m inactive=30m;

server {
    listen 80;
    access_log /var/log/nginx/site.access.log;
    error_log  /var/log/nginx/site.error.log;
    root /var/www/site/public;
    index index.html index.htm index.php;
    charset utf-8;
    ssi on;
    server_name site.ac;
    #404 error handler
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    #Cache everything by default
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    add_header X-Cache $upstream_cache_status;
    set $no_cache 0;
    if ($request_method = POST)
    {
        set $no_cache 1;
    }
    if ($request_uri ~* "/admin/|eecms/|actions/|cpresources/")
    {
        set $no_cache 1;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    # Craft-specific location handlers to ensure AdminCP requests route through index.php
    # If you change your `cpTrigger`, change it here as well
    location ^~ /admin {
        try_files $uri $uri/ @phpfpm_nocache;
    }
    location ^~ /index.php/admin {
        try_files $uri $uri/ @phpfpm_nocache;
    }
    location ^~ /cpresources {
        try_files $uri $uri/ /index.php?$query_string;
    }
    location ^~ /eecms {
        try_files $uri $uri/ @phpfpm_nocache;
    }
    location ^~ /index.php/eecms {
        try_files $uri $uri/ @phpfpm_nocache;
    }
    #php-fpm configuration
    location ~ [^/]\.php(/|$) {
        try_files $uri $uri/ /index.php?$query_string;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        # php-fpm parameters
        fastcgi_param HTTP_PROXY "";
        fastcgi_param CRAFTENV_CRAFT_ENVIRONMENT "";
        fastcgi_param CRAFTENV_DB_HOST "127.0.0.1";
        fastcgi_param CRAFTENV_DB_NAME "x";
        fastcgi_param CRAFTENV_DB_USER "x";
        fastcgi_param CRAFTENV_DB_PASS "x";
        fastcgi_param CRAFTENV_SITE_URL "http://site.ac/";
        fastcgi_param CRAFTENV_BASE_URL "http://site.ac/";
        fastcgi_param CRAFTENV_BASE_PATH "/var/www/site/public/";
        # shared php-fpm configuration
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        # FastCGI Cache settings
        fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        fastcgi_cache SITE;
        fastcgi_hide_header Set-Cookie;
        fastcgi_cache_valid 200 30m;
        fastcgi_cache_use_stale updating error timeout invalid_header http_500;
        fastcgi_cache_bypass $no_cache;
        fastcgi_no_cache $no_cache;
    }
    # php-fpm configuration for non-cached content
    location @phpfpm_nocache {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
        fastcgi_param PATH_INFO $query_string;
        # php-fpm parameters
        fastcgi_param HTTP_PROXY "";
        fastcgi_param CRAFTENV_CRAFT_ENVIRONMENT "";
        fastcgi_param CRAFTENV_DB_HOST "127.0.0.1";
        fastcgi_param CRAFTENV_DB_NAME "x";
        fastcgi_param CRAFTENV_DB_USER "x";
        fastcgi_param CRAFTENV_DB_PASS "x";
        fastcgi_param CRAFTENV_SITE_URL "http://site.ac/";
        fastcgi_param CRAFTENV_BASE_URL "http://site.ac/";
        fastcgi_param CRAFTENV_BASE_PATH "/var/www/site/public/";
        # shared php-fpm configuration
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        # No FastCGI Cache
        fastcgi_cache_bypass 1;
        fastcgi_no_cache 1;
    }
    location ~ /\.ht {
        deny all;
    }
}

I hope someone can shed some light on this issue I'm having. Any suggestions or comments is greatly appreciated.

  • Is this a Craft site? What is the eecms page? – andrew.welch Jul 12 '17 at 5:45
  • Yes, it's a Craft site. I changed the cpTrigger to eecms. – Anya Dit Jul 12 '17 at 6:09
4

The issue had something to do with PATH_INFO. Adding 'usePathInfo => true' in general.php fixed the issue. Here's the detailed explanation from Craft docs: https://craftcms.com/support/enable-path-info.

  • 1
    Ah glad you figured it out, thanks for sharing the answer. – andrew.welch Jul 16 '17 at 20:20
  • @khalwat I should be the one thanking you. I love your articles. I learned a lot from them. – Anya Dit Aug 5 '17 at 11:12
1

You might also need to add something like this for your actions pages:

location ^~ /actions/users/setpassword {
    try_files $uri $uri/ @phpfpm_nocache;
}

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.