mevdschee / php-crud-api

Single file PHP script that adds a REST API to a SQL database
MIT License
3.61k stars 1.01k forks source link

Rewrite via nginx doesn't work #343

Closed Disane87 closed 6 years ago

Disane87 commented 6 years ago

Hey man,

since I'm experimenting with microcontroller, I switched to nginx to pusch the performance of you api (because I need every ms to save battery life of my controllers).

I followed your nginx example and got it working without rewriting the url. The rewritten call wont work and I dont know why.

My nginx conf:

server {
        listen 80 default_server;
    access_log /var/www/html/logs/access.log;
    error_log /var/www/html/logs/error.log;

        server_name WohnzimmerPi3.local;
        #return 301 https://$server_name$request_uri;
        root /var/www/html/;
        index index.php index.htm index.html;
    # location ~ .php$ {
    #   fastcgi_pass 127.0.0.1:9000;
    #   fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #   fastcgi_index index.php;
    #   include fastcgi_params;

    # }
    location / {
        try_files $uri $uri/ =404;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        try_files $fastcgi_script_name =404;
        set $path_info $fastcgi_path_info;
        fastcgi_param PATH_INFO $path_info;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_pass 127.0.0.1:9000;

    }
    location /homeautomation/api/ {
        fastcgi_index api.php;
        rewrite ^/api(.*)$ /api.php$1 last;
    }
}

Error on error.log:

2018/02/23 14:04:38 [error] 24586#24586: *1 open() "/var/www/html/homeautomation/api/Devices" failed (2: No such file or directory), client: 80.150.96.114, server: wohnzimmerpi3.local, request: "GET /homeautomation/api/Devices HTTP/1.1", host: "xyzmyds.me"

My directory structure:

/var/www/html
├── /var/www/html/homeautomation
│   ├── /var/www/html/homeautomation/api <-- Your class
│   │   └── /var/www/html/homeautomation/api/lib
│   ├── /var/www/html/homeautomation/logs
│   ├── /var/www/html/homeautomation/ota
│   │   └── /var/www/html/homeautomation/ota/files
│   └── /var/www/html/homeautomation/website
└── /var/www/html/logs

Could you please assists me to get that fixed? :/

DonnyVerduijn commented 6 years ago

Your rewrite points to the wrong location. The sample in the docs assumes that api.php is located inside the root directory. It should be something like:

location /homeautomation/api/ {
    fastcgi_index api.php;
    rewrite ^/api(.*)$ /homeautomation/api/api.php$1 last;
}