php-crud-api: Nginx configuration unavailable.

Hi! My app works correctly with Apache, however it throws 404 error when launching with Nginx. I see that it’s a common problem and in a few issues there is a link to Nginx config: https://github.com/mevdschee/php-crud-api#nginx-config-example

However, there is no any config. Could you please help me to make Nginx work? My local config:

server {
    listen         %ip%:%httpport%;
    listen         %ip%:%httpsport% ssl;
    server_name    %host% %aliases%;
    
    ssl_certificate               "%sprogdir%/userdata/config/cert_files/server.crt";
    ssl_certificate_key           "%sprogdir%/userdata/config/cert_files/server.key";
    
    #add_header Strict-Transport-Security "max-age=94608000";
    
    # if ($request_method !~* ^(GET|HEAD|POST)$ ){return 403;}
    location ~ /\. {deny all;}

    location / {
        root       "%hostdir%";
        index      index.php index.html index.htm;
    }

    location ~ \.php$ {
        root                               "%hostdir%";
        try_files                          $uri =404;
        # if (!-e $document_root$document_uri){return 404;}
        fastcgi_pass                       backend;
        fastcgi_index                      index.php;
        fastcgi_buffers                    4 64k;
        fastcgi_connect_timeout            1s;
        fastcgi_ignore_client_abort        off;
        fastcgi_next_upstream              timeout;
        fastcgi_read_timeout               5m;
        fastcgi_send_timeout               5m;
        fastcgi_param    CONTENT_TYPE      $content_type;
        fastcgi_param    CONTENT_LENGTH    $content_length;
        fastcgi_param    DOCUMENT_URI      $document_uri;
        fastcgi_param    DOCUMENT_ROOT     $document_root;
        fastcgi_param    GATEWAY_INTERFACE CGI/1.1;
        fastcgi_param    HTTPS             $https;
        fastcgi_param    QUERY_STRING      $query_string;
        fastcgi_param    REQUEST_METHOD    $request_method;
        fastcgi_param    REQUEST_URI       $request_uri;
        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       $host;
        fastcgi_param    SERVER_PROTOCOL   $server_protocol;
        fastcgi_param    SERVER_SOFTWARE   nginx;
        fastcgi_param    SCRIPT_FILENAME   $document_root$fastcgi_script_name;
        fastcgi_param    SCRIPT_NAME       $fastcgi_script_name;
        fastcgi_param    TMP               "%sprogdir%/userdata/temp";
        fastcgi_param    TMPDIR            "%sprogdir%/userdata/temp";
        fastcgi_param    TEMP              "%sprogdir%/userdata/temp";
    }
	
    #---------------------------------------#
    # <Не изменяйте этот блок конфигурации>
    location /openserver/ {
        root     "%sprogdir%/modules/system/html";
        index    index.php;
        %allow%allow    all;
        allow    127.0.0.0/8;
        allow    ::1/128;
        allow    %ips%;
        deny     all;
        location /openserver/server-status {
            stub_status on;
        }
        location ~ ^/openserver/.*\.php$ {
            root                               "%sprogdir%/modules/system/html";
            fastcgi_pass                       backend;
            fastcgi_index                      index.php;
            fastcgi_buffers                    4 64k;
            fastcgi_connect_timeout            1s;
            fastcgi_ignore_client_abort        off;
            fastcgi_next_upstream              timeout;
            fastcgi_read_timeout               5m;
            fastcgi_send_timeout               5m;
            fastcgi_param    CONTENT_TYPE      $content_type;
            fastcgi_param    CONTENT_LENGTH    $content_length;
            fastcgi_param    DOCUMENT_URI      $document_uri;
            fastcgi_param    DOCUMENT_ROOT     $document_root;
            fastcgi_param    GATEWAY_INTERFACE CGI/1.1;
            fastcgi_param    HTTPS             $https;
            fastcgi_param    QUERY_STRING      $query_string;
            fastcgi_param    REQUEST_METHOD    $request_method;
            fastcgi_param    REQUEST_URI       $request_uri;
            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       $host;
            fastcgi_param    SERVER_PROTOCOL   $server_protocol;
            fastcgi_param    SERVER_SOFTWARE   nginx;
            fastcgi_param    SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            fastcgi_param    SCRIPT_NAME       $fastcgi_script_name;
            fastcgi_param    TMP               "%sprogdir%/userdata/temp";
            fastcgi_param    TMPDIR            "%sprogdir%/userdata/temp";
            fastcgi_param    TEMP              "%sprogdir%/userdata/temp";
        }
    }
    # <Не изменяйте этот блок конфигурации/>
    #---------------------------------------#
}

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (8 by maintainers)

