docker-php icon indicating copy to clipboard operation
docker-php copied to clipboard

NGINX: 405 Method Not allowed when using OPTIONS request to "/" vs "index.php"

Open jaydrogers opened this issue 3 years ago • 5 comments

Affected Docker Images

*-fpm-nginx

Current Behavior

When an OPTIONS request is made to a URL NOT ending in .php, a "405 Method Not Allowed" is returned.

Expected Behavior

When making an OPTIONS request, it should return a "200".

Steps To Reproduce

  1. Use the cores-issue branch for latest code: https://github.com/jaydrogers/docker-php-test-app/tree/cors-issue
  2. Use docker compose up to bring up the containers
  3. Test with Insomnia

Request to http://localhost

Result is 405. image

Request to http://localhost/index.php

Result is 200. image

Host Operating System

N/A

Docker Version

N/A

Anything else?

This issue was originally reported by @zigazajc007 via Discord.

Possible causes

  • It could be related to these lines? https://github.com/serversideup/docker-php/blob/main/src/fpm-nginx/etc/nginx/site-opts.d/http.conf#L29:L38

Possible solutions

  • http://invalidlogic.com/2011/04/12/serving-static-content-via-post-from-nginx/
  • https://stackoverflow.com/questions/24415376/post-request-not-allowed-405-not-allowed-nginx-even-with-headers-included

Hesitations on solutions

It seems these might be quite "hacky". Would love to open this up to the community for further discussion on a stable solution.

jaydrogers avatar Nov 03 '22 20:11 jaydrogers

Hello,

For my application / needs this solved the issue:

# Have NGINX try searching for PHP files as well
location / {
	try_files $uri /index.php?$query_string;
}

So basically when you access / location it will redirect user to index.php with GET parameters included.

zigazajc007 avatar Nov 04 '22 06:11 zigazajc007

To add to this, I did more research to see what the industry is doing.

Digital Ocean acquired an NGINX configuration generator: https://www.digitalocean.com/community/tools/nginx?domains.0.php.phpServer=127.0.0.1%3A9000

In their example, have their config exactly how I have it (which I've seen in many other examples too).

Screen Shot 2022-11-09 at 11 30 48

I'm hesitant to remove $uri/ because I am unsure of the "blowback consequences".

Thoughts?

jaydrogers avatar Nov 09 '22 17:11 jaydrogers

I talked with Andy Dawson from Nginx Community on Slack and here are some screenshots: image

image

https://stackoverflow.com/questions/58003172/nginx-return-405-not-allowed-while-using-try-files-and-proxy-pass/58007153#58007153

Based on some research, this problem has been solved by removing $uri/ from try_files if your application does not require it.

My suggestion is to make another environment variables called something like SINGLE_PAGE_APPLICATION, SINGLE_PAGE_APP, NGINX_SINGLE_PAGE_APP, SPA or NGINX_SPA. If this variable is set to true then conf should look something like this:

# Have NGINX try searching for PHP files as well
location / {
	try_files $uri /index.php?$query_string;
}

if it's set to false (Should be by default) then you can leave it like it's now:

# Have NGINX try searching for PHP files as well
location / {
	try_files $uri $uri/ /index.php?$query_string;
}

zigazajc007 avatar Nov 10 '22 13:11 zigazajc007

image

https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#front-controller-pattern-web-apps

zigazajc007 avatar Nov 10 '22 17:11 zigazajc007

Thank you so much for diving in deep on this and publishing official resources to reference.

I am going to ping @danpastori on this to review, but unfortunately he's quite tied up right now. The next free moment he has I have some Laravel specific questions on why we're not able to experience this issue in Laravel.

We appreciate your patience and dedication to resolving this the right way in an issue that's deep into the web application stack 😃

Possible workaround

In your own image, I wonder if you could try using sed to change that line (like what I do here: https://github.com/serversideup/docker-php/blob/main/src/fpm-nginx/etc/s6-overlay/scripts/debug-nginx#L4)?

jaydrogers avatar Nov 11 '22 14:11 jaydrogers