0

I'm trying to create an NGINX config for PHP that will run on a docker setup. Using some NGINX/PHP guides online, I created a conf file intended to get all requests to the server except files that exist, but when I hit the base url, Chrome downloads a file with the content of my index.php, unprocessed. If I hit any other page, I 404.

server {
    index index.php;
    server_name gamersplane.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/api;

    location .* {
        try_files $uri =404;
        rewrite .* index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass api:9000;
        fastcgi_index index.php;
        fastcgi_param HTTP_PROXY '';
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

I feel like I'm supposed to create a location block for anything not .php, before the existing location block, but I can't seem to figure out where I've gone wrong. Is the conf wrong, or is there something wrong with my docker setup that's preventing communication? Not sure how to tell which.

Rohit
  • 3,018
  • 2
  • 29
  • 58
  • I just tried your config and it works great. Only difference between your and my configuration is `fastcgi_pass` try to change this line.. `api:9000` looks weird I think it is the problem. For example I use php-fpm socket: `fastcgi_pass unix:/run/php/php7.1-fpm.sock;` – Eflyax Dec 11 '18 at 04:51
  • So maybe its the docker side of it that's the problem. From what I've read, as my php-fpm is on a different container, I need to use the name I assign to it, in this case, api. – Rohit Dec 11 '18 at 04:56
  • @RhoVisions can you change `api` to container IP address and try? – Ivan Shatsky Dec 11 '18 at 05:00
  • @IvanShatsky I may not understand docker correctly, but don't think containers have internal IPs you can access outside the network? If you can, I don't know how and can't seem to find how. – Rohit Dec 11 '18 at 05:08
  • Try to change directive `location .* {` to `location ~ \.php$ {` and remove line `rewrite .* index.php;` – Eflyax Dec 11 '18 at 05:15
  • @JakubZáruba That results in `File not found`, though wouldn't that eliminate the part where all requests are directed to one file? – Rohit Dec 11 '18 at 05:19
  • That results in `File not found` even if you directly requests `gamersplane.local/index.php`? – Ivan Shatsky Dec 11 '18 at 05:22
  • @JakubZáruba Yes – Rohit Dec 11 '18 at 05:23
  • I'm not familiar with dockers, I think of them as some special kind of VM, but I read [this](https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host) and think that you can try to temporary replace `api` with IP address and test it. – Ivan Shatsky Dec 11 '18 at 05:33

1 Answers1

0

Can you try this config?

server {
    index index.php;
    server_name gamersplane.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/api;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ [^/]\.php(/.*$|$) {
        fastcgi_split_path_info ^(.*[^/]\.php)(/.*$|$);
        fastcgi_pass api:9000;
        fastcgi_index index.php;
        fastcgi_param HTTP_PROXY '';
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

This might work, but I'm not sure about api:9000 backend.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37