How to fix “serve static content from a cookieless domain” error on Linux

We will show you how to fix the “serve static content from a cookieless domain” error. When using a website analyzer tool like PageSpeed ​​Insights, GTMetrix, Pingdom, or Uptrends, there is often a warning that says: “Serve static content from a cookieless domain”This means your web server is setting cookies – this forces all future HTTP requests to include this cookie, creating unwanted network traffic. This, in turn, slows down your site’s speed when a user tries to access it. If you need your site to be as fast as possible, going to a cookie-free domain is the solution. The domain does not accept cookies from browsers, which reduces overall network traffic. The goal is to serve all static content – like images, javascript, CSS, etc. – through static, domain cookies, which helps to improve the loading speed of those resources.

In this article, we’ll take a look at how to configure domain cookies to allow this warning message. There are two ways to create a cookieless, use a new domain to serve static content, or use a subdomain to serve static content. Using a subdomain is the most popular choice as it doesn’t require buying a new domain name – that’s the method we’ll look at today. However, using a new domain instead of a subdomain can be made to work fairly easily with this article. This tutorial discusses how to set up a cookieless domain on a Linux based server.

Note: You cannot use a subdomain to serve static content if your main site sets up cookies using the domain root instead of the www prefix. If your site installs using yourdomain.ru, and not www.yourdomain.ru, then using a subdomain for static content is not possible. This is because using it in the root domain also applies to all subdomains.

Step 1. Create a subdomain

Subdomain creation can be accomplished using several methods. If you have a control panel like cPanel or DirectAdmin, there are built-in utilities to create subdomains quickly and easily. Use the built-in subdomain creation page to create a subdomain named “static”. When finished, your subdomain should look like this:

static.yourdomain.ru

After that, try accessing the new subdomain by entering the full URL in your web browser. You should welcome a completely white page.

If you don’t have a control panel to create your subdomain, that’s not a problem – just follow these next two steps. To create a subdomain without a control panel, you will need to edit your website’s configuration file. The location of the configuration file you need to edit or create depends on which web server software package you are using. In this tutorial, we’ll take a look at both NGINX and Apache. However, first you need to create a new folder that will contain all the site files in the new subdomain. / var / www / is a good place to create a new folder – name it “static.yourdomain.ru”.

Note You will need to place your static files from your site in this folder. Without this, your static content cannot be loaded from a static subdomain,

Creating a subdomain in Apache

To create a subdomain using Apache, we need to add a few extra lines to the existing website configuration. The location of the configuration file depends on which Linux distribution you are using. Here is the file path for RedHat based systems:

/etc/httpd/conf.d/static.yourdomain.conf

and for Debian based systems:

/etc/httpd/sites-available/static.yourdomain.conf

Open your website configuration file using your preferred text editor and add the following paragraph to the end of the file:

<VirtualHost *:80>
ServerName static.yourdomain.ru
DocumentRoot /var/www/static.yourdomain.ru
ErrorLog /your/error/log/path
CustomLog /your/requested/log/path combined
</VirtualHost>

Replace “yourdomain” with your domain name and provide the paths to their respective directories on the system. When this is done, you can restart Apache with the following command:

service httpd restart

Creating a subdomain in NGINX

With NGINX, we need to create a complete config file for this new subdomain. So, create and open a new file in / etc / nginx / sites – accessible with your preferred text editor and open the static.yourdomain.ru.conf file. Then add the following to your new file. (Don’t forget to change “yourdomain” to your domain name):

server {
        listen 80;
        
        server_name static.yourdomain.ru;

        log_not_found off; 
        error_log off; 
        fastcgi_hide_header Set-Cookie;

        root /var/www/static.yourdomain.ru;
        location / { if ($request_filename ~ ".(css|js|jpg|jpeg|gif|ico|png|bmp|pict|csv|doc|pdf|pls|ppt|tif|tiff|eps|ejs|swf|midi|mid|ttf|eot|woff|otf|svg|svgz|webp|docx|xlsx|xls|pptx|ps|class|jar|woff2|less|scss)$") { 
                add_header Pragma "public"; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; add_header Access-Control-Allow-Origin *; access_log off; expires 1M; break;
                   }
                return 302 http://www.yourdomain.ru$request_uri;
        }
}

Save this file and exit your text editor. The next step is to create a symbolic link (symbolic link) to enable this configuration in NGINX. You can do it with the following line:

ln -s /etc/sites-available/static.yourdomain.ru.conf /etc/sites-enabled/static.yourdomain.ru.conf

Now restart NGINX for the changes to take effect:

service nginx restart

Step 2. Configure DNS settings.

Now that the subdomain has been successfully configured, we now need to configure our DNS records to use that subdomain as an alias. We will do this using a CNAME record. Using the client tools that your hosting provider provides, you can easily create a CNAME record that points your subdomain to your main domain. Or, if you don’t have a control panel for your server, you can manually change your zone files. You need to add a line to the zone file that creates an alias to your primary domain. This is how it should look:

static.yourdomain.ru. IN CNAME yourdomain.ru.

Step 3. Set up the .htaccess file.

Now you need to add an extra line to your .htaccess file found at the root of your cookieless file. So, using your preferred text editor, open the .htaccess file in the folder where your current website is located and add this line to the end:

CookieDomain www.yourdomain.ru

The prefix “www” is important here. Without it, a static subdomain cannot function.

Note This only works for Apache. If you are using NGINX, the config file from previously was made for static content only, which means it can never accept cookies.

Step 4: Customize your site.

Now that the basic setup is done, you need to change the links on your site to point to the new static subdomain. From that moment on, he will receive his resources. For example, you would change this:

<link rel="stylesheet" type="text/css" href="https://andreyex.ru/css/main.css?x13673">

to the following:

<link rel="stylesheet" type="text/css" href="http://static.yourdomain.com/css/main.css">

Now when you try to access your site, it loads static content from the subdomain instead of the main domain. This has the added benefit of simultaneously loading site content when a user accesses your site.

Sidebar