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

405 error on Nginx #1155

Open
waltergs opened this issue Sep 10, 2024 · 0 comments
Open

405 error on Nginx #1155

waltergs opened this issue Sep 10, 2024 · 0 comments

Comments

@waltergs
Copy link

waltergs commented Sep 10, 2024

Describe the bug
I'm getting this error in all POST request to my server

405 Method Not Allowed
Use of the request method is not allowed on the resource

To Reproduce
POSTMAN request:
https://invayasys.com/api/save_from_api

My Controller:

    defined('BASEPATH') OR exit('No direct script access allowed');
require_once(APPPATH."vendor/chriskacerguis/codeigniter-restserver/src/RestController.php");
require_once(APPPATH."vendor/chriskacerguis/codeigniter-restserver/src/Format.php");
use chriskacerguis\RestServer\RestController;

class Api extends RestController {

    function __construct()
    {
        // Construct the parent class
        parent::__construct();
        header("Access-Control-Allow-Origin: *");
        header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
        header("Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding");
        header("Access-Control-Allow-Credentials: true");
        header("Access-Control-Allow-Headers: *");
        $method = $_SERVER['REQUEST_METHOD'];
        if ($method == "OPTIONS") {
            die();
        }
        
        $this->load->database();
        $this->load->helper('url');
        $this->load->library('session');      
        $this->load->model('invoice');
        $this->load->model('po');
        $this->load->model('api_model');
    }
public function save_from_api_post() {
        //Load Model
        $this->load->model('api_model');
        // Get the JSON data
        $json_data = $this->security->xss_clean($this->input->raw_input_stream);
        print_r($json_data);
        

        // Call the model to save the data
        $result = $this->api_model->save_product($json_data);

        

        if ($result) {
            $this->output->set_status_header(200);
            $this->output->set_content_type('application/json');
            $this->output->set_output(json_encode(array('status' => 'success')));

        } else {
            $this->output->set_status_header(500);
            $this->output->set_content_type('application/json');
            $this->output->set_output(json_encode(array('status' => 'error')));
        }
    }

    public function test_post() {
        log_message('debug', 'POST data: ' . print_r($this->input->post(), true));
        log_message('debug', 'Raw input: ' . file_get_contents('php://input'));
        $this->response(array('status' => 'success', 'message' => 'POST received'), 200);
    }

    public function getallheaders() {
        if (!is_array($_SERVER)) {
            return array();
        }

        $headers = array();
        foreach ($_SERVER as $name => $value) {
            if (substr($name, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
            }
        }
        return $headers;
            
    }
}

My Nginx Server

server {
    root /var/www/cig;
    server_name invayasys.com;
    index index.php;
    client_max_body_size 200M;

    # CORS configuration
    location / {
        autoindex off;
        try_files $uri $uri/ /index.php?$query_string;
        client_max_body_size 200M;

        if ($bad_referer = 1) {
            access_log off;
            return 403;
        }

        # CORS headers
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;

        if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
            return 204;
        }
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_read_timeout 300;
        client_max_body_size 200M;
        fastcgi_param PHP_VALUE "upload_max_filesize=200M \n post_max_size=200M \n max_input_vars=60000";
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;

        # CORS headers for PHP scripts
        add_header 'Access-Control-Allow-Origin' '*' always;
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always;
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always;
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always;
    }

    location ~ /\.ht {
        deny all;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/invayasys.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/invayasys.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}

server {
    if ($host = invayasys.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot
    server_name invayasys.com;
    listen 80;
    return 404; # managed by Certbot
} 

Expected behavior
STATUS 200 OK in POST request

Screenshots / Error Messages

{
    "status": false,
    "error": "Unknown method"
}

Environment (please complete the following information):

  • PHP Version: [e.g. 5.6]
  • CodeIgniter Version [e.g. 2.2.6]
  • Version [e.g. 22]

Additional context
Only GET request work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant