We have recently came across a new problem which can best be described as “Server Error 405”. Upon investigation we have discovered that our Nginx server is not accepting request method called “OPTIONS”.

Today we wanted to show you how we quickly dealt with that problem by modifying Nginx server configuration.

You need to know that 405 response is not 404 (not found). In case of 405 the file that you are trying to access is available on the server but server is not allowing you to access it with the OPTIONS method.

In order to fix that problem we have modified our Nginx configuration that usually can be found in /etc/nginx/nginx.conf or on a site configuration level, in our case /etc/nginx/conf.d/magento2.conf

Since we had problems with accessing files in the magento 2 static folder we have modified the following section of the magento 2 config file:

location /static/ {
    # Uncomment the following line in production mode
    expires max;
    # Remove signature of the static files that is used to overcome the browser cache
    location ~ ^/static/version {
        rewrite ^/static/(version\d*/)?(.*)$ /static/$2 last;
    }
    location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ {
        add_header Cache-Control "public";
        add_header X-Frame-Options "SAMEORIGIN";
        expires off;
        if (!-f $request_filename) {
            rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    location ~* \.(zip|gz|gzip|bz2|csv|xml)$ {
        add_header Cache-Control "no-store";
        add_header X-Frame-Options "SAMEORIGIN";
        expires    off;
        if (!-f $request_filename) {
           rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
        }
    }
    if (!-f $request_filename) {
        rewrite ^/static/?(.*)$ /static.php?resource=$1 last;
    }
    add_header X-Frame-Options "SAMEORIGIN";
}

Depending on your magento¬†2 configuration you should see something similar and the only thing that you have to do now is to place the following code inside the “location” block:

if ($request_method = OPTIONS ) {
        add_header Content-Length 0;
        add_header Content-Type text/plain;
        return 200;
}

After saving your changes you need to reload Nginx or simply restart it and your server should now be accepting OPTIONS requests for static files. The above line is instructing Nginx that in case of OPTIONS request it should add special headers to instruct the browser that content is available and does not require special encoding and then return 200 code to confirm that everything is fine and we can process it.

Did you have similar problems in the past? How did you handle them? Please share in comments, we will be more than happy to hear!