Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jaydrogers opened this issue Nov 3, 2022 · 5 comments
Assignees
Labels
🐛 Bug: Confirmed Bugs that are able to be replicated. 🙏 Help Wanted Issues that specifically could use some help from the community

Comments

@jaydrogers
Copy link
Member

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

Possible solutions

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 jaydrogers added the 🐛 Bug: Confirmed Bugs that are able to be replicated. label Nov 3, 2022
@jaydrogers jaydrogers self-assigned this Nov 3, 2022
@jaydrogers jaydrogers added 📣 Feedback Request 🙏 Help Wanted Issues that specifically could use some help from the community labels Nov 3, 2022
@zigazajc007
Copy link

zigazajc007 commented Nov 4, 2022

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.

@jaydrogers
Copy link
Member Author

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?

@zigazajc007
Copy link

zigazajc007 commented Nov 10, 2022

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;
}

@jaydrogers
Copy link
Member Author

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)?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 Bug: Confirmed Bugs that are able to be replicated. 🙏 Help Wanted Issues that specifically could use some help from the community
Projects
None yet
Development

No branches or pull requests

2 participants