Tips&Tricks

Server Error 405 Method Not Allowed – Nginx and the HTTP OPTIONS Method

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!

View Comments

Share
Published by
Kamil

Recent Posts

SEO content: Keyword Selection for Content Marketing Success

SEO content, keyword selection... you probably hear these words everywhere in digital marketing. Everyone wants… Read More

6 years ago

A/B testing and conversion rate – what they are; with examples

If you own an online shop, you will certainly hear a lot about conversion and… Read More

6 years ago

How to track sales and automate order to cash processes in your online shop

You're starting to run an online shop with a few, several orders per day. You… Read More

6 years ago

How to promote your business if you don’t have a website?

Chances are that by now you have heard about the Coronavirus, also known as COVID-19.… Read More

6 years ago

How to get a SSL certificate for a website

Do you remember the little padlock that appears next to the website address? Look at… Read More

6 years ago

Real challenges of e-commerce – Part 3: Billing statements

So your packages are on the way! Or maybe even delivered! Amazing. Now it is… Read More

6 years ago

This website uses cookies.