R
R
Ross Alex2021-07-17 19:27:26
PHP
Ross Alex, 2021-07-17 19:27:26

How to skip OPTIONS request in php-fpm in NGINX?

Good afternoon, colleagues!

NGINX config setup question: how to make PUT, PATCH, HEAD, DELETE, OPTIONS and other requests, besides GET and POST, reach php-fpm?

Thanks for the help

upd1 : not interested in the answer of NGINX itself, like here . It is necessary that PHP see all these requests!
upd2 : cross-domain requests. For example, from test1.ru to api.domain.com
Solution : You cannot send requests other than GET/POST/HEAD to / host address.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Karabanov, 2021-07-18
@Wacdis

You don't have to intentionally do anything to enable these methods, as they are available by default. Here's what to disable some methods, but you have to make an effort.
Let me give you an example to prove my point:

docker-compose.yml

version: '3'

services:
  alpine-nginx:
    image: nginx:stable-alpine
    container_name: alpine-nginx
    restart: "no"
    links:
      - composer-php
    volumes:
      - ./default.conf:/etc/nginx/conf.d/default.conf
      - ./code:/code
    ports:
      - "80:80"

  composer-php:
    image: php:7-fpm
    container_name: composer-php
    restart: "no"
    volumes:
      - ./code:/code
    ports:
      - "9000:9000"


default.conf

server {
  listen 80;
  listen [::]:80;

  client_max_body_size 2M;

  server_name domain.com admin.domain.com www.domain.com api.domain.com;

  charset utf-8;
  index index.php;

  root /code;

  gzip on;
  location / {
    try_files $uri $uri/ @phpindex;
  }

  location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass composer-php:9000;
    fastcgi_index index.php;
    include fastcgi_params;

    fastcgi_param   QUERY_STRING            $query_string;
    fastcgi_param   REQUEST_METHOD          $request_method;
    fastcgi_param   CONTENT_TYPE            $content_type;
    fastcgi_param   CONTENT_LENGTH          $content_length;
    fastcgi_param   SCRIPT_FILENAME         $realpath_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME             $fastcgi_script_name;
    fastcgi_param   PATH_INFO               $fastcgi_path_info;
    fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;
    fastcgi_param   REQUEST_URI             $request_uri;
    fastcgi_param   DOCUMENT_URI            $document_uri;
    fastcgi_param   DOCUMENT_ROOT           $realpath_root;
    fastcgi_param   SERVER_PROTOCOL         $server_protocol;
    fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
    fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;
    fastcgi_param   REMOTE_ADDR             $remote_addr;
    fastcgi_param   REMOTE_PORT             $remote_port;
    fastcgi_param   SERVER_ADDR             $server_addr;
    fastcgi_param   SERVER_PORT             $server_port;
    fastcgi_param   SERVER_NAME             $server_name;
  }

  location @phpindex {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass composer-php:9000;
    fastcgi_index index.php;
    include fastcgi_params;

    set $index_name /index.php;

    fastcgi_param   QUERY_STRING            $query_string;
    fastcgi_param   REQUEST_METHOD          $request_method;
    fastcgi_param   CONTENT_TYPE            $content_type;
    fastcgi_param   CONTENT_LENGTH          $content_length;
    fastcgi_param   SCRIPT_FILENAME         $realpath_root$index_name;
    fastcgi_param   SCRIPT_NAME             $index_name;
    fastcgi_param   PATH_INFO               $fastcgi_path_info;
    fastcgi_param   PATH_TRANSLATED         $document_root$fastcgi_path_info;
    fastcgi_param   REQUEST_URI             $request_uri;
    fastcgi_param   DOCUMENT_URI            $document_uri;
    fastcgi_param   DOCUMENT_ROOT           $realpath_root;
    fastcgi_param   SERVER_PROTOCOL         $server_protocol;
    fastcgi_param   GATEWAY_INTERFACE       CGI/1.1;
    fastcgi_param   SERVER_SOFTWARE         nginx/$nginx_version;
    fastcgi_param   REMOTE_ADDR             $remote_addr;
    fastcgi_param   REMOTE_PORT             $remote_port;
    fastcgi_param   SERVER_ADDR             $server_addr;
    fastcgi_param   SERVER_PORT             $server_port;
    fastcgi_param   SERVER_NAME             $server_name;
  }
  location ~ /\.ht {
    deny  all;
  }
}


code/index.php

<?php

header('X-PHP-REQUEST_METHOD: ' . $_SERVER['REQUEST_METHOD']);

echo $_SERVER['REQUEST_METHOD'];

echo "\n\n"
?>


Result

[email protected]:~$ curl -X GET http://api.domain.com/ -d ''
GET

[email protected]:~$ curl -X POST http://api.domain.com/ -d ''
POST

[email protected]:~$ curl -X DELETE http://api.domain.com/user/1000
DELETE

[email protected]:~$ curl -X OPTIONS http://api.domain.com/user/1000
OPTIONS

[email protected]:~$ curl -X PATCH http://api.domain.com/user/1000
PATCH

[email protected]:~$ curl -I http://api.domain.com/user/1000
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Sun, 18 Jul 2021 18:05:05 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.21
X-PHP-REQUEST_METHOD: HEAD

[email protected]:~$ curl -X PUT http://api.domain.com/user/1000
PUT

R
Ross Alex, 2021-07-19
@Wacdis

I found a needle in a haystack... Everything worked for me too. The difference is in the request URL. I sent all requests to / address. But it is impossible, apparently, to send requests other than GET POST and HEAD to /
I will consider yours as the correct answer, Alexander Karabanov , since I found a difference in your advice.
Thank you!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question