0
votes

I try to run node js and php on the same domain with nginx. However the location for php ("/ajax") does not work. I always get the message "File not found.". The nginx logs print

The URL is http://localhost:8085/ajax so far, the scripts are located at /var/www/public The folder /ajax does NOT exist (none of the paths do, as everthing shall be redirected to /var/www/public/index.php)

nginx | 2017/08/27 20:47:48 [error] 6#6: *6 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.23.0.1, server: localhost, request: "GET /ajax HTTP/1.1", upstream: "fastcgi://172.23.0.4:9000", host: "localhost:8085"

nginx | 172.23.0.1 - - [27/Aug/2017:20:47:48 +0000] "GET /ajax HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36" "-"

This is my configuration, what do I have to change?

upstream react {
    server react:3000;
    keepalive 8;
}

server {
    listen 0.0.0.0:80;

    server_name  localhost;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://react;
        proxy_redirect off;
    }

    location /ajax {
        index index.php index.html;
        root /var/www/public;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    client_max_body_size 5m;
}

I tried

fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;

as suggested in some threads, but that does not work

2
Post the URL that you have accessed and also the folder where the file should have it. Also remember that ajax with go as a path in that url, so if your folder doesn't have ajax the script to run will not be found and hence the errorTarun Lalwani
Thanks for your suggestions. I updated my postSebastian

2 Answers

0
votes

So I have trying to work on your Issue for last 3 days, to get a better and deeper understanding of FASTCGI. I put a logger between and NGINX and FPM, and that helped me better understand the interactions. Below is the config that I believe should work for you

Edit-1

Updated config after resolving issue on chat

upstream react {
    server react:3000;
    keepalive 8;
}

map $fastcgi_script_name $fastcgi_script_name_fcgi {
    "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
    default $fastcgi_script_name;
}

map $request_uri $request_uri_fcgi {
    "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
    default $request_uri;
}

map $document_uri $document_uri_fcgi {
    "~*/ajax/(?P<rest_url>.*)$"   /$rest_url;
    default $document_uri_fcgi;
}

server {
    listen 0.0.0.0:80;

    server_name localhost;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://react;
        proxy_redirect off;
    }

    location /ajax {
        alias /var/www/public;
        try_files $uri @php;
    }

    location @php {
        fastcgi_split_path_info ^/ajax/(.+\.php)(/.+)$;
        fastcgi_pass fpm:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param  SCRIPT_NAME        /index.php;
        fastcgi_param  REQUEST_URI        $request_uri_fcgi;
        fastcgi_param  DOCUMENT_URI       $document_uri_fcgi;
        fastcgi_param  SCRIPT_FILENAME    /var/www/public/index.php;
    }

    client_max_body_size 5m;
}

So the basic idea is to replace /ajax from document_root using the alias directive. Then to override the request_uri and other parameters so the correct urls reach the PHP code for route resolution.

Below is part of what my php script receives when I call http://vm/ajax/router.php?x=2

{
  "COOKIES": null,
  "GET": {
    "x": "2"
  },
  "POST": [],
  "REQUEST": {
    "x": "2"
  },
  "HEADERS": null,
  "SERVER": {
    "HOME": "/var/www",
    "SCRIPT_FILENAME": "/var/www/html/router.php",
    "DOCUMENT_ROOT": "/var/www/html",
    "DOCUMENT_URI": "/router.php",
    "REQUEST_URI": "/router.php?x=2",
    "SCRIPT_NAME": "/router.php",
    "CONTENT_LENGTH": "",
    "CONTENT_TYPE": "",
    "REQUEST_METHOD": "GET",
    "QUERY_STRING": "x=2",
    "FCGI_ROLE": "RESPONDER",
    "PHP_SELF": "/router.php",
    "argv": [
      "x=2"
    ],
    "argc": 1
  }
}

As you can see nothing gets /ajax

0
votes

The current configuration works for me, however /ajax is passed to php-fpm. I was not able to get rid of the /ajax prefix in all variables like REQUEST_URI

upstream react {
    server react:3000;
    keepalive 8;
}

server {
    listen 0.0.0.0:80;

    server_name localhost;

    location / {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://react;
        proxy_redirect off;
    }

    location /ajax {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME /var/www/public$fastcgi_script_name;
        fastcgi_param QUERY_STRING    $query_string;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    client_max_body_size 5m;
}