Most upvoted comments

Maurits, hello! We have solved this problem yesterday late night, but we shall be able to commit changes to our test server only today’s evening. In a few words - my colleague has discovered that adding the following line of code solves the problem:

$_SERVER[ 'PATH_INFO' ] = str_replace( '/api', '', $_SERVER[ 'REQUEST_URI' ] );

I’ll write one more message when we’ll make it work on test server. Thank you so much for your patience and advice!

Maurits, I’ll write back ASAP!

It’s better if you start it simple with the server block, here is what works for me, I’m using nginx 1.4

/etc/nginx/nginx.conf in the beginning of http block just make sure you have all the includes, you can test with the command nginx -t

user http;
worker_processes 4;

events {
  worker_connections 4096;  ## Default: 1024
}

http {
        include    /etc/nginx/mime.types;
        include    /etc/nginx/proxy.conf;
        include    /etc/nginx/fastcgi.conf;
        index      index.html index.htm index.php;

        sendfile     on;
        tcp_nopush   on;
        server_names_hash_bucket_size 128; # this seems to be required for some vhosts

        include /etc/nginx/sites-available/delivery;
        #include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-available/delivery I split the block server because I have more sites with this nginx instance, but you can use this block inside the nginx.conf file above.

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

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

    location / {
        #try_files $uri $uri/ =404;
        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location /Delivery {
        alias /usr/share/nginx/html/Delivery/;
        autoindex on;

        # rewrite rules
        rewrite ^(.*)$ $1.php last;
        #rewrite ^/Delivery/index/?$ /Delivery/index.php; # this one works with filename
        #try_files $uri /index.php?$args;
        #try_files $uri $uri/ /index.php$is_args$args;

        allow 127.0.0.1;
        allow ::1;
        deny all;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php-fpm/php-fpm.sock;
    }

    #location @extensionless-php {
    #   rewrite ^/(.*)$ /Delivery/$1.php;
    #}
}

Unfortunately it didn’t help. Now I receive the following error:

{"code":1000,"message":"Route '' not found"}

Thank you! I almost made it work on test server, here is the config:

server {
    server_name test.okna-olkon.ru;

    root  /var/www/olkon/test/;
    index index.html;

    access_log            /var/www/olkon/test_nginx_access.log;
    error_log             /var/www/olkon/test_nginx_error.log;

    client_max_body_size       512m;
    client_body_buffer_size    512m;
    proxy_buffer_size          32k;
    proxy_buffers              16 16k;
    proxy_busy_buffers_size    64k;
    proxy_temp_file_write_size 64k;
    proxy_connect_timeout       3600;
    proxy_send_timeout          3600;
    proxy_read_timeout          3600;
    send_timeout                3600;
    fastcgi_read_timeout        3600;

    location / {
       index index.html;
    }

    location /api/ {
        try_files $uri $uri/ /api.php?path=$uri?$args;
    }

    gzip on;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

    location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {
            # включать только после прочтения этого
            # expires max;
            try_files $uri =404;
    }

    location ~ \.php$ {
        set $path_info $fastcgi_path_info;
        fastcgi_index index.php;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        try_files $uri $uri/ /index.php$is_args$args;
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;

        fastcgi_param SCRIPT_FILENAME $request_filename;
  fastcgi_read_timeout 300;
    }

    location ~ /\. {
            deny all;
      log_not_found off;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/test.okna-olkon.ru/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/test.okna-olkon.ru/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 = test.okna-olkon.ru) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name test.okna-olkon.ru;
    return 404; # managed by Certbot
}

Unfortunately, when I try to make a sample request, for example

https://test.okna-olkon.ru/api/records/custom/37

I get the following:

{"code":1000,"message":"Route '\/' not found"}`

If you would be so kind to help me, I’d greatly appreciate it